This is my test class where I want to execute each input of Data Provider in new browser parallely. I am able to open new browsers but I get Session Not Found Exception and no such element exception
public class DemoTest {
private WebDriver driver;
@Test(dataProvider = "dp")
public void f(Integer n, String s) {
try {
System.out.println("Driver: "+driver.toString());
driver.get("www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("1234567");
System.out.println("method f id:"+Thread.currentThread().getId()+" n:"+n+" s:"+s);
}
catch(Exception e) {
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
try {
driver= new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
System.out.println("Before method id:"+Thread.currentThread().getId());
}
catch (Exception e) {
e.printStackTrace();
}
}
@AfterMethod
public void afterMethod() {
try {
System.out.println("After method id:"+Thread.currentThread().getId());
if(driver != null ) {
driver.quit();
// driver.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
@DataProvider(parallel=true)
public Object[][] dp() {
return new Object[][] {
new Object[] { 1, "a" },
new Object[] { 2, "b" },
new Object[] { 3, "c" },
new Object[] { 4, "d" },
};
}
}
This is testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite" parallel="methods">
<test name="prelogin">
<classes>
<class name="com.package.DemoTest"></class>
</classes>
</test>
</suite>
Could anyone tell me why I get this exception & how to solve it?
You're sharing a
WebDriver
instance across multiple threads. As such, one test can try to use theWebDriver
after another test'safterMethod
has already quit it (hence the session not found exception) or hasn't loaded google.com yet (hence the no such element exception).If you want to run Selenium WebDriver tests using TestNG in parallel then you can use a
ThreadLocal
to maintain aWebDriver
instance per thread.e.g.
I've omitted wrapping each test with a try-catch block as TestNG already catches exceptions thrown by test/configuration methods and prints stack traces for them, etc.
The above example will create a
WebDriver
instance associated with the test's thread for each test. i.e. If they were to all run truly in parallel you would have 4 instances of Firefox open (which I think is what you want).If you have more tests than threads and want to reuse
WebDriver
instances then you can use a ThreadLocal with an initial value.e.g.
And a testng.xml to reduce the total number of threads to demonstrate this:
Example output (see how it only creates 2 FirefoxDriver instances, runs two tests in parallel, and then runs the next two tests in parallel, using all the available threads and reusing the web drivers for each thread):
Issue has nothing to with data providers. The problem lies in
driver.get("www.google.com");
. WebDriver api sends RESTfull request to Selenium server to execute a command. REST services useshttp
orhttps
protocol for request/response. Hence when we call the url without mentioning the protocol prefix (in this case http), exception is thrown.Change to :
This will work.