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

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)
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.

<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.
It looks like below:

<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

Followers