TestNG Framework: How to run Parallel Tests in Selenium with TestNG

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:-

To start with, add the below mentioned dependencies to POM.xml (Maven project)

<dependencies>
  
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>3.141.59</version>
      </dependency>
      
      <dependency>
          <groupId>io.github.bonigarcia</groupId>
          <artifactId>webdrivermanager</artifactId>
          <version>5.1.0</version>
       </dependency>

      <dependency>
           <groupId>org.testng</groupId>
           <artifactId>testng</artifactId>
           <version>7.5</version>
           <scope>test</scope>
      </dependency>

  </dependencies>

Let us create a class with multiple tests. In the below Example, four test cases or methods . We want to run these methods parallelly. To achieve this, we need to add the below command in testng.xml

parallel="methods"
import static org.testng.Assert.assertTrue;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;

public class ParallelTestsExample {
	
    @Test
    public void invalidLoginTest() throws InterruptedException {
        System.out.println("Test Case 1 with Thread Id - "+Thread.currentThread().getId());

        WebDriver driver = WebDriverManager.chromedriver().create();  
        driver.manage().window().maximize();
        driver.get("https://opensource-demo.orangehrmlive.com/");    
        driver.findElement(By.name("txtUsername")).sendKeys("admin123123");
        driver.findElement(By.name("txtPassword")).sendKeys("adm");
        driver.findElement(By.id("btnLogin")).click();
        String expectedError = driver.findElement(By.id("spanMessage")).getText();
        Assert.assertTrue(expectedError.contains("Invalid credentials"));

    }

    @Test
    public void verifyLinkedIn() {

    	System.out.println("Test Case 2 with Thread Id - "+Thread.currentThread().getId());
    	
    	WebDriver driver = WebDriverManager.chromedriver().create();    
        driver.manage().window().maximize();
    	driver.get("https://opensource-demo.orangehrmlive.com/");
        Boolean linkedInIcon = driver.findElement(By.xpath("//*[@id='social-icons']/a[1]/img")).isEnabled();
        System.out.println("Actual linkedIn Text :" + linkedInIcon);
        assertTrue(linkedInIcon);
    }

    @Test
    public void validLoginTest() throws InterruptedException {

        System.out.println("Test Case 3 with Thread Id - "+Thread.currentThread().getId());

        WebDriver driver = WebDriverManager.chromedriver().create();  
        driver.manage().window().maximize();    
        driver.get("https://opensource-demo.orangehrmlive.com/");
        driver.findElement(By.name("txtUsername")).sendKeys("Admin");
        driver.findElement(By.name("txtPassword")).sendKeys("admin123");
        driver.findElement(By.id("btnLogin")).click();
        String expectedTitle = driver.findElement(By.xpath("//*[@id='content']/div/div[1]/h1")).getText();
        Assert.assertTrue(expectedTitle.contains("Dashboard"));
    }

    @Test
    public void forgotLinkTest() throws InterruptedException {

        System.out.println("Test Case 4 with Thread Id - "+Thread.currentThread().getId());

        WebDriver driver = WebDriverManager.chromedriver().create();      
        driver.manage().window().maximize();    
        driver.get("https://opensource-demo.orangehrmlive.com/");
        String expectedLink = driver.findElement(By.id("forgotPasswordLink")).getText();
        Assert.assertTrue(expectedLink.contains("Forgot your password?"));
    }
}

Now, let us create a testng.xml. Right click on the project and select TestNG -> Convert to TestNG.

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

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Parallel Tests" parallel = "methods" thread-count="4">
    <classes>
      <class name="com.example.parallel.ParallelTestsExample"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

How to run the tests?

Right-click on testng.xml and select 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.

Execution

Here it can be seen that 4 tests were running on thread no – 19, 20, 21 and 22. Out of all 4 tests, browser for only 1 test is closed and rest 3 browsers are left open.

First thread initialized a browser and set a value to static WebDriver reference. Second thread initialized another browser and set a new value to the same static WebDriver reference and this will impact value set by first thread as it is a static. All threads wanted to close same browser that is the reason there is one configuration method failure as one browser is closed another threads will not find sessions to close the browsers. Browser was closed already so last 3 tests did not able to close the browser.

