TestNG – 18 || Multiple Browser and Parallel Execution in TestNG.

Parallel execution of the script on multiple browser can be handled using the TestNG very easily and in a simple way. It means same set of test cases can be run over the more than one web browser.

Handling Multiple Web Browsers

Multiple Browser Handling

First of all, we need to understand the concept of Multiple Browser Handling. It is based on the theory that the same set of commands can be executed on multiple browsers without any changes.

The only difference would be the way to initialize the browser objects. While using Multiple browsers we need third party drivers to handle each browser. To understand the process of coding to handle multiple browser I have created a separate blog post. I would recommend before proceeding you must visit the below link:

Browser name as a Parameter

Let’s start with a simple example. Suppose we have to launch a specific browser based on the value of the Parameter. This parameter can be defined under the TestNG XML file. In this program, only one browser can be launched at one time based on the parameter.

Structure of class file would be as:

  • MultiBrowserExample.java
    • @BeforeClass
      • @Parameters(“browserName“)
      • public void launchBrowser(String targetBrowser)
    • @Test
      • public void launchURL()
    • @Test
      • public void searchKey()

MultiBrowserExample.java

package MultiBrowserExecution;
import java.awt.RenderingHints.Key;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 */
public class MultiBrowserExample 
{
	WebDriver driver;
	
	@BeforeClass
	@Parameters("browserName")
	public void launchBrowser(String targetBrowser)
	{
		if(targetBrowser.equalsIgnoreCase("chrome"))
		{
			System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
			driver=new ChromeDriver();
			System.out.println("Chrome Browser Launched...");
		}
		else if(targetBrowser.equalsIgnoreCase("ff"))
		{
			System.setProperty("webdriver.gecko.driver", "c://geckodriver.exe");
			driver=new FirefoxDriver();
			System.out.println("Firefox Browser Launched...");
		}
		else if (targetBrowser.equalsIgnoreCase("edge"))
		{
			System.setProperty("webdriver.edge.driver", "c://MicrosoftWebDriver.exe");
			driver=new EdgeDriver();
			System.out.println("Edge Browser Launched...");
		}
		else
		{
			System.out.println("Invalid Browser Name...");
		}
	}
	
	@Test
	public void launchURL()
	{
		driver.get("https://www.google.com");
		System.out.println("URL launched...");
	}
	
	@Test
	public void searchKey()
	{
		String keyword="allinoneblogs";
		driver.findElement(By.name("q")).sendKeys(keyword+Keys.ENTER);
		System.out.println("Keyword search successfully.");
	}
}

MultiBrowserXML.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="MultiBrowser">
	<test name="Test1">
	<parameter name="browserName" value="edge"></parameter>
		<classes>
		<class name="MultiBrowserExecution.MultiBrowserExample"></class>
		</classes>
	</test>
</suite>

Result Analysis

Multiple Browser Handling

Launch Multiple Browser in Sequence

  • I am not wrong if would say the below points as limitation of the above program:
    1. Only one browser can be launched in one execution
    2. Need to change the value of browserName each time in the XML File.

We can overcome these limitations, by changing only the XML file with the below code. After change will observe that all the browsers will launch sequentially in one go without changing the browserName. To achieve this we have to create 3 separate tests having different browserName.

MultiBrowserXML_Updated.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="MultiBrowser" parallel="">
	<test name="Test1">
	<parameter name="browserName" value="chrome"></parameter>
		<classes>
		<class name="MultiBrowserExecution.MultiBrowserExample"></class>
		</classes>
	</test>
	<test name="Test2">
	<parameter name="browserName" value="ff"></parameter>
		<classes>
		<class name="MultiBrowserExecution.MultiBrowserExample"></class>
		</classes>
	</test>
	<test name="Test3">
	<parameter name="browserName" value="edge"></parameter>
		<classes>
		<class name="MultiBrowserExecution.MultiBrowserExample"></class>
		</classes>
	</test>
</suite>

Result Analysis with Updated XML File

Result Analysis with Updated XML File

Parallel Execution on Multiple Browsers

Parallel execution based on the concept that all the script will start running on all the specified web browsers simultaneously. This saves time by running multiple threads for Parallel execution.

The only change in the XML file would be to add parallel=”tests” and thread-count=”3″ attributes under <suite> tag. The difference between output can be observed clearly by comparing the Console results.

MultiBrowserParallelXML.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="MultiBrowser" parallel="tests" thread-count="3">
	<test name="Test1">
	<parameter name="browserName" value="chrome"></parameter>
		<classes>
		<class name="MultiBrowserExecution.MultiBrowserExample"></class>
		</classes>
	</test>
	<test name="Test2">
	<parameter name="browserName" value="ff"></parameter>
		<classes>
		<class name="MultiBrowserExecution.MultiBrowserExample"></class>
		</classes>
	</test>
	<test name="Test3">
	<parameter name="browserName" value="edge"></parameter>
		<classes>
		<class name="MultiBrowserExecution.MultiBrowserExample"></class>
		</classes>
	</test>
</suite>

Result Analysis with Parallel Execution

Result Analysis with Parallel Execution

TestNG:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


652total visits,1visits today

Leave a Reply

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