1

is there any ways to achieve type casting of local instance of WebDriver through ThreadLocal<WebDriver> to MarionetteDriver??? My code goes like this

    public class Base_Class 
   {
    protected ThreadLocal<WebDriver> Driver = null;
    @BeforeMethod
    @Parameters("BrowserName")
    public void setUp(@Optional("Firefox") String BrowserName) throws MalformedURLException
    {

        Driver = new ThreadLocal<WebDriver>();
        if(BrowserName.equalsIgnoreCase("FireFox"))
        { 

            System.setProperty("webdriver.gecko.driver", "..//BrowserDrivers//wires");
            DesiredCapabilities capabilities = DesiredCapabilities.firefox();
            capabilities.setCapability("marionette", true);
             Driver = new <ThreadLocal<WebDriver>> MarionetteDriver(capabilities);


       }
        else  if(BrowserName.equalsIgnoreCase("Chrome"))
        {
            System.setProperty("webdriver.chrome.driver", "..//BrowserDrivers//chromedriver");
            Driver = new <ThreadLocal<WebDriver>>ChromeDriver();
        }
    }

    public WebDriver getDriver()
    {
        return Driver.get();
    }

    @AfterMethod
    public void closeBrowser()
    {
        getDriver().quit();

    }
    }

And all the test case are defined in separate classes which extends this above Base_Class.

Getting Error @ Driver = new <ThreadLocal<WebDriver>> MarionetteDriver(capabilities); and Driver = new <ThreadLocal<WebDriver>>ChromeDriver(); lines as Type mismatch: cannot convert from MarionetteDriver to ThreadLocal<WebDriver>

I am using Chrome Version 52.0.2743.116 (64-bit) and FireFox Version 48.0 version browsers on Ubuntu 14.04 Os and Selenium version selenium-server-standalone-2.53.0

Wanted to achieve parallel test execution through testng.xml file..

any help would be highly appreciated..

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Ag_Yoge
  • 77
  • 1
  • 11
  • For my knowledge, what is the reason you are using ThreadLocal for the driver? Thanks – Grasshopper Aug 25 '16 at 16:50
  • @Grasshopper: I am trying to run parallel tests through multi thread in different browser instances by using WebDriver through testng.xml.. as the Driver instance is getting overwritten by each TC, my assertion of TC is failing at teardown methods. so Wanted to create a local instance of WebDriver for each thread for passing of the TC's assertion. – Ag_Yoge Aug 26 '16 at 05:22

1 Answers1

1

You need to make the below changes in your code to make it compile.

  1. Create MarionetteDriver object with your DesiredCapabilities and
  2. Set this Driver object inside ThreadLocal object using its set method.

Like below :

if(BrowserName.equalsIgnoreCase("FireFox")) { 
  System.setProperty("webdriver.gecko.driver", "..//BrowserDrivers//wires");
  DesiredCapabilities capabilities = DesiredCapabilities.firefox();
  capabilities.setCapability("marionette", true);

  // Commented out below line from your code
  //Driver = new <ThreadLocal<WebDriver>> MarionetteDriver(capabilities);
  Driver.set(new MarionetteDriver(capabilities));
}

Try this and let me know

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
CARE
  • 618
  • 3
  • 11