When we use dataprovider with parallel tests then in the second browser it is not opening up the URL

24 views
Skip to first unread message

prasanth kotagiri

unread,
May 22, 2018, 10:40:32 AM5/22/18
to Selenium Users
Hi All,

Below is my code

ublic class SampleTest1 {

public WebDriver driver;

@DataProvider(name = "Authentication", parallel=true)

public static Object[][] credentials() {

return new Object[][] { { "editor...@yopmail.com", "Medscape@123" },{ "editor...@yopmail.com", "Medscape@123" } };

}

/*@BeforeMethod
public void openBrowser() throws InterruptedException {
System.out.println("Creating Chrome object");
System.setProperty("webdriver.chrome.driver", "D:/Library/chromedriver_win32/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();

Thread.sleep(20000);
}*/

@Test(dataProvider = "Authentication",threadPoolSize = 3,invocationCount = 1)
public void sampletset1(String username, String password) throws InterruptedException {
System.out.println("Creating Chrome object");
System.setProperty("webdriver.chrome.driver", "D:/Library/chromedriver_win32/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Long id = Thread.currentThread().getId();
System.out.println("Test method executing on thread with id: " + id);
driver.findElement(By.xpath("//*[@id='edit-name']")).sendKeys(username);
driver.findElement(By.xpath("//*[@id='edit-pass']")).sendKeys(password);

}
}

In the above code it is opening 2 chrome browsers parallel at a time.

But in the first browser it is not opening up the URL
But in the second browser it is opening the URL and entering the 2 credentials in the same username box and password box.

My Expectation: It should open 2 browsers and in one browser it should login with first user and in second browser it should login with second user.

Actual Result: It is not opening the url in first browser and in the second browser it is doing all the remaining thing as i mentioned above.

Please help me ASAP

Jonathan Herbaut

unread,
May 22, 2018, 10:52:20 AM5/22/18
to seleniu...@googlegroups.com
That's because your webDriver object is common in your parallel class. The first parallel run, you instanciate your webdriver that brings you a session to link your browser to the webdriver. During this time, your parallel code is executing. And he is trying to instanciate another webdriver in the same class in your code. And this instance erase your previous one. You lost the session Id and all your code is executing with the last session Id you get (so the last browser).
If you are working with Grid, you will see in the console that some of your sessions are used but never closed. Once you will correct your problem, you will fix another one : RAM consumption (you open and never close the first chrome for the moment) ! :)

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/49c486fb-f668-4f48-a917-4d7fb8094170%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

prasanth kotagiri

unread,
May 22, 2018, 11:04:13 AM5/22/18
to Selenium Users
HI,
Thanks for your support. How to overcome this one?
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

Krishnan Mahadevan

unread,
May 22, 2018, 11:15:57 AM5/22/18
to seleniu...@googlegroups.com

Have your @BeforeMethod initialize a ThreadLocal variant of the WebDriver instead of the class level data member.

 

Change: public WebDriver driver;

To: private static final ThreadLocal<WebDriver> driver = new ThreadLocal<>();

 

Change: driver = new ChromeDriver();

To: driver.set(new ChromeDriver());

 

Create a method such as the one below to get access to the driver object :

 

private WebDriver getDriver() {

    return driver.get();

}

 

And use “getDriver()” instead of “driver” through-out your code.

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Jonathan Herbaut

unread,
May 22, 2018, 11:44:52 AM5/22/18
to seleniu...@googlegroups.com
I will look at this solution.
I have instanciate and declared my webDriver object in the test directly.
I have to use generic classes with my webdriver that's why I made this.
Reply all
Reply to author
Forward
0 new messages