2

I am trying to make my WebDriver threadSafe but when i run the test i get the nullPointerException. I then checked if my remote WebDriver get initialized but it doesn't and am not sure where am going wrong. I am using help from online to make my WebDriver threadSafe as i dont know how to.

Here my code.

    DesiredCapabilities cap = null;
    private List<WebDriver> webDriverPool = Collections.synchronizedList(new ArrayList<WebDriver>());
    private ThreadLocal<WebDriver> driverThread;

    @Parameters ({"browser"})
    @BeforeTest
    public void launchBrowser(String browser) {
        //initLogs(this.getClass());
        initConfigurations();
        if(browser.equals("firefox")){
            cap = DesiredCapabilities.firefox();
            cap.setBrowserName("firefox"); // 
            cap.setPlatform(Platform.WINDOWS);
        }else if (browser.equals("chrome")){
            cap = DesiredCapabilities.chrome(); // no need path of chrome exe
            cap.setBrowserName("chrome");
            cap.setPlatform(Platform.WINDOWS);
        }else if (browser.equals("internet explorer")){
            cap = DesiredCapabilities.internetExplorer(); // no need path of ie exe
            cap.setBrowserName("internet explorer");
            cap.setPlatform(Platform.WINDOWS);
            cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            cap.setCapability("ie.ensureCleanSession", true);
            }
        this.driverThread   = new ThreadLocal<WebDriver>()  {
            @Override
            protected WebDriver initialValue(){
              WebDriver webDriver = null;
              System.out.println(webDriver);
            try {
                webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
                webDriverPool.add(webDriver);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
              System.out.println(webDriver);
              return webDriver;
            }
          };
          getDriver().manage().window().maximize();
          getDriver().manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

    public  WebDriver getDriver() {
        return this.driverThread.get();
      }

    @AfterTest
     public void afterTest() {
         for (WebDriver driver : this.webDriverPool) {
              driver.quit();
        }
 }

Exception trace

java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:34)
at com.sun.proxy.$Proxy9.sendKeys(Unknown Source)
at ww5.pages.LoginPage.doLogin(LoginPage.java:31)
at ww5.testcases.storageRuleSuite.GoToStorageRulePageTest.goToStorageRulePageTest(GoToStorageRulePageTest.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Ahmed Yaslem
  • 123
  • 1
  • 4
  • 20

1 Answers1

0

try instantiating like this:

public static ThreadLocal<Webdriver> driver=new ThreadLocal<Webdriver>();

Then before you invoke any method on the driver instance set it like this:

driver.set(new FirefoxDriver());

and then use it:

driver.get();
Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71
  • I set the above but it runs okay on my first test but i get null exception on the test to follow. I am using POM Page factory framework, do i need to specify 'public static ThreadLocal driver=new ThreadLocal();' in all my page class – Ahmed Yaslem Aug 11 '15 at 08:54
  • No need. just use .driver.get() everywhere. TestNG will then take care of assigning new you static local instance to each thread. the initialization of the driver should happen when a new test instance gets created. – Mrunal Gosar Aug 15 '15 at 20:29