0

I am opening multiple instance of browser for each data set but all the input data is getting entered only in one instance/session instead of each data set in each instance. I am using selenium and TestNG

@DataProvider(name="URLprovider", parallel=true )
private Object[][] getURLs() {
  return new Object[][] {
      {"Fist data"},
      {"Second Data"},
      {"3 data"}
  };
 }

 @Test(dataProvider="URLprovider",threadPoolSize = 3)
 public void testFun(String url){
    BaseDriver baseReference = BaseDriver.getBaseDriverInstance();      
  System.out.println("Test class"+url +"=" 
       +Thread.currentThread().getId());

    driver = baseReference.initBrowser();       
    driver.get("http://stackoverflow.com/"); 
driver.findElement(By.xpath("//*@id='search']/div/input")).sendKeys(url);

}

So here i am opening three browser instance parallel (as we have 3 set of data in @dataprovider ) and entering value in text box. But while executing the code 3 instance is getting opened but test data value is entered only in one instance... but my expectation is to enter one data in one instance.

1 Answers1

0

The problem lies in your test code.

The test code that you shared as part of testFun() seems to suggest that you are making use of the same WebDriver instance amongst all of your @Test iterations. You haven't shown us what BaseDriver baseReference = BaseDriver.getBaseDriverInstance(); looks like, but going by your issue I am assuming that its returning the same webdriver instance.

That explains why all of your test methods seem to be sharing the same webdriver instance.

To fix this issue, you would need to do one of the following:

  1. Move your webdriver instantiation logic inside your test method i.e., testFun() (or)
  2. Create a @BeforeMethod configuration method which would be responsible for creating a browser instance and persist that within a ThreadLocal<RemoteWebDriver> instance and your test method viz., testFun() gets the current thread's webdriver instance via driver.get() [ here driver is of type ThreadLocal<RemoteWebDriver>. Don't forget to declare driver as a static variable.
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66