TestNG Framework: How to run Parallel Tests with Selenium

HOME
 

What is Parallel Testing?

Parallelism or multi-threading in software terms is defined as the ability of the software, operating system, or program to execute multiple parts or sub-components of another program simultaneously. 

Parallel testing helps to reduce execution time and efforts and results in faster time to delivery.  In a scenario where we have two versions of software available, and we need to check its stability and compatibility, we can run the two versions simultaneously and find issues at a much faster rate.

How to run Parallel Tests with Selenium

TestNG provides multiple ways to execute tests in separate threads. In testng.xml, if we set ‘parallel’ attribute on the tag to ‘methods’, testNG will run all the ‘@Test’ methods in tag in a separate thread.

The project Structure looks like shown below:-

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

The attribute thread-count allows you to specify how many threads should be allocated for this execution.

parallel = “methods” means that the methods will run parallel

The parallel attribute can be extended for multiple values, as below:

·         Methods: Helps run methods in separate threads

·         Tests: Help to run all methods belonging to the same tag in the same thread, means tests will run sequentially

·         Classes: Helps to run all methods belonging to a class in a single thread

·         Instances: Helps run all methods in the same instance in the same thread

How to run Tests parallel

Right click on TestNG.xml and Run AS TestNG Suite. If you will run the Test Class – ParallelTestDemo.java as Right click and then Run As TestNG Tests, then the methods will run sequentially.

In this Example, three test cases or methods will run parallel and enter search word like TestNG Framework, Maven, and Selenium WebDriver in Google.

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
 
public class ParallelTestDemo {

      @Test
       public void executSessionOne() {
            System.setProperty("webdriver.chrome.driver",
                                              "C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();

             // Goto google site
             driver.get("https://www.google.com/ ");

             // enter word in search box
              WebElement Element1 = driver.findElement(By.name("q"));
              Element1.sendKeys("Selenium WebDriver");
              Element1.sendKeys(Keys.ENTER);
              System.out.println("Test Case One with Thread Id:-"+Thread.currentThread().getId());
         }
 
       @Test
       public void executeSessionTwo() {
                        
            // Second session of WebDriver
            System.setProperty("webdriver.chrome.driver",
                                              "C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
                        
           // Goto google site
           driver.get("https://www.google.com/ ");

           // enter word in search box
           WebElement Element2 = driver.findElement(By.name("q"));
           Element2.sendKeys("TestNG Framework");
           Element2.sendKeys(Keys.ENTER);
            System.out.println("Test Case Two with Thread Id:-"+Thread.currentThread().getId());
      }
 
        @Test
        public void executSessionThree() {

             // Third session of WebDriver
              System.setProperty("webdriver.chrome.driver",
                                              "C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
              WebDriver driver = new ChromeDriver();

              // Goto google site
               driver.get("https://www.google.com/ ");

              // enter word in search box
               WebElement Element3 = driver.findElement(By.name("q"));
               Element3.sendKeys("Maven");
               Element3.sendKeys(Keys.ENTER);
               System.out.println("Test Case Three with Thread Id:-"+Thread.currentThread().getId());
        }
}

Output
Test Case Three with Thread Id:- 14
Test Case Two with Thread Id:- 15
Test Case One with Thread Id:- 13
 
===============================================
TestSuite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
 
===============================================

TestNG Report – Go to test-output folder and open emailable-report.html

Run Tests Sequentially

If you will run the Test Class – ParallelTestDemo.java as Right click and then Run As TestNG Tests, then the methods will run sequentially. Here all tests are run with Thread 1 whereas with parallel execution tests were run with different threads.

We can make parallel = none, if don’t want to run them parallel.

<suite name="TestSuite" thread-count="3" parallel="none" >

4 thoughts on “TestNG Framework: How to run Parallel Tests with Selenium

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s