My scenario is to launch multiple chrome browsers(minimum 2) in Parallel.
I have created a separate class for WebDriver initialization, also I have 2 xml files and in that file it has 2 tests each.
WebDriver Initialization
public class LaunchBrowser
{
public WebDriver driver;
public WebDriver initDriver() {
if (driver == null) {
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
return driver;
}
}
XML file 1 : test method 1
public class Stackoverflow extends LaunchBrowser
{
@Test
public void 1test() throws InterruptedException
{
initDriver();
driver.get("https://stackoverflow.com");
Thread.sleep(3000);
System.out.println("Stack");
}
@Test
public void 2test() throws InterruptedException
{
Thread.sleep(3000);
}
}
XML file 1 : Test method 2
public class StackLogin extends LaunchBrowser
{
@Test
public void 1test() throws InterruptedException
{
driver.findElement(By.xpath("//a[@href='https://stackoverflow.com/users/login?
ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2f']")).click();
Thread.sleep(3000);
}
@Test
public void 2test() throws InterruptedException
{
Thread.sleep(3000);
}
}
XML file 2 : Test method 1
public class Google extends LaunchBrowser
{
@Test
public void 1test() throws InterruptedException
{
initDriver();
driver.get("https://www.google.co.in");
Thread.sleep(3000);
System.out.println("Google");
}
@Test
public void 2test() throws InterruptedException
{
Thread.sleep(3000);
}
}
XML file 2 : Test Method 2
public class Gmail extends LaunchBrowser
{
@Test
public void 1test() throws InterruptedException
{
driver.findElement(By.xpath("//a[@href='https://mail.google.com/mail/?tab=wm'][text()='Gmail']")).click();
Thread.sleep(3000);
}
@Test
public void 2test() throws InterruptedException
{
Thread.sleep(3000);
}
}
testng1.xml
<suite name="Suite1">
<test name="01Stackoverflow">
<classes>
<class name="com.ci.selenium.Stackoverflow" />
</classes>
</test>
<test name="02StackLogin">
<classes>
<class name="com.ci.selenium.StackLogin" />
</classes>
</test>
</suite>
testng2.xml
<suite name="Suite2">
<test name="1Google">
<classes>
<class name="com.ci.selenium.Google"/>
</classes>
</test>
<test name="2Gmail">
<classes>
<class name="com.ci.selenium.Gmail"/>
</classes>
</test>
</suite>
Also I have made the below configurations in my pom.xml file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>${file}</suiteXmlFiles>
<skipTests>false</skipTests>
<properties>
<property>
<name>suitethreadpoolsize</name>
<value>2</value>
</property>
</properties>
</configuration>
</plugin>
Finally I have triggered the XML file using the below maven command.
mvn clean test -Dfile=MyWork/testng1.xml,MyWork/testng2.xml
Result:
Two Chrome browsers were launched at a time, but only first test method in each xml file got passed and the second test in both xml files gets failed.
Kindly help me to fix this issue.
Logs
java.lang.NullPointerException
at com.ci.selenium.StackLogin.1test(StackLogin.java:12)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
... Removed 18 stack frames