Canarys | IT Services


Parallel Execution using Selenium Webdriver and TestNG

While testing a web application it is very important to test the application on different browsers. We can achieve this by using Selenium Web driver and TestNG. If there are more number of scripts to be executed and executing them on each and every browsers sequentially is time consuming. This can be avoided using a concept in Selenium called Parallel Execution
TestNG allows us to run the test cases/scripts, test methods or tests in parallel, With Parallel Execution concept we can reduce the execution time, as tests are executed parallel on different browsers as declared in testng.xml file.
Below is a Sample test which will run on multiple browsers parallel. Below Code launches the parts2build application, navigates to the cement page and adds the mentioned cement to the wishlist.

package com.exploration.scripts;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebDriverException;


import org.openqa.selenium.firefox.FirefoxDriver;


import org.testng.annotations.BeforeTest;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class AddingToWishList


      private WebDriver driver;

       String url = "";

       @Parameters ({"browser"})


       public void preCondition(String browser)






                           driver = new FirefoxDriver();




                           //Location of the chromedriver.exe file stored in your machine

                            System.setProperty("","D:/ exefiles/chromedriver.exe");

                           driver = new ChromeDriver();




                           //Location of the IEDriverServer.exe file stored in your machine               

System.setProperty("","D:/ IEDriverServer.exe");

                           driver = new InternetExplorerDriver();



              catch (WebDriverException e)


                     System.out.println("Browser not found" +e.getMessage());




              driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);



       public void addToCart() throws InterruptedException


              //Clicking on Cement link on the home page


              WebElement ele = driver.findElement(By.cssSelector("a[href='/Nopcommercesite/maha-shakthi-ppc']"));

              Actions action = new Actions(driver);


              //Clicking on "Add to Wishlist button


              //Clicking on the wishlist button on the confirmation pop up

              driver.findElement(By.xpath("//input[@class='button-1 productAddedToCartWindowCheckout']")).click();

              //Getting the count on the wishlist field

              String whishlistCount = driver.findElement(By.xpath("//span[@class='wishlist-qty']")).getText();

              //Printing the Count on the Whishlist field

              System.out.println("Whishlist count: " +whishlistCount );




In the above code we have dataprovider annotation of testNG where we need to pass the type of browser from the testng file. In testng xml file, we will have to define three tags to run each test with different browsers.

Below is the code in the testng xml file. In this example we have defined three tests for three different browsers. i.e Firefox, Google Chrome and IE browsers.

Browser value which is sent in the testng xml file is compared with the parameter value in the precondition method and based on the value respective browser’s instance will be created and the code will be executed parallel.

Run the testng.xml file and you should be able to see parts2build application launched on all the defined browsers i.e Firefox, Chrome and IE and adding the item to the whishlist. 


Executing Selenium scripts sequentially

We can also use testng to execute the code one by one, by defining “parallel attribute to none”. This change has to be made on Suite tag of testng.xml file.


By doing the above mentioned change in the testng.xml file, execution happens sequentially on different browsers one after the another.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.