To overcome this issue, will use ThreadLocal<WebDriver>. The complete program looks like as below:

First, I will create a HelperClass which contains the initialization of driver and closing the driver. I like to keep the tests only in Test Class. This is not mandatory. You can combine the code of both classes in one also.

HelperClass

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import io.github.bonigarcia.wdm.WebDriverManager;

public class BaseClass {
		
	private static final ThreadLocal<WebDriver> driver = new ThreadLocal<WebDriver>();
	  
	    @BeforeMethod
	    public void setDriver()  {
	    	
	    	WebDriverManager.chromedriver().setup();
			driver.set(new ChromeDriver());
			driver.get().get("https://opensource-demo.orangehrmlive.com/");
			driver.get().manage().window().maximize();
	         System.out.println("Before method Thread Id:" + Thread.currentThread().getId());         
	     
	    }


	    public WebDriver getDriver() {
	        return driver.get();
	    }


	    @AfterMethod
	    public  void closeBrowser() {
	    	System.out.println("After method Thread Id:" + Thread.currentThread().getId());
	    	driver.get().quit();
	    	driver.remove();		  
	    }
	}

ParallelTestsExampleWithThread

import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;

public class ParallelTestsExampleWithThread extends BaseClass {

	@Test
    public void invalidLoginTest() {
        
		System.out.println("Test Case 1 with Thread Id - "+Thread.currentThread().getId());
        
        getDriver().findElement(By.name("txtUsername")).sendKeys("admin123123");
        getDriver().findElement(By.name("txtPassword")).sendKeys("adm");
        getDriver().findElement(By.id("btnLogin")).click();
        String expectedError = getDriver().findElement(By.id("spanMessage")).getText();
        Assert.assertTrue(expectedError.contains("Invalid credentials"));

    }
	
	@Test
    public void blankLoginTest() {
        
		System.out.println("Test Case 2 with Thread Id - "+Thread.currentThread().getId());
        
        getDriver().findElement(By.name("txtUsername")).sendKeys("");
        getDriver().findElement(By.name("txtPassword")).sendKeys("");
        getDriver().findElement(By.id("btnLogin")).click();
        String expectedError = getDriver().findElement(By.id("spanMessage")).getText();
        Assert.assertTrue(expectedError.contains("Username cannot be empty"));

    }

	@Test
    public void validLoginTest() throws InterruptedException {

        System.out.println("Test Case 3 with Thread Id - "+Thread.currentThread().getId());
        
        getDriver().findElement(By.name("txtUsername")).sendKeys("Admin");
        getDriver().findElement(By.name("txtPassword")).sendKeys("admin123");
        getDriver().findElement(By.id("btnLogin")).click();
        Thread.sleep(5000);
        String expectedTitle = getDriver().findElement(By.xpath("//*[@id='content']/div/div[1]/h1")).getText();
        Assert.assertTrue(expectedTitle.contains("Dashboard"));
    }
	
	@Test
    public void forgotLinkTest() {

        System.out.println("Test Case 4 with Thread Id - "+Thread.currentThread().getId());

        String expectedLink = getDriver().findElement(By.id("forgotPasswordLink")).getText();
        Assert.assertTrue(expectedLink.contains("Forgot your password?"));
    }
   
}

The output of the above program is

Report Generation

TestNG generates 2 reports – emailable-report.html and index.html

Emailable-Report.html

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

This report gives a summary of all the tests executed, passed, failed, skipped and retried with their respective execution time.

Index.html

This report provides the detailed description of the tests like no of tests present, no of methods, time taken by each step, total time taken by each steps, testng.xml data and soon.

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. It is shown below that all the tests are running on Thread 1 that means once a test ends then another test starts on that thread.

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

Test Reports

Emailable-Report.html

Index.html

Congratulations. We are able to run methods parallelly using TestNG.

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

Leave a reply to Vibha Cancel reply