How to perform tests in parallel execution in Selenium?

We define multiple test methods inside @Test annotation and all these definitions are done within a TestNG class. Thus, we can create multiple TestNG classes, hence, the multiple test methods can be written inside each TestNG class. Now our goal is to run these classes and each method simultaneously, that is, in a parallel execution. Suppose there are 20 test methods and 100 classes so total test cases would be 2000. Some test cases require sequential run, and some could be considered to run in parallel execution. If we ignore parallel run for a while then think how much time it will take to finish 2000 test cases, even if it takes 30 seconds to complete one test case. Hence, it will be around 60000 seconds. So, the total time would be 16 hours to finish the test execution. Is this worth?

In this course of time, it will be good to test all the test cases manually, because automation is going to take a big time.

Now the question comes here, can we manage to reduce the time of the test execution?

The answer would be yes.

There are various ways with which we can improve the performance and efficiency of the test execution. The first technique, we are going to discuss it today by making tests in parallel during execution. This is one of the important features of the TestNG framework.

This technique is basically about running tests in different threads. Here if you implement multithreading so you can set the name of the thread.

Implementation of Tests in parallel execution in Selenium

Tests in Normal Execution

Let’s look at the program below in which we have created two methods which trigger two instances of the WebDriver. This program simply opens the browser and hit URL then closes the browser.

package Test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class TestNGParallelRun {
	
	@Test
	  public void thread1() {
		
		System.out.println("thread1 method starts");
		
		  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
		  	 
	  	  WebDriver driver = new ChromeDriver();
	  	  
	  	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  
	  	  driver.get("http://www.theavinashmishra.com/");
		  
	  	  driver.manage().window().maximize();
	  	
	  	  driver.close();
			  
		  driver.quit();
		  System.out.println("thread1 method ends");
		  
		  
	  }
	
	
	
  @Test
  public void thread2() {
	  System.out.println("thread2 method starts");
	  
	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  	 
  	  WebDriver driver = new ChromeDriver();
  	  
  	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
  	  driver.get("http://www.inviul.com/");
	  
  	  driver.manage().window().maximize();
  	
  	  driver.close();
		  
	  driver.quit();
	  
	  System.out.println("thread2 method ends");
	  
  }
}

Below is the tesng.xml file which executes the TestNG class file. When we execute it normally then all the @Test annotation will be triggered sequentially in alphabetical order if priority not defined.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="2"  name="Test" >
    <classes>
      <class name="Test.TestNGParallelRun"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Here is the console output. We observe from the below output in the console that at first thread1 method got triggered then thread2 method got triggered.

Normal execution flow without parallel run

Tests in Parallel Execution

We saw the normal execution of the tests. Let’s make a change in the testng.xml file so to run tests in parallel execution we need to add the following line in the testng.xml file.

<test thread-count="Y" parallel="X" name="Test" >

Here, X = methods or classes or tests or instances
      Y = any integer

We need to set the value to the thread-count variable and also, we need to set the value to the parallel. Assigning a value to the thread-count create different threads, we just need to set the value to threads with the help of Java Multi-threading. Similarly, assigning a value to the parallel gives instruction to TestNG to decide which parameters to run in parallels like methods, tests, classes, and instances.

Here is the sample testng.xml file to run tests in parallel execution.

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

Following screenshot shows the console output which clearly displays the parallel execution of the tests.

Tests in parallel execution

We already have some of the tutorials on setting the value to the thread and parallel execution through session handling. We highly recommend you read these tutorials:

 

Join Inviul fb group

Leave a Reply