Parallel Execution using Selenium Webdriver and TestNG - Blogs
X
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.

 

Related

SAP Intelligent RPA 2.0: End to End working scenario of Supplier Invoice Status Checks Bot. 

Introduction: These days for checking the Invoice and Payment status, most ...

Read More >

What is Defect? Defect Life Cycle in Software Testing.

Defect is an unexpected behavior of the software application flow against the requirement specificat...

Read More >

What are Frames? How to handle frames in Selenium WebDriver with C#?

IFrame (FullForm: Inline Frame) is an HTML document that is included in another HTML document and is...

Read More >

What is Synchronization? Handling Synchronization in Selenium WebDriver using C#:

Synchronization meaning: when two or more components involved to perform any action, we expect these...

Read More >

Sending Test reports by Email using Office 365, Gmail

Wouldn’t it be great if Test Report are sent automatically across team as soon the Test Execut...

Read More >

Extent Reports in Selenium CSharp (C#)

Reports play a fundamental role when it comes to TESTING. Tester can now  know the real-time r...

Read More >

How to Set Up Selenium WebDriver in Visual Studio Enterprise 2015?

Pre-requisite : Visual Studio Enterprise 2015 application should be installed into the system.Create...

Read More >

Assertions in Coded UI

Let us discuss how to add assertions in coded UI. Assertions are checkpoints/benchmarks to UI c...

Read More >

Analysis of Load Test Results

Analysis of Performance DataAfter you capture and consolidate your results, analyze the captured dat...

Read More >

Share

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Text/HTML
Contact Us
  • *
  • *