Parallel Testing Using TestNG in Selenium

Hello Friends, In this post we will see How to do ‘Parallel testing using TestNG in Selenium’. For Parallel testing in selenium we will use TestNG third party tool. We will also see ‘Parallel execution using DataProvider in TestNG’.

QA teams always try to reduce the testing time in automation but without using parallel test execution it is not possible.

If teams move from sequential testing to parallel testing then they can cover maximum coverage with reduced execution time.

What is Parallel Testing?

In parallel testing, we can test multiple applications or modules on multiple browsers in parallel means one by one.

Parallel testing is different from sequential testing, where we test different modules one after the other.

We can also perform the test execution sequentially on different browsers but it is very time consuming.

Parallel testing reduced the execution time and efforts.


Parallel Testing using TestNG and Selenium

TestNG (Where NG stands for Next Generation) is an open source Java-based framework. This framework itself is inspired by JUnit.TestNG allows us to run test scripts in parallel mode or multithreaded mode by utilizing the Multi-Threading concept of Java.

TestNG provided auto-defined testng.xml file where we can set the parallel attribute to method/tests/classes and by using the concept of multi-threading of Java, one can set the number of threads. Below is the syntax of defining this attributes in testng.xml file.


<suite name="Parallel_Testing" parallel="methods" thread-count="2">

We can use four different values in parallel attributes.

Tests – All test cases inside <test> tag of testng.xml will run in parallel.

Classes– All test cases inside <classes> tag or Java class will run parallel.

Methods- All test methods which are in @Test annotation will execute parallel.

Instances – It will run all test methods which are in the same instances in same thread.

In above syntax I have given thread-count=2. Thread count is used to open webdriver instances in different thread. Thread-count=2 means two webdriver instances can run parallel in two different browsers.

Testng.xml file –

Parallel testing using TestNG in Selenium



Selenium code for Parallel Test Execution with TestNG.xml

package datapkg;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class ParallelTesting {
    public void A() throws InterruptedException {
        System.setProperty("", "E:\\Selenium\\chromedriver_win32_2\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        boolean text = driver.getPageSource().contains("CSS Tooltip");
    public void B() throws InterruptedException {
        System.setProperty("", "E:\\Selenium\\chromedriver_win32_2\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        boolean text = driver.getPageSource().contains("CBSE Syllabus");



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name="TestSuite" thread-count="2" parallel="methods" >
<test name="testAutomation">
<class name="datapkg.ParallelTesting">

Note – Here I passed parallel attribute value as a method so it will execute @Test annotation methods in parallel

Parallel Test Execution using DataProvider in TestNG

Apart from running test cases in parallelly through the testng xl file, TestNG also provides

Dataprovider method to achieve the parallel execution.

Code for Parallel Test Execution using DataProvider:

package datapkg;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTestNG {
        public void EmployeeEntry(String First_Name, String Last_Name) {
            System.out.println("Employee Name: " +First_Name + "'" + Last_Name );
        @DataProvider(name="DataContainer", parallel=true)
        public Object[][] myDataProvider(){
            Object data[][] = new Object[3][2];
            // First Employee details

            // Second Employee details

            // Third Employee details
            return data;



Parallel Test Execution using DataProvider


You can see console output not in sequence of data provided by DataProvider method. Means It was run in parallel.


Parallel testing using TestNG is beneficial in saving time and putting lesser efforts. So using Testng.xml file and using DataProvider method we can perform parallel testing.

Hope this post is helpful to improve you knowledge in automation.

So, bookmark this post “Parallel testing using TestNG in Selenium” for future reference.


Must Read:

TestNG Listeners in Selenium Webdriver

Most Popular TestNG Interview Questions

Leave a Comment