Configure parallel execution of tests using TestNG selenium

In testing, it is always important to test application in different browsers. We can perform automation on multiple browsers using selenium and testng. If there are more number of tests that need to be executed in parallel on different browsers also, we can do this using testng. And there is an other challenging task such executing tests in different browser versions and also different Operating systems. This can be achieved with Selenium Grid. You can check article on 'Parallel execution of tests' using selenium grid

We will look into the below examples in detail:

Below is the sample test which will only run one browser at a time and to run all the browsers in parallel, we need to add a parameter in testng.xml file which we will see below

package com.pack;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelTest {

	private WebDriver driver;
	String baseURL = "http://www.google.com/";

	@Parameters({ "browser" })
	@BeforeTest
	public void openBrowser(String browser) {
		try {
			if (browser.equalsIgnoreCase("Firefox")) {
				driver = new FirefoxDriver();
			} else if (browser.equalsIgnoreCase("chrome")) {
				System.setProperty("webdriver.chrome.driver",
						"D:/chromedriver.exe");
				driver = new ChromeDriver();
			} else if (browser.equalsIgnoreCase("IE")) {
				System.setProperty("webdriver.ie.driver",
						"D:/IEDriverServer.exe");
				driver = new InternetExplorerDriver();
			}
		
		} catch (WebDriverException e) {
			System.out.println(e.getMessage());
		}
	}

	@Test
	public void login_TestCase() {
		driver.navigate().to(baseURL);
                //do something
	}

	@Test
	public void search_TestCase() {
		driver.navigate().to(baseURL);
             //do something
	}

	@AfterTest
	public void closeBrowser() {
		driver.quit();
	}
}

In the above code, we have OpenBrowser method with BeforeTest annotation along with parameter 'browser'. In the xml we will define three tests tags to run each test with different browser. We will compare the browser value with the parameter value and based on that we will create the driver instance. We have now defined three tests with three browsers (Firefox, Google Chrome and Internet Explorer)

You can find more details about defining parameters in testng

Below is the testng.xml file which will run all the tests which are defined. We are passing parameter value with browser name for each test. Tests will be executed in there browsers one by one.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite">
  <test name="Firefox Test">
  <parameter name="browser" value="Firefox"/>
    <classes>
      <class name="com.pack.ParallelTest"/>
    </classes>
  </test>
  <test name="Chrome Test">
  <parameter name="browser" value="chrome"/>
    <classes>
      <class name="com.pack.ParallelTest"/>
    </classes>
  </test>
    <test name="Internet Explorer Test">
    <parameter name="browser" value="IE"/>
    <classes>
      <class name="com.pack.ParallelTest"/>
    </classes>
  </test>
</suite>

We can also use TestNG to execute tests Simultaneously by defining the "parallel" attribute to "tests" to run all the tests in defined browser with the help of testng.xml configuration file.

<suite name="Parallel test suite" parallel="tests">

We just need to update the single line in above testng.xml. By defining parallel="tests" in suite tag, tests will get executed simultaneously. In order to execute to execute tests simultaneously it is recommended to have good system configuration, so that the execution time can be saved.

You can find more details about Executing multiple tests using testng

It looks like below:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="tests">
  <test name="Firefox Test">
  <parameter name="browser" value="Firefox"/>
    <classes>
      <class name="com.pack.ParallelTest"/>
    </classes>
  </test>
  <test name="Chrome Test">
  <parameter name="browser" value="chrome"/>
    <classes>
      <class name="com.pack.ParallelTest"/>
    </classes>
  </test>
    <test name="Internet Explorer Test">
    <parameter name="browser" value="IE"/>
    <classes>
      <class name="com.pack.ParallelTest"/>
    </classes>
  </test>
</suite>

After executing you can view the report, which will look like below:

Multi browser tests

Selenium Tutorials: 

Comments

Above code is not working for me, it is giving report as below
[TestNG] Running:
C:\compatibility\org.compatibility\testng.xml

===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 0
===============================================
Can anybody help me to get ans for this

Implemented TestNG, Selenium, Maven. The params are defined in the TestNg.xml file. Parameters anotation is calling the parameter name 'browser'. but the values are not received.
@Parameters({ "browser" })
@Test
public void setup(String browser){
---
---
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Chrome Test">
<parameter name="browser" value="Chrome"/>
<classes>
<class name="com.org.MavenSeleniumScripts.SmokeTest"/>
</classes>
</test>

<test name="IE Test">
<parameter name="browser" value="IE"/>
<classes>
<class name="com.org.MavenSeleniumScripts.SmokeTest"/>
</classes>
</test>

It looks strange. It should work.
Are you getting any exception?

FAILED CONFIGURATION: @BeforeTest openBrowser
org.testng.TestNGException:
Parameter 'BROWSER' is required by @Configuration on method openBrowser

Above is the error i am encountering i have done everything exactly same way as stated.

Make sure the parameter is same in both xml and @BeforeTest method

I have browser value calling from excel sheet and this is overriding the @Parameters. How to handle this scenario

**Please write @beforeTest annotation just after @Parameters.

Exceptions i got and their solution
1. class not found error
-- Make sure that you have not provided .java extension in testNG.xml
2. Tests not running
-- Please write @beforeTest annotation just after @Parameters.

What if i am using a data provider and i have 2 or more set of data to be tested in parallel . What changes i am supposed to do?

Add new comment