How To Perform Parallel Test Execution In TestNG With Selenium
Tags: testng, selenium, automation, java
Published: 2022-04-21 14:32:18

The evolution in the process of software delivery in organizations in response to business agility has resulted in a paradigm shift from traditional release cycles to continuous release models. To achieve the product delivery objectives in this new paradigm, [continuous testing](https://www.lambdatest.com/blog/continuous-testing-using-shift-left-testing-approach/?utm_source=devto&utm_medium=organic&utm_campaign=apr21_rd&utm_term=rd&utm_content=blog) plays a vital role in ensuring the quality of end-to-end processes, along with ensuring effective collaboration between Quality Assurance (QA) and development teams.

The new era of continuous testing demands QA teams to explore solutions to increase test execution speed. Enterprises are looking to leverage the benefits offered by automation testing while ensuring that manual testing is used in scenarios where automation is not possible.

The traditional approach of manual testing is already taken over by automation testing. One popular automation testing strategy widely adopted by QA teams to further reduce the testing time while ensuring maximum test coverage is the shift from sequential testing to [parallel testing](https://www.lambdatest.com/blog/what-is-parallel-testing-and-why-to-adopt-it/?utm_source=devto&utm_medium=organic&utm_campaign=apr21_rd&utm_term=rd&utm_content=blog). You can perform parallel test execution in TestNG or any other framework of your choice with Selenium.

![](https://cdn-images-1.medium.com/max/2000/1*r9Py1hJ9r9tkdgeo9osJlw.gif)

In this [Selenium TestNG](https://www.lambdatest.com/blog/create-testng-project-in-eclipse-run-selenium-test-script/?utm_source=devto&utm_medium=organic&utm_campaign=apr21_rd&utm_term=rd&utm_content=blog) tutorial, we deep dive into the integral aspects of parallel testing in TestNG.

Let’s get started!

## Introduction to Parallel Test Execution in TestNG

Parallel testing is an approach in which independent tests are run parallel to reduce overall test execution time. A tester runs two or more versions of the software concurrently with the same input or test methods. Each test can be run on multiple devices, browsers, and OS combinations.

The parallel testing process can be applied to a single application, multiple applications, or even multiple components of an application. Parallel execution in TestNG can be achieved at different levels — threads, methods, etc.

Parallel testing was introduced to replace the traditional approach of sequential testing. As the name suggests, in sequential test execution tests are executed one after another, which is more time-consuming.

![](https://cdn-images-1.medium.com/max/2000/1*IdvudChXslX7lJV-yrVUug.jpeg)

Because of the growing demand for cross-browser testing in today’s market, parallel testing is implemented heavily with Selenium automation testing.

## Advantages of Parallel Test Execution in TestNG

Parallel testing is a powerful approach that allows you to reduce the time taken to perform [automation testing](https://www.lambdatest.com/learning-hub/automation-testing?utm_source=devto&utm_medium=organic&utm_campaign=apr21_rd&utm_term=rd&utm_content=learning_hub). There are various reasons that make it important for testers to adopt parallel testing in Selenium. Some of the most popular reasons are:

* **Accelerated Test Execution Speed**

If a sequential test takes 1 minute to complete a single test execution, it will take 5 minutes to run five tests one after another on the same device. However, overall test execution will take 1 minute (or less) when the same test is run in parallel on 5 devices (or input combinations).

* **Increased Test Coverage and Wider Compatibility**

Parallel testing enables you to test across more devices (desktop or mobile), browsers, and OS combinations at once. This extended test coverage will help in risk mitigation, reducing the chance of unnoticed bugs and offering wider compatibility, which will be instrumental in shipping a top-quality product.

* **Overcome the Testing Bottleneck**

Testing is often the bottleneck of DevOps. Tedious testing cycles can hold up the entire release process. But this bottleneck can be removed by accelerated test execution through parallel testing.

* **Fix Bugs Earlier**

Earlier the testing is completed, earlier the bugs can be unearthed and fixed. In addition, parallel testing is results-driven, since we can test more scenarios in less time, and there is more actionable data towards enhancements.

* **Flexibility**

You can revert to sequential testing when necessary.

## Challenges of Parallel Test Execution in TestNG

To achieve full-fledged parallel testing, as with any type of testing, you may face some challenges. The common challenges that you will run into are often related to:

* Scalability

* Infrastructure limitations. This includes:

 1. Availability of secure, enterprise-grade lab that meet all the requirements

2. Recurring investments involved in the maintenance and upgradation of test infrastructure.

The popular strategy recommended to overcome these challenges and move towards parallel testing is to use cloud-based testing providers. Through [Cloud testing](https://www.lambdatest.com/blog/cloud-testing-tutorial/), teams can test their apps on devices and browsers hosted on the cloud infrastructure. By utilizing a cloud-based testing infrastructure, you no longer need to maintain in-house infrastructure.

Also, the scalability issues associated with the local testing infrastructure for covering all major browsers, their different versions, and OS will be taken care of. Needless to say, it is more cost-efficient as well.

Cloud-based cross browser testing platforms like LambdaTest offer a cloud Selenium Grid with access to 3000+ real browsers and devices for your testing needs. You can choose a specific OS-device-browser combination as per your requirement. The platform also aids you in performing parallel testing in Selenium, which can drastically scale your automation test and reduce test execution time through easy setup and minimal configurations.

## How to perform Parallel Test Execution in TestNG?

TestNG (where NG stands for ‘Next Generation’) is an open-source Java-based test automation framework that has gained huge popularity among developers and testers. The framework itself is inspired by JUnit, another popular unit testing framework in the Java ecosystem. However, it offers a variety of advanced and distinctive features, making it much more powerful than its peers. One such remarkable feature is the seamless support for Parallel Testing.

The TestNG [test automation](https://www.lambdatest.com/automation-testing) framework allows you to run tests in parallel or multithreaded mode by utilizing the Multi-Threading concept of Java. Multi-Threading is the process of executing multiple threads simultaneously, without any dependence on each other. Therefore, exceptions occurring on one thread won’t affect the others. In TestNG, we enable parallel testing by making required changes in the configuration file — TestNG XML file.

With TestNG certification, you can challenge your skills in performing automated testing with TestNG and take your career to the next level.

Here’s a short glimpse of the TestNG certification from LambdaTest:
{%youtube dzXX2hJhuCY%}
However, you can follow the [LambdaTest YouTube Channel](https://www.youtube.com/c/Lambdatest?sub_confirmation=1) and stay updated with the latest certifications and tutorials around Selenium testing, [Cypress testing](https://www.lambdatest.com/cypress-testing), and more.

## Parallel Test Execution in Selenium using Configuring TestNG XML in TestNG

TestNG XML is the test suite configuration file in TestNG, which helps you organize your tests by defining test suites, grouping test cases in TestNG, and running them under one XML file. IN a nutshell, it helps in customizing the execution of tests.

It also allows you to run a single test file over numerous combinations of parameters and values specified in it. First, let us go through the key features of the TestNG XML file, which will be useful for this tutorial.

* Provides seamless parallel test execution

* Supports [parameterization in TestNG](https://www.lambdatest.com/blog/parameterization-in-testng-dataprovider-and-testng-xml-examples/?utm_source=medium&utm_medium=organic&utm_campaign=apr21_rd&utm_term=rd&utm_content=blog) using @Parameter annotation

* Allows grouping of test methods and group execution

* Allows executing test cases inside multiple Java class files

* TestNG listeners automatically create logs

A simple TestNG XML file without any configuration for the parallel tests will look like this.

![](https://cdn-images-1.medium.com/max/2000/1*zAePnednW9YfAhEuOgvdig.png)

To trigger parallel test execution in TestNG, i.e., run tests on separate threads, we need to set the parallel attribute. This attribute accepts four values:

* **methods **— runs all methods with @Test annotation in parallel mode

* **tests** — runs all test cases present inside <test> tag in the XML in parallel mode

* **classes **— runs all test cases present inside classes in the XML in parallel mode

* **instances **— runs all test cases in the same instance in parallel mode

We can also set the number of threads we wish to allocate during the execution using the thread-count attribute. TestNG has a default value of thread = 5.

    <suite name="Regression Test Suite" parallel="methods" thread-count="2">

Here, we have defined parallel test execution at suite level, and the @Test methods will start running simultaneously. The thread count is set to be 2, which means that only two threads will be created for this execution, on which 2 test methods will start running parallelly. If there are more than 2 methods, all the remaining ones need to wait until any of the threads get free.

Now that we know how to configure TestNG for running tests simultaneously, it’s time to do the hands-on! In the upcoming section of this Selenium TestNG tutorial, we will learn how to create a TestNG framework setup and write tests and execute them parallelly through the online Selenium Grid.

## Parallel Test Execution in multiple browsers using TestNG for Selenium automation testing

In this section, we will be writing test cases in the TestNG framework and learning how to execute them parallelly at tests, classes, and methods levels by configuring the TestNG XML file. Our test cases will be executed on 5 different browsers — Chrome, Firefox, Safari, IE, and Opera by leveraging the online Selenium Grid provided by LambdaTest. Also, a custom configuration will be done for the browser as well as the OS version. Using LambdaTest, you can now test on Safari browser online on both desktop and mobile browsers for cross browser compatibility.

If you are already working with TestNG, feel free to skip the installation and project set-up part, which we will be covering next.

## Installation and Setup

Before getting started with the project setup, make sure that your system is equipped with the following prerequisites:

 1. **Java
**TestNG requires JDK 8 or higher. If you do not have Java installed in your system, you can [download Java](https://www.oracle.com/java/technologies/downloads/) from the Oracle official website. Don’t forget to set the environment variables as well.

 2. **Eclipse IDE
**In this tutorial, for the demo, we will be using Eclipse as IDE. If not installed already, you can [download Eclipse](https://www.eclipse.org/downloads/) from the official Eclipse website.

 3. **Selenium Webdriver and Client for Java
**You can download by visiting the Selenium WebDriver [official website](https://www.selenium.dev/downloads/). Selenium WebDriver is an open-source, free Selenium testing tool for browser automation.

Once this list is checked, the next step is to [install TestNG in Eclipse](https://www.lambdatest.com/blog/how-to-install-testng-in-eclipse-step-by-step-guide/). First, go to Eclipse Marketplace and install TestNG. Once installed, you can locate it under the Installed section.

![](https://cdn-images-1.medium.com/max/2000/1*QXzwkFhqu5aFsif9uVohvA.jpeg)

You have now completed the installation. Let us do the basic project setup now.

Note: [ClassList](https://www.lambdatest.com/web-technologies/classlist?utm_source=devto&utm_medium=organic&utm_campaign=apr21_rd&utm_term=rd&utm_content=web_technologies) provides a way to manipulate classes using the DOMTokenList object.

## Creating a TestNG Project

To use TestNG in a Java project, you must know how to create a TestNG project. You can follow the below-mentioned steps to create a new Java project in Eclipse.

**Step 1**: Launch Eclipse and create a new Java project by navigating to **File > New > Java Project**.

![](https://cdn-images-1.medium.com/max/2000/1*A0c2aAs3MXlaKywXlmC-gQ.png)

**Step 2**: Name your project and click Next.

![](https://cdn-images-1.medium.com/max/2000/1*ukq-JRxrAU3Mjl0IuUIUsw.png)

**Step 3**: Add libraries by following the below-mentioned steps:

 1. Right-click on the project folder and select Properties.

 2. Choose Java Build Path.

 3. Go to Libraries, select Classpath, and click Add Library.

![](https://cdn-images-1.medium.com/max/2000/1*O-7VsSmGSQMMqokgagD7FQ.png)

**Step 4**: Select TestNG and click Next.

![](https://cdn-images-1.medium.com/max/2000/1*BdsO2Fcot3XzNJdKFueM9g.png)

**Step 5**: Now, you will see that TestNG is added to the project library.

![](https://cdn-images-1.medium.com/max/2120/1*PsDIjRnlE2iCaB38e_UrkA.jpeg)

**Step 6**: Click Finish, and we have successfully created a TestNG project.

![](https://cdn-images-1.medium.com/max/2000/1*8SeFI1c5loOefS0LZ29ePw.png)

**Step 7**: Add Selenium API JAR files to the TestNG project by following the below-mentioned steps:

 1. Right-click on the project and select Properties.

 2. Select Java Build Path.

 3. Under the libraries section, click on Add External JARs, which gets enabled upon opting for Classpath.

 4. Select the option and upload the JARs for Selenium Java language bindings by navigating to the path where you have downloaded and saved it.

![](https://cdn-images-1.medium.com/max/2712/1*3XemaQjv9vlr3_t9kvLyJg.png)

![](https://cdn-images-1.medium.com/max/2000/1*lqyTA9VqPXYMnKRJ3fbQLQ.png)

5. Once done, select Apply and Close.

![](https://cdn-images-1.medium.com/max/2748/1*N4aUXQO0BXrQhE2rAo0OBg.jpeg)

With this, our project set-up is done, and we are good to start coding. Notice that the libraries are also visible in the left panel.

![](https://cdn-images-1.medium.com/max/2000/1*SljHI1EqOW8BTNQg4LaPgA.png)

Note: The [Canvas](https://www.lambdatest.com/web-technologies/canvas?utm_source=devto&utm_medium=organic&utm_campaign=apr21_rd&utm_term=rd&utm_content=web_technologies) API provides a way to generate fast, dynamic graphics using JavaScript.

## How to run test cases in parallel with Selenium in TestNG on Cloud Grid

In this section, we will learn how to achieve parallel test execution in TestNG by running the test cases on separate threads. Follow the below mentioned steps:

**Step 1**: Launch Eclipse and create a new Java project for TestNG. I have named it ParallelTestNG. You can skip this step if you have followed along with me in the Project set-up part.

**Step 2**: Create a TestNG class file by following the below-mentioned steps:

 1. Right-click on project src and find TestNG towards the bottom of the list.

 2. Select Create TestNG Class.

![](https://cdn-images-1.medium.com/max/2000/1*Ksws22i4xusRUE2BoXhOBQ.png)

3. Name the class, choose the required annotations and click Finish. Here, I have named the class as TestCasesParallelExecution.

![](https://cdn-images-1.medium.com/max/2000/1*XAcnDhekOnvbL_ptPwRY-w.png)

Let us quickly go through the TestNG annotations that have been used:

* **@Test** — Implies that the method is a test case. Therefore, all tests should be marked with this annotation.

* **@BeforeMethod** and **@AfterMethod** — These annotations run once before and after each @Test method in a class.

![](https://cdn-images-1.medium.com/max/2000/1*WB33X3CBWzv_MPAxLlTO0Q.png)

**Step 3**: Write our first test case using TestNG and Selenium. Finally, it is time to get our hands dirty! In this section, we will be writing our first test case using TestNG. For this demo, I will be writing an automation test for a simple login scenario. Following are the test steps:

 1. Launch the website URL ([https://opensource-demo.orangehrmlive.com/](https://opensource-demo.orangehrmlive.com/))

 2. Enter username and password.

 3. Click on the LOGIN button.

![](https://cdn-images-1.medium.com/max/3200/1*DNO_g0-xT1y7dQuMhGAr8A.png)

Like mentioned before, we will be running this test case parallelly on five different browsers through an online Selenium Grid provided by LambdaTest.

Let us write our test case inside the TestNG class we created in the previous step. Follow along with me.

1. Create a free account in LambdaTest and secure your unique username and access key.

2. Inside the ParallelTest class(the TestNG class), define all the required variables for connecting with the Remote Grid. In addition, you will need to provide your unique username, and access key.

    public class TestCasesParallelExecution {
     
    public String username = "YOUR_USERNAME";
    public String accesskey = "YOUR_ACCESSKEY";
    public RemoteWebDriver driver = null;
    public String gridURL = "[@hub](http://twitter.com/hub).lambdatest.com/wd/hub";
    boolean status = false;
    ..
    }

3. Define automation setup steps inside the method marked by @BeforeClass annotation. This method gets executed first. Notice that I have also changed the method name from beforeClass() to setUp().

Inside the setUp(), we have defined the following:

* **DesiredCapabilities** for custom configuring our browser-device-OS version, enabling reporting and logging features, and naming the build. I have generated this by utilizing the [Desired Capabilities Generator](https://www.lambdatest.com/capabilities-generator/) offered by LambdaTest.

* A New instance of **RemoteWebDriver()**

* Also, I have added **@Parameters** annotation on top of the method for receiving the browser, version, and platform values from our TestNG XML file.

    [@BeforeMethod](http://twitter.com/BeforeMethod)
    [@Parameters](http://twitter.com/Parameters)(value={"browser","version","platform"})
    public void setUp(String browser, String version, String platform) throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
         capabilities.setCapability("browserName", browser);
         capabilities.setCapability("version", version);
         capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
         capabilities.setCapability("build", "ParallelTestNG");
         capabilities.setCapability("name", "ParallelTestNG");
         capabilities.setCapability("network", true); // To enable network logs
         capabilities.setCapability("visual", true); // To enable step by step screenshot
         capabilities.setCapability("video", true); // To enable video recording
         capabilities.setCapability("console", true); // To capture console logs
         try {
             driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
         } catch (MalformedURLException e) {
             System.out.println("Invalid grid URL");
         } catch (Exception e) {
             System.out.println(e.getMessage());
         }
     }

4. Define tear-down steps inside the method marked by @AfterClass annotation. This method gets executed last. Notice that I have also changed the method name from afterClass() to tearDown().

    [@AfterMethod](http://twitter.com/AfterMethod)
        public void tearDown() throws Exception {
           if (driver != null) {
                driver.quit();
            }
        }

5. Write our actual test steps. Our test methods will be marked by the @Test annotation. Since we are performing a login scenario, I have renamed the method to login().

    [@Test](http://twitter.com/Test)
    public void login() {
           
      // To navigate to URL
      driver.get("[https://opensource-demo.orangehrmlive.com/](https://opensource-demo.orangehrmlive.com/)");
      // To locate input field for username and enter value
      driver.findElement(By.name("txtUsername")).sendKeys("Admin");
      // To locate input field for password and enter value
      driver.findElement(By.name("txtPassword")).sendKeys("admin123");
      // To locate and click on login button
       driver.findElement(By.className("button")).click();          
    }

I have made use of Chrome Developer tools for locating the elements.

![](https://cdn-images-1.medium.com/max/3200/1*MwxRa3zVsB6ryzRgm2FoIw.png)

Our final code looks like this:

    package testng;
     
    import java.net.MalformedURLException;
    import java.net.URL;
     
    import org.openqa.selenium.By;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;
     
    public class TestCasesParallelExecution {
     
    public String username = "YOUR_USERNAME";
    public String accesskey = "YOUR_ACCESSKEY";
    public RemoteWebDriver driver = null;
    public String gridURL = "@hub.lambdatest.com/wd/hub";
    boolean status = false;
     
    @BeforeMethod
    @Parameters(value={"browser","version","platform"})
    public void setUp(String browser, String version, String platform) throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
         capabilities.setCapability("browserName", browser);
         capabilities.setCapability("version", version);
         capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
         capabilities.setCapability("build", "ParallelTestNG");
         capabilities.setCapability("name", "ParallelTestNG");
         capabilities.setCapability("network", true); // To enable network logs
         capabilities.setCapability("visual", true); // To enable step by step screenshot
         capabilities.setCapability("video", true); // To enable video recording
         capabilities.setCapability("console", true); // To capture console logs
         try {
             driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
         } catch (MalformedURLException e) {
             System.out.println("Invalid grid URL");
         } catch (Exception e) {
             System.out.println(e.getMessage());
         }
     }
    @Test
    public void login() {
           
      // To navigate to URL
      driver.get("https://opensource-demo.orangehrmlive.com/");
      // To locate input field for username and enter value
      driver.findElement(By.name("txtUsername")).sendKeys("Admin");
      // To locate input field for password and enter value
      driver.findElement(By.name("txtPassword")).sendKeys("admin123");
      // To locate and click on login button
       driver.findElement(By.className("button")).click();          
    }
     
    @AfterMethod
        public void tearDown() throws Exception {
           if (driver != null) {
                driver.quit();
            }
        }
    }

**Step 4**: Configure the TestNG XML file. In this step, we will create the testng.xml file for our project and configure it to enable our tests to run in parallel mode. The steps are as below:

1. Right-click on our Project. Select New > File and name it as testng.xml.

2. Add the following boilerplate code to get started. You can name the test suite and test name as per your liking. But the class name should be exact as that of our TestNG class.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "[https://testng.org/testng-1.0.dtd](https://testng.org/testng-1.0.dtd)">
    <suite name="Parallel Test Suite">
        <test name="Test">
            <classes>
                <class name="testng.ParallelTest" />
            </classes>
        </test> <!-- Test -->
    </suite> <!-- Suite -->

3. To trigger parallel test execution in TestNG, provide the parallel attribute to <suite>. As mentioned before, we can assign any of the four values given below, as per our requirement. For parallel execution of test methods, set the attribute value as “tests.”

    <suite name="Parallel Test Suite" parallel="methods">

4. Add parameters for configuring the tests to run in a specific browser, browser version, and OS. For example, below is the code for setting up the test case to run in Chrome Browser version 79.0 in Windows 10. Likewise, you can provide as many combinations as you like.

    <test name="Chrome Test" thread-count="3">
            <parameter name="browser" value="chrome" />
            <parameter name="version" value="79.0" />
            <parameter name="platform" value="WIN10" />
            <classes>
                <class name="testng.ParallelTest" />
            </classes>
        </test>

The final code for testng.xml looks like this.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
    <suite name="Parallel Test Suite" parallel="tests" thread-count="5">
     
        <test name="Chrome Test">
            <parameter name="browser" value="chrome" />
            <parameter name="version" value="79.0" />
            <parameter name="platform" value="WIN10" />
            <classes>
                <class name="testng.ParallelTest" />
            </classes>
        </test>
     
        <test name="Firefox Test">
            <parameter name="browser" value="firefox" />
            <parameter name="version" value="62.0" />
            <parameter name="platform" value="WIN8" />
            <classes>
                <class name="testng.ParallelTest" />
            </classes>
        </test>
     
        <test name="Safari Test">
            <parameter name="browser" value="safari" />
            <parameter name="version" value="11.0" />
            <parameter name="platform" value="macos 10.13" />
            <classes>
                <class name="testng.ParallelTest" />
            </classes>
        </test>
       
            <test name="IE Test">
            <parameter name="browser" value="ie" />
            <parameter name="version" value="11.0" />
            <parameter name="platform" value="WIN10" />
            <classes>
                <class name="testng.ParallelTest" />
            </classes>
        </test>
       
                <test name="Opera Test">
            <parameter name="browser" value="opera" />
            <parameter name="version" value="73.0" />
            <parameter name="platform" value="WIN10" />
            <classes>
                <class name="testng.ParallelTest" />
            </classes>
        </test>
       
     
        <!-- Test -->
    </suite> <!-- Suite -->

We have a total of 5 different tags, and each of these will run on different threads.


5. Finally, it is time to run our test case. Right-click on our testng.xml file and navigate to Run As > TestNG Test Suite.

![](https://cdn-images-1.medium.com/max/2026/1*f11AnA__r2aXKiGiBs16pQ.png)

Our test starts getting executed. Go to your [LambdaTest Automation Dashboard](https://automation.lambdatest.com/timeline) to see the live running status.

![](https://cdn-images-1.medium.com/max/3200/1*aASAJdV6K9QbQ5Tmqq-63g.png)

The test is executed parallelly in the 5 browsers, and reports and logs are generated successfully.

![](https://cdn-images-1.medium.com/max/3200/1*4tKdU3oVq4MCRAxQbQrdWg.png)

## How to run test methods in parallel with TestNG

Now let us see how to run test methods parallelly in the TestNG. In this example, we will create one more method and execute them on different threads. The steps are as below:

**Step 1**: Create a new TestNG class file. You can also opt for an auto-defined TestNG XML file.

![](https://cdn-images-1.medium.com/max/2000/1*W9YWNuy-f4cz74eiaRXI2w.jpeg)

**Step 2**: Inside this class, I have added a new test method forgotPassword() which will:

 1. Launch the website URL ([https://opensource-demo.orangehrmlive.com/](https://opensource-demo.orangehrmlive.com/)).

 2. Click on Forgot your password? link.

 3. Enter Username.

 4. Click the Reset Password button.

![](https://cdn-images-1.medium.com/max/3200/1*PhIRMl2OquRZNV85LCkYpw.png)

    [@Test](http://twitter.com/Test)
      public void forgotPassword() {
     
        // To navigate to URL
        driver.get("[https://opensource-demo.orangehrmlive.com/](https://opensource-demo.orangehrmlive.com/)");
        // To locate and click on Forgot password link
        driver.findElement(By.linkText("Forgot your password?")).click();
        // To locate input field and enter username
        driver.findElement(By.id("securityAuthentication_userName")).sendKeys("testuser");
        // To locate and click on reset button
        driver.findElement(By.id("btnSearchValues")).click();          
      }

So the final code will look like this:

    package testng;
    import java.net.MalformedURLException;
    import java.net.URL;
     
    import org.openqa.selenium.By;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;
     
    public class TestMethodParallelExecution {
     
    public String username = "YOUR_USERNAME";
    public String accesskey = "YOUR_ACCESSKEY";
    public RemoteWebDriver driver = null;
    public String gridURL = "@hub.lambdatest.com/wd/hub";
    boolean status = false;
     
    @BeforeMethod
    @Parameters(value={"browser","version","platform"})
    public void setUp(String browser, String version, String platform) throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
         capabilities.setCapability("browserName", browser);
         capabilities.setCapability("version", version);
         capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
         capabilities.setCapability("build", "TestMethodParallelExecution");
         capabilities.setCapability("name", "TestMethodParallelExecution");
         capabilities.setCapability("network", true); // To enable network logs
         capabilities.setCapability("visual", true); // To enable step by step screenshot
         capabilities.setCapability("video", true); // To enable video recording
         capabilities.setCapability("console", true); // To capture console logs
         try {
             driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
         } catch (MalformedURLException e) {
             System.out.println("Invalid grid URL");
         } catch (Exception e) {
             System.out.println(e.getMessage());
         }
     }
     
     
    @Test
    public void login() {
           
      // To navigate to URL
      driver.get("https://opensource-demo.orangehrmlive.com/");
      // To locate input field for username and enter value
      driver.findElement(By.name("txtUsername")).sendKeys("Admin");
      // To locate input field for password and enter value
      driver.findElement(By.name("txtPassword")).sendKeys("admin123");
      // To locate and click on login button
       driver.findElement(By.className("button")).click();          
    }
     
      @Test
      public void forgotPassword() {
     
        // To navigate to URL
        driver.get("https://opensource-demo.orangehrmlive.com/");
        // To locate and click on Forgot password link
        driver.findElement(By.linkText("Forgot your password?")).click();
        // To locate input field and enter username
        driver.findElement(By.id("securityAuthentication_userName")).sendKeys("testuser");
        // To locate and click on reset button
        driver.findElement(By.id("btnSearchValues")).click();          
      }
     
    @AfterMethod
        public void tearDown() throws Exception {
           if (driver != null) {
                driver.quit();
            }
        }
    }
     
    }

**Step 3**: Configure the TestNG XML file. Set the value of the parallel attribute as ”methods”. My testng2.xml file now looks like this.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "[https://testng.org/testng-1.0.dtd](https://testng.org/testng-1.0.dtd)">
    <suite name="Parallel Test Suite" parallel="methods" thread-count="2">
     
        <test name="Chrome Test">
            <parameter name="browser" value="chrome" />
            <parameter name="version" value="79.0" />
            <parameter name="platform" value="WIN10" />
            <classes>
                <class name="testng2.TestMethodParallelExecution" />
            </classes>
        </test>
        <!-- Test -->
    </suite> <!-- Suite -->

**Step 4**: Run the tests. Our two test methods start running in parallel. You can observe the live running status in your LambdaTest dashboard.

![](https://cdn-images-1.medium.com/max/3200/1*fyX57X35RwYpOA2XQKhNtA.png)

Note: [Autofocus attribute](https://www.lambdatest.com/web-technologies/autofocus?utm_source=devto&utm_medium=organic&utm_campaign=apr21_rd&utm_term=rd&utm_content=web_technologies) — a lightweight JavaScript library that allows a form field to be immediately focused on page load.

## How to run test classes in parallel with TestNG

Lastly, let us see how to run test classes parallelly in the TestNG. First, let us create 2 classes that will be running parallel on separate threads.

1. Create 2 classes — TestClassOne and TestClassTwo. Each contains a test method.

**TestClassOne.java**

    package testng3;
     
    import org.testng.annotations.Test;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Parameters;
     
    import java.net.MalformedURLException;
    import java.net.URL;
     
    import org.openqa.selenium.By;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.AfterMethod;
     
    public class TestClassOne {
       
        public String username = "YOUR_USERNAME";
        public String accesskey = "YOUR_ACCESSKEY";
        public RemoteWebDriver driver = null;
        public String gridURL = "@hub.lambdatest.com/wd/hub";
        boolean status = false;
     
     
        @BeforeMethod
        @Parameters(value={"browser","version","platform"})
        public void setUp(String browser, String version, String platform) throws Exception {
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("browserName", browser);
            capabilities.setCapability("version", version);
            capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
            capabilities.setCapability("build", "TestMethodParallelExecution");
            capabilities.setCapability("name", "TestMethodParallelExecution");
            capabilities.setCapability("network", true); // To enable network logs
            capabilities.setCapability("visual", true); // To enable step by step screenshot
            capabilities.setCapability("video", true); // To enable video recording
            capabilities.setCapability("console", true); // To capture console logs
            try {
                driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
            } catch (MalformedURLException e) {
                System.out.println("Invalid grid URL");
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
     
        @Test
        public void login() {
     
            // To navigate to URL
            driver.get("https://opensource-demo.orangehrmlive.com/");
            // To locate input field for username and enter value
            driver.findElement(By.name("txtUsername")).sendKeys("Admin");  
            // To locate input field for password and enter value
            driver.findElement(By.name("txtPassword")).sendKeys("admin123");
            // To locate and click on login button
            driver.findElement(By.className("button")).click();            
        }
     
        @AfterMethod
        public void tearDown() throws Exception {
            if (driver != null) {
                System.out.println("Tearing down..");
                driver.quit();
            }
        }
    }

**TestClassTwo.java**

    package testng3;
     
    import org.testng.annotations.Test;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Parameters;
     
    import java.net.MalformedURLException;
    import java.net.URL;
     
    import org.openqa.selenium.By;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.AfterMethod;
     
    public class TestClassTwo {
     
        public String username = "haritagr16";
        public String accesskey = "KhRUhWBtSMg06DfGkKDY1khYNJm95695WV42HfaYPot7nqQfw6";
        public RemoteWebDriver driver = null;
        public String gridURL = "@hub.lambdatest.com/wd/hub";
        boolean status = false;
     
        @BeforeMethod
        @Parameters(value={"browser","version","platform"})
        public void setUp(String browser, String version, String platform) throws Exception {
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("browserName", browser);
            capabilities.setCapability("version", version);
            capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
            capabilities.setCapability("build", "ParallelTestNG");
            capabilities.setCapability("name", "ParallelTestNG");
            capabilities.setCapability("network", true); // To enable network logs
            capabilities.setCapability("visual", true); // To enable step by step screenshot
            capabilities.setCapability("video", true); // To enable video recording
            capabilities.setCapability("console", true); // To capture console logs
            try {
                driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
            } catch (MalformedURLException e) {
                System.out.println("Invalid grid URL");
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
       
        @Test
        public void forgotPassword() {
     
            // To navigate to URL
            driver.get("https://opensource-demo.orangehrmlive.com/");
            // To locate and click on Forgot password link
            driver.findElement(By.linkText("Forgot your password?")).click();  
            // To locate input field and enter username
            driver.findElement(By.id("securityAuthentication_userName")).sendKeys("testuser");
            // To locate and click on reset button
            driver.findElement(By.id("btnSearchValues")).click();              
        }
     
        @AfterMethod
        public void tearDown() throws Exception {
            if (driver != null) {
                System.out.println("Tearing down..");
                driver.quit();
            }
        }
    }

2. Create a new TestNG XML file and set the parallel attribute as “classes.”

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "[https://testng.org/testng-1.0.dtd](https://testng.org/testng-1.0.dtd)">
    <suite name="Parallel Test Suite" parallel="classes" thread-count="2">
     
        <test name="Chrome Test">
            <parameter name="browser" value="chrome" />
            <parameter name="version" value="79.0" />
            <parameter name="platform" value="WIN10" />
            <classes>
                <class name="testng3.TestClassOne" />
                <class name="testng3.TestClassTwo" />
            </classes>
        </test> 
        <!-- Test -->
    </suite> <!-- Suite -->

3. Run your test suite. The test gets executed successfully.

![](https://cdn-images-1.medium.com/max/3200/1*tiQGm9i2eG0gdZpTokWGtA.png)

## Conclusion

Parallel testing is widely adopted by Agile teams to radically reduce the time and cost associated with the traditional testing methods. It enables you to speed up the overall test cycle and deliver projects at a faster rate. To maximize the test coverage while ensuring scalability, the responsibility of maintaining a testing grid is often outsourced to cloud-based platforms.

In this Selenium TestNG tutorial, we learned how to achieve parallel test execution in the TestNG framework with Selenium. We have also leveraged the cloud-based Selenium Grid provided by LambdaTest to run our test cases parallelly in 5 different browsers while performing [Selenium automation testing with TestNG](https://www.lambdatest.com/selenium-automation-testing-with-testng?utm_source=devto&utm_medium=organic&utm_campaign=apr21_rd&utm_term=rd&utm_content=webpage). I hope this tutorial turned out to be beneficial to you. So, keep exploring different angles of parallel testing.

Happy Coding!