Reputation: 471
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
Upvotes: 1
Views: 4051
Reputation: 471
I have launched multiple(minimum 2) chrome browsers in Parallel by using the below site.
Thread Local for Parallel Test Execution
To achieve this scenario, I have created 3 classes. One for WebDriver initialization and other classes for Threads.
Class 1
public class SetTestNG implements Runnable
{
public String xmlString;
public SetTestNG(String suitXMLUrl){
xmlString = suitXMLUrl;
}
@Override
public void run(){
List<String> testSuites = Lists.newArrayList();
testSuites.add(xmlString);
TestNG testng = new TestNG();
testng.setTestSuites(testSuites);
testng.run();
}
}
Class 2 : Main Class
public class MultiThread
{
private static String inputFiles;
private static String[] xmlFile;
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, InterruptedException
{
inputFiles = args[0];
xmlFile = inputFiles.split(",");
for(String file : xmlFile)
{
Thread object1 = new Thread(new SetTestNG(System.getProperty("user.dir")+"/"+file));
object1.start();
Thread.sleep(5000);
}
}
}
Note: The main use case of this code to launch Chrome browser for every single testNG Suite files.
Upvotes: 2
Reputation: 14746
It looks like you want your webdriver instance to be created only once per <suite>
tag and then shared across @Test
annotated test methods that reside amongst multiple <test>
tags.
For achieving this, you would need to change your LaunchBrowser
class to something like below :
public class LaunchBrowser {
protected WebDriver driver;
@org.testng.annotations.BeforeSuite
public void initDriver() {
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
}
You also need to refactor all your tests, so that they don't explicitly call LaunchBrowser.initDriver()
else, it would cause the webdriver instantiation to be happened twice - explicitly by your call and implicitly once by TestNG due to the method being annotated using a @BeforeSuite
annotation.
That should solve your use case. But please remember that this is the most in-efficient way of managing your webdriver instance, because you are now strictly confined to sequential execution. You cannot run tests in parallel.
Upvotes: 0
Reputation: 1289
You again need to initilize driver in your second Test method of both class
public class LaunchBrowser
{
public WebDriver driver =null;
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 2
public class StackLogin extends LaunchBrowser
{
@Test
public void 1test() throws InterruptedException
{
initDriver();
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 2
public class Gmail extends LaunchBrowser
{
@Test
public void 1test() throws InterruptedException
{
initDriver();
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);
}
}
Upvotes: 0