August 31, 2020 Jonny Steiner

Selenium Parallel Execution for Dummies

Share this knowledge!

Selenium is an open-source test automation tool that has become quite popular in testing circles in recent times. Selenium offers a suite of tools that enables organizations to integrate test automation into the CI/CD pipeline and run Selenium parallel execution on various devices and browsers running on different platforms.

We know that many of our regular readers are familiar with a lot of these concepts. The post is meant as a primer for beginners as well as a refresher for anyone used to working with Selenium but hasn’t for any number of reasons.

The tool is more popular because it supports all major programming languages such as Java, JavaScript, C#, Ruby, Python, or Perl. So, you can write code in your existing language and easily run it using Selenium. Moreover, it supports all major browsers such as Chrome, Edge, Opera, Internet Explorer, Firefox, and Safari. which means you can use a single code script and comfortably perform cross-browser testing instead of writing different scripts for each browser.

Not only is Selenium open-source and freely accessible, but it is also highly portable. It means you can run it on all major operating systems such as Linux, MAC, Windows, and UNIX. Selenium allows you to integrate it with a myriad of frameworks to reuse the code or create customized reports. You can integrate it with Jenkins CI/CD tool or add TestNG to run reports. Selenium is easy to install and use. The simple interface allows you to easily manage testing environments. The large and vibrant community is always there to help you.

Parallel Test Execution with Selenium

In the pre-DevOps and pre-Agile era wherein monolithic architecture dominated software development methods, sequential testing served a good purpose. However, the advent of DevOps and agile practices brought continuous integration and continuous testing into the picture. Today, continuous testing and scalability are key necessities and sequential testing fails to meet these rapidly changing development requirements. Selenium parallel execution enables organizations to simultaneously run tests in parallel on different devices and browsers to significantly shrink testing times.

Parallel testing is a test automation method wherein test cases are simultaneously run on multiple combinations of browsers, operating systems, and devices. Virtual devices are generally used for parallel test execution as they enable you to simulate hundreds of testing environments. Sequential testing is time-consuming. However, with parallel testing, you write one script and run it multiple times. Once the test scripts are ready, the testing job becomes easy.

Testing this way brings speed and faster time to market. In addition, it is cost-effective as you can use a cloud testing environment and pay as you go. It gives better coverage as you can try it on different devices, browsers, and platforms. Selenium parallel execution optimizes the CI/CD processes as well.

It is also essential because of the growing digital platform segmentation. While desktop systems dominated the computer world, the mobile platform is rapidly taking over. When it comes to desktop OS, Windows is the leader but in the mobile OS segment, Android stands tall. With multiple devices, OS platforms, and browsers, developers face a tough challenge of dynamically adjusting screen sizes, resolutions, and screen elements. Moreover, CSS behaves differently on each browser. While responsive design solves some of the developer challenges, it transfers these challenges to the testing segment. Today, QA teams have to run scripts across all environments and scenarios. Cross-browser testing is a key requirement. To save time and efforts, QA teams have to perform parallel execution of tests and Selenium parallel testing comes as a great option in this regard.

TestNG is a test automation framework that allows you to efficiently organize your tests and maintain readability as well. With its support for parallel testing, TestNG makes it easy to perform parallel test execution. Using the auto-defined XML file, you can specify parallel attributes to classes, tests, and methods. Using the Java multi-thread feature, you can define the number of threads for parallel testing.

Here is a step by step tutorial about running Selenium parallel testing.

  1. Java installation and configure environment variables
  2. Selenium installation
  3. Eclipse IDE installation
  4. TestNG installation
  5. Run test scripts in Eclipse

Step 1: Install Java

Selenium software comes as a jar file. As such, you need to install Java before using Selenium. Visit the Oracle website to download Java.

https://www.oracle.com/java/technologies/javase-downloads.html 

selenium parallel execution - java

  1. Choose the right version for your operating system. For instance, Windows x64 installer.
  2. Accept the license agreement and download the file.
  3. After the file gets downloaded, double-click the file to begin the installation procedure.
  4. Choose the destination folder and click next.
  5. The installation process begins.
  6. After the installation is completed, click finish.

Configure Java Environment Variables

To configure Java environment variables:

  1. Right-click on the Windows Start button.
  2. Choose System and go to Advanced System Settings.
  3. Click on the System tab and then click on environmental variables.
  4. Go to the system variable tab and set the path of Java folder.
  5. To check if Java is correctly installed, open command prompt and type the following command.
    • Java –version

java version selenium parallel execution

It displayed the java version and the build details. So, Java is now installed on the machine.

Step 2: Selenium Installation

The next step is to download Selenium. To do so, visit the Selenium website here:

https://www.Selenium.dev/downloads/

Selenium Webdriver

The latest stable version is 3.141.59. It comes as a jar file. Download the file and save it in a separate folder.

Step 3: Eclipse Installation

To install the Eclipse IDE, go to the following link and download the eclipse tool.

https://www.eclipse.org/downloads/

Selenium Parallel Execution Eclipse

  1. Choose the mirror site that is closer to your location and download the software. Double-click the file to begin the installation procedure.
  2. When the Eclipse installer begins, choose the Eclipse IDE for Developers.
  3. Choose the eclipse folder and click installation
  4. When the installation is completed, click on Launch
  5. When you launch the tool for the first time, it will prompt you for the workspace folder. The eclipse welcome screen looks like this.
  6. Now, the Eclipse IDE is successfully installed.

Step 4: TestNG Installation

  1. To install TestNG, open the Eclipse IDE and click on Help
  2. In the Eclipse Marketplace search bar, type ‘testng’ and click go. It will list the TestNG plugin. Click install and TestNG gets installed.
  3. However, the new version of Eclipse doesn’t offer TestNG in the marketplace. In such a case, you can directly download it from the site here: http://dl.bintray.com/testng-team/testng-eclipse-release/
  4. Click on the zipped directory
  5. Download the latest version from the next page.
  6. Now open Eclipse and click on Help and go to Install new software.
  7. Click on Add.
  8. On the next screen, go to Archive.
  9. Select the zipped folder and click ‘Add’. In the next screen, select TestNG and click next.
  10. Accept the license agreement on the next screen and click next.
  11. After the installation, click finish, and the eclipse gets restarted.

Step 5: Parallel Execution of Test Scripts

Open Eclipse, and click on file-> create a new java project. Eg: ExperitestParallelTesting.

Selenium Parallel Execution

selenium parallel execution

Click on the Libraries tab and click on Add Library.

Parallel Test Execution

Choose TestNG and click next and click finish.

Parallel Test Execution

TestNG is now ready for Selenium parallel testing.

Similarly, click on ‘Add External JARs’ and select the Selenium Server JAR file. It will add Selenium to your project.

Selenium Parallel Execution

Now, Go to package explorer view in Eclipse and right-click on your project and click on src and choose new -> Package and give a name to the package. Eg: ExperitestParallelTesting.

Selenium Parallel Execution

Click New -> Class and create a new class. Eg: Experitestparalleltest.java

Now, create a simple program that will open a browser and open Google site and search for a keyword. Create another test case with the same code but with another browser.

Here is the code:

package ExperitestParallelTesting;

import org.openqa.selenium.*;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;
public class Experitestparalleltest1 {
	
private static final String ACCESS_KEY = "Enter your access key here.";

private URL url;


@BeforeTest

public void Setup() throws MalformedURLException
{
	 url = new URL("https://cloud.seetest.io/wd/hub");
}

@Test
public void ExperitestGoogleSearch() {

	RemoteWebDriver driver;
	DesiredCapabilities dc = new DesiredCapabilities();
    dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
    
    dc.setCapability(CapabilityType.PLATFORM, Platform.ANY);
    dc.setCapability("accessKey", ACCESS_KEY);
    dc.setCapability("testName", "Quick Start Firefox Browser Demo");
    driver = new RemoteWebDriver(url, dc);

    driver.get("https://www.google.com");
    new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.name("q")));
    WebElement searchBar = driver.findElement(By.name("q"));
    searchBar.click();
    searchBar.sendKeys("Experitest");
    searchBar.sendKeys(Keys.ENTER);
    System.out.print("Experitest TestCase is running in Thread  " + Thread.currentThread().getId());
    driver.quit();
}

@Test
public void SeeTestGoogleSearch() {

	RemoteWebDriver driver1;
	DesiredCapabilities dc1 = new DesiredCapabilities();
    dc1.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
    
    dc1.setCapability(CapabilityType.PLATFORM, Platform.ANY);
    dc1.setCapability("accessKey", ACCESS_KEY);
    dc1.setCapability("testName", "Quick Start Chrome Browser Demo");
    driver1 = new RemoteWebDriver(url, dc1);

    driver1.get("https://www.google.com");
    new WebDriverWait(driver1, 10).until(ExpectedConditions.presenceOfElementLocated(By.name("q")));
    WebElement searchBar1 = driver1.findElement(By.name("q"));
    searchBar1.click();
    searchBar1.sendKeys("SeeTest");
    searchBar1.sendKeys(Keys.ENTER);
    System.out.print("SeeTest TestCase is running in Thread  " + Thread.currentThread().getId());
    driver1.quit();
}
}

Here, we have written two test methods. One method opens the Google Chrome browser from the SeeTest Cloud and searches for the term ‘Experitest’. The other method opens the Firefox browser from the SeeTest Cloud and searches for the term ‘SeeTest’. The output shows that these 2 tests have passed.

selenium parallel execution

However, the tests were executed serially. TestNG offers an easy way to execute them in parallel. It allows you to specify how you want to run the test in an XML file. To do so, create a TestNG XML file.

Right-click on the project and click New -> File.

selenium Parallel Execution

Give a name with an XML extension.

selenium parallel execution

Alternatively, you can right-click on the project and click on TestNG and choose ‘Convert to TestNG.

selenium parallel execution

On the next screen, choose the name of the XML file, suite name, etc.

selenium parallel execution

Under the Parallel mode option, you can see methods, classes, tests, etc. You can specify whether parallel execution should run at the method level, test level, or class level. You can choose the thread count as well. Click Finish to create the testng.xml file.

Now, run the testng.xml file.

 

Selenium Parallel Execution

 

Notice the starting time of the two test cases. Both tests started simultaneously and both tests are running in different threads (14 and 15).

The Thread.CurrentThread().getID() function will give you the details of the thread in which the test case is running. This is how you can perform Selenium parallel execution.

Summary

As technology keeps rapidly innovating, new technologies are being introduced into the market every day. With new device models, browsers, platforms, and changing web development requirements, organizations face a tough challenge of running full test suites in a myriad of environments. Selenium parallel testing offers an inexpensive and easy way to simultaneously run test scripts on multiple browsers and devices.

However, Selenium’s parallel execution comes with its own challenges. To perform mobile testing, you need to integrate it with tools such as Appium. Similarly, you need a Reporting and analytics tool. So, setting up the test automation environment with multiple tools and frameworks is a tough task. As the number of nodes increase, the speed and performance get reduced.

SeeTest is a highly scalable test automation framework that enables QA teams to fully leverage Selenium’s parallel execution capabilities. Real-time testing with devices located across the globe offers a wide range of test coverage. Moreover, you can monitor and manage it from a central dashboard while being able to report customized reports. With a dedicated support team, SeeTest makes parallel testing easy and efficient.

Guy ArieliCTO

Share this knowledge!