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.

Syntax-

<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.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class ParallelTesting {
    
    @Test
    public void A() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32_2\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.w3schools.com/css/css_tooltip.asp");
        driver.manage().window().maximize();
        Thread.sleep(2000);
        boolean text = driver.getPageSource().contains("CSS Tooltip");
        Assert.assertTrue(text);
        driver.close();
        
        
}
    
    @Test
    public void B() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32_2\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.tutorialspoint.com/index.htm");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//a[@href='https://www.tutorialspoint.com/tutorialslibrary.htm']//following::span[contains(text(),'Library')]")).click();
        Thread.sleep(2000);
        boolean text = driver.getPageSource().contains("CBSE Syllabus");
        Assert.assertTrue(text);
        driver.close();

 }
    
}

TestNG.xml

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

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 {
    
    
        
        @Test(dataProvider="DataContainer")
        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
            data[0][0]="John";
            data[0][1]="Karter";
            

            // Second Employee details
            data[1][0]="Meli";
            data[1][1]="Wilson";
            

            // Third Employee details
            data[2][0]="Ajeet";
            data[2][1]="Maurya";
            
            return data;
            
            
            
            
        }
        
        

}

Output:

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.


Conclusion

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