top of page
Writer's picturespeaktovasanthi

A Detailed Guide to Reduce Run-time Using Parallel Testing in Selenium



Contents


Introduction


Parallel testing is a process where multiple tests are executed simultaneously/in parallel in different thread processes. With respect to Selenium, it allows you to execute multiple tests on different browsers, devices, environments in parallel and at the same time, instead of running it sequentially.


With Parallel Testing, automated tests can be run in parallel or concurrently across various test environments of different device/browser/Operating System versions and their combinations. Hence more tests are run at a time which reduces the execution time by many folds.

Less execution time implies a less wait and fast build releases thus reducing the overall testing time required!


Benefits of Parallel Testing

  • Test for Wider Compatibility

  • Parallel Testing Optimizes Continuous Integration and Delivery

  • Saves time and improves productivity

  • Better coverage of tests

  • Can Radically Reduce Feedback Time

How do you perform parallel testing with TestNG?


TestNG allows running the tests in parallel by setting the “parallel” attribute in the “<suite>” tag respectively to “tests”, “methods”, “classes”, “instances”. As per the setting provided to run the tests, it will start executing the tests in separate threads.

The goal of parallel testing is to resolve the constraints of time by distributing tests across available resources. For example, if 20 test cases take a total of 100 minutes to complete, then 10 parallel executions could run 2 test cases each and bring the total testing time down to 10 minutes.


Parallel testing accepts the following keywords (values) in TestNG:

  • Methods: This will run the parallel tests on all @Test methods in TestNG.

  • Tests: All the test cases present inside the <test> tag will run with this value.

  • Classes: All the test cases present inside the classes that exist in the XML will run in parallel.

  • Instances: This value will run all the test cases parallelly inside the same instance.

Example for Parallel Testing


This example has three files created as a Maven Project.


i) ParallelScript.java

ii) Pom.xml

iii) Testng.xml


ParallelScript.java

Note: We can use either if else block or switch case to select different browsers

package BrowserTypes;
 import java.util.concurrent.TimeUnit;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.edge.EdgeDriver;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.testng.annotations.AfterTest;
 import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Parameters;
 import org.testng.annotations.Test;

 public class ParallelScript {
 WebDriver driver;
 @BeforeTest
 @Parameters("browser")
 public void setup(String browser) throws Exception{
  System.out.println("Browser Name is :" + browser);
 if(browser.equalsIgnoreCase("firefox")){
   driver = new FirefoxDriver();
  }
  else if(browser.equalsIgnoreCase("chrome")){
    driver = new ChromeDriver();
  }
  else if(browser.equalsIgnoreCase("Edge")){
   driver = new EdgeDriver();
  }
  else{
   throw new Exception("Browser is not correct");
 }

  switch(browser.toUpperCase()) {
  case "FIREFOX":
   driver=new FirefoxDriver();
   break;
  case "CHROME":
   driver = new ChromeDriver();
   break;
  case "EDGE":
   driver=new EdgeDriver();
   break;
  default:
   System.out.println("Browser is not correct");
  }
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }
 
 @Test
 public void testParameterWithXML() throws InterruptedException{
  
  driver.get("http://demo.guru99.com/V4/");
  driver.manage().window().maximize();
  WebElement userName = driver.findElement(By.name("uid"));
  userName.sendKeys("guru99");
  WebElement password = driver.findElement(By.name("password"));
  password.sendKeys("guru99");
  System.out.println("Test completed Successfully");
 }
 @AfterTest
 public void browserQuit() {
  driver.quit();

 }
 }




Pom.xml

In this file, we add all the necessary plugins and dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>VasCrossBrowser</groupId>
  <artifactId>VasCrossBrowser</artifactId>
  <version>0.0.1-SNAPSHOT</version>

<build>
 <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M6</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>          
            </suiteXmlFiles>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
</plugins>
</build>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.8.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>7.6.0</version>
  <scope>test</scope>
</dependency>

<dependency>
 <groupId>io.github.bonigarcia</groupId>
 <artifactId>webdrivermanager</artifactId>
 <version>5.3.2</version>
</dependency>
</dependencies>
</project>

TestNG.xml

Since we are using TestNG Parameters in ParallelScript.java, we need to specify the values from the TestNG.XML file that will pass to the test case file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" thread-count="3">
<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="BrowserTypes.ParallelScript">
</class>
</classes>
</test>
  <test name="FirefoxTest">
<parameter name="browser" value="Firefox" />
<classes>
<class name="BrowserTypes.ParallelScript">
</class>
</classes>
</test>
<test name="EdgeTest">
<parameter name="browser" value="Edge" />
<classes>
<class name="BrowserTypes.ParallelScript">
</class>
</classes>
</test>
</suite> <!-- Suite -->

Here because the testing.xml has three Test tags (‘ChromeTest’,’FirefoxTest’,’EdgeTest’),this test case will execute simultaneously on 3 different browsers.

In the testing.xml, we set the parallel attribute to “tests” in testing.xml, to perform parallel testing.

Note: To run the test, Right click on the testing.xml, Select Run As, and Click TestNG


Testng.xml

Test Run Time Comparison


Below is the console output if we run the testing.xml


If we check refresh the project on left panel, test-output folder is created. You can find the index.report.html file. It shows the total run time of the tests.


Below is the index report showing test run time, we get if remove ‘Parallel=”tests”’ from the testing.xml file


If you see the Results of running suite, the total time taken for parallel testing is 7186 ms whereas in normal testing,it is 12085 ms. When we run large volume of test cases, parallel execution reduces test execution time significantly.


Advantages of Parallel Testing

To summarize, parallel testing has the following advantages:

  • Reduces Time: Running the tests in parallel reduces the overall execution time.

  • Allow Multi-Threaded Tests: Using the parallel execution in TestNG, we can allow multiple threads to run simultaneously on the test case providing independence in the execution of different components of the software.

Disadvantages of Parallel Testing

At the same time, parallel testing in TestNG also has some disadvantages as below:

  • Fails On Dependent Modules: Parallel testing allows independent running of modules simultaneously. Due to this, we cannot go ahead with modules that are dependent on each other, and this occurs quite frequently while testing. So, either we run serially or remove dependence, which takes extra time and effort.

  • Knowledge Of Program Flow: The tester should have good knowledge with the flow of the program to create parallel testing modules. A slight interdependency can bring down the whole test case execution. The tester should also know which modules to run in multiple threads and which ones to run in the same threads etc.

Conclusion


Parallel testing in TestNG using Selenium helps us to deliver the projects at a faster rate in agile and continuous delivery working environment, which is challenging in its way.



Thus, you would have got an idea of Parallel Test Execution in Selenium. Happy Testing!!!




1,757 views
bottom of page