What is Parallel Execution in Selenium? – Devstringx

Back to Blog
Parallel Execution

What is Parallel Execution in Selenium? – Devstringx

In parallel testNG or parallel execution, we can run numerous tests at the same time in different threads so that it can reduce the execution time.

Where test cases are run on numerous browsers or operating systems. Basically, parallel execution helps us to reduce the time and effort of the testing.

It is most commonly used with Selenium, because of the increase in demand for Cross Browser Testing and we can run Independent test cases only.

With TestNG, we can run test cases in parallel before we have to run test cases sequentially which is time-consuming.

TestNG provides multiple ways for the execution of our tests in separate threads simply by using a parallel attribute in the testng.xml file in the system.

Parallel Tests, Classes, and Methods

Use parallel attributes on the <suite> tag respectively (for methods, tests, classes, instances).

By using parallel attributes we can run methods, tests, and classes in parallel.

The syntax is

<suite name="My suite" parallel="methods" thread-count="5">

<suite name="My suite" parallel="tests" thread-count="5">

<suite name="My suite" parallel="classes" thread-count="5">

<suite name="My suite" parallel="instances" thread-count="5">

How to Create Parallel Suite?

Below I have created Two test classes and created a parallel suite.

Code-

package testing;import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;

public class AmazonTest1 {

WebDriver driver;

@BeforeClass

public void lounchbrowser() {

WebDriverManager.chromedriver().setup();

driver = new ChromeDriver();

driver.manage().window().maximize();

driver.get("https://www.amazon.in/");

}

@Test

public void test() {

String title= driver.getTitle();

System.out.println(title);

}

@AfterTest

public void testEnd() {

driver.close();

}

}
package TestNG;import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;

public class AmazonTest2 {

WebDriver driver;

@BeforeClass

public void lounchbrowser() {

WebDriverManager.chromedriver().setup();

driver = new ChromeDriver();

driver.manage().window().maximize();

driver.get("https://www.amazon.in/");

}

@Test

public void test() {

String currentUrl= driver. getCurrentUrl();

System.out.println(currentUrl);

}

@AfterTest

public void testEnd() {

driver.close();

}

}

XML File:

<?XML version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Suite" verbose="1" parallel="classes" thread-count="2"><test name="Parallel Test 1"><classes<class name="AmazonTest1" /><class name="AmazonTest2" /></classes>

</test>

</suite>

FAQs

  • What is parallel testing in selenium?

To shorten test times, parallel testing involves testing many applications or application components simultaneously. Two or more parts of a parallel test examine distinct components or characteristics of an application. These components run concurrently on various computers.

  • What is parallel execution in Selenium?

Helps run methods in different threads. Tests: Assist in running every method with the same tag on the same thread. Classes: Assist in running all of a class’s methods in a single thread. Helps run all methods in the same instance and thread using instances.

  • Can we do parallel testing in Selenium?

Selenium makes extensive use of parallel testing due to the current market’s relevance of cross-browser testing. If we have a large number of browsers, each with a distinct version, we can simply establish a browser matrix and do the tests concurrently, saving us a tremendous amount of time and resources.

  • How to execute Selenium WebDriver test cases parallel in multiple browsers?

Selenium WebDriver and TestNG framework can be combined to run test cases on various browsers simultaneously on the same computer. Because there are two Test tags in this instance (‘ChromeTest’ and ‘FirefoxTest’), this test case will run twice, once for each of the two browsers.

  • How may TestNG be used for parallel execution?

Setting the parallel property is necessary to start TestNG’s parallel test execution, which runs tests on different threads. This attribute can take one of four values: runs all methods marked with the @Test annotation in parallel. tests – parallelizes the execution of all test cases found in the XML’s test> tag.

If you are interested in even more software testing-related articles and information from us here at Devstringx, then we have a lot to choose from for you.

Share this post

Back to Blog