Canarys | IT Services

Blogs

Parallel Execution using Selenium Webdriver and TestNG

Share
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.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.ie.InternetExplorerDriver;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class AddingToWishList

{   

      private WebDriver driver;

       String url = "http://sites.ecanarys.com/Nopcommercesite";

       @Parameters ({"browser"})

       @BeforeTest

       public void preCondition(String browser)

       {

              try

              {

                     if(browser.equalsIgnoreCase("Firefox"))

                     {

                           driver = new FirefoxDriver();

                     }

                     if(browser.equalsIgnoreCase("Chrome"))

                     {

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

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

                           driver = new ChromeDriver();

                     }

                     if(browser.equalsIgnoreCase("IE"))

                     {

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

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

                           driver = new InternetExplorerDriver();

                     }                   

              }

              catch (WebDriverException e)

              {

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

              }

              driver.get(url);

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

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

       }

       @Test

       public void addToCart() throws InterruptedException

       {

              //Clicking on Cement link on the home page

              driver.findElement(By.partialLinkText("Cement")).click();

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

              Actions action = new Actions(driver);

              action.moveToElement(element).click().perform();

              //Clicking on "Add to Wishlist button

              driver.findElement(By.id("add-to-wishlist-button-1465")).click();

              //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 );

              driver.quit();     

     }

}

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. 

 Results

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.

Eg

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.