Execute webdriver tests in Parallel using selenium Grid

In previous article we have seen configuring selenium grid and execute a simple test on firefox browser. In this tutorial we will see 'Parallel execution of tests' using selenium grid and execute tests on firefox and chrome browser.

We will register multiple nodes to the Hub and execute tests in parallel. In the below example we will register a node in the same local machine where hub is running and other node in remote machine.

We will register node 1 in local machine with the below command:

java -jar selenium-server-standalone-2.48.2.jar -role node -hub http://localhost:4444/grid/register

We will register node 2 with the below command in remote machine. In the below command, need to pass a parameter by passing JVM properties using the -D flag along with chrome driver path , so that when ever there is a request to execute in chrome driver, Hub will send the request to this node. And here we have to mention the IP address of the machine where the hub is running as we are starting this node in remote machine.


java -jar selenium-server-standalone-2.48.2.jar -role node -hub http://10.0.0.6:4444/grid/register -Dwebdriver.chrome.driver=.\chromedriver.exe

After executing the above command, console should look like below:

grid starting in chrome

When a Hub receives request to execute test in Chrome browser and If we don't specify chrome driver path for node, it will throw an exception as "Exception: The path to the chromedriver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list".

Now when these two nodes are registered to the hub, hub console should look like below :

grid with multiple nodes

And the Grid console should look like below which shows configuration details of the nodes with IP address. In the below image below image Node 1 is running in Windows * machine (WIN8_1) and Node 2 is running in Windows7 machine.

grid node machine configuration

Let us now take the example and execute tests in parallel.

In order to do this, we will create two classes which has multiple @Test methods in it. And a Browser class which invokes the remote webdriver based on the browser parameter that we pass in testng.xml file

First create a class as 'Browser.java'

package com.test;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Browser {
	
	public static RemoteWebDriver getDriver(String browser) throws MalformedURLException {
		return new RemoteWebDriver(new URL("http://10.0.0.6:4444/wd/hub"), getBrowserCapabilities(browser));
	}
	
	private static DesiredCapabilities getBrowserCapabilities(String browserType) {
		switch (browserType) {
		case "firefox":
			System.out.println("Opening firefox driver");
			return DesiredCapabilities.firefox();
		case "chrome":
			System.out.println("Opening chrome driver");
			return DesiredCapabilities.chrome();
		case "IE":
			System.out.println("Opening IE driver");
			return DesiredCapabilities.internetExplorer();
		default:
			System.out.println("browser : " + browserType + " is invalid, Launching Firefox as browser of choice..");
			return DesiredCapabilities.firefox();
		}
	}
}

We will call 'getDriver' method which will intern call 'getBrowserCapabilities' based on browser parameter from the below two classes. If we pass 'chrome' as parameter, it is invoke chromedriver

Let us create a class as 'ParallelTestA.java' as below:

package com.test;

import java.net.MalformedURLException;

import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelTestA {
	
	public static RemoteWebDriver driver;
	public static String appURL = "http://www.google.com";
	
	@BeforeClass
	@Parameters({ "browser" })
	public void setUp(String browser) throws MalformedURLException {
		System.out.println("*******************");
		driver = Browser.getDriver(browser);
		driver.manage().window().maximize();
	}
	
	@Test
	public void testGooglePageTitleInFirefox() {
		driver.navigate().to(appURL);
		String strPageTitle = driver.getTitle();
		Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match");
	}

	@AfterClass
	public void tearDown() {
		if(driver!=null) {
			System.out.println("Closing browser");
			driver.quit();
		}
	}
	
}

Let us create a class as 'ParallelTestB.java' as below:

package com.test;

import java.net.MalformedURLException;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelTestB {
		
	public static RemoteWebDriver driver;
	public static String appURL = "http://www.google.com";
	
	@BeforeClass
	@Parameters({ "browser" })
	public void setUp(String browser) throws MalformedURLException {
		System.out.println("*******************");
		driver = Browser.getDriver(browser);
		driver.manage().window().maximize();
	}
	
	@Test
	public void testGooglePageTitleInChrome() {
		driver.navigate().to("http://www.google.com");
		String strPageTitle = driver.getTitle();
		Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match");
	}
	
	@Test
	public void testSearchGoogle() {
		System.out.println("Opening Google..");
		driver.navigate().to(appURL);
		driver.findElement(By.name("q")).sendKeys("Selenium Easy Grid Tutorials");
		driver.findElement(By.name("btnG")).click();
	}

	@AfterClass
	public void tearDown() {
		if(driver!=null) {
			System.out.println("Closing browser");
			driver.quit();
		}
	}
}

Now to execute these tests, we need to create testng.xml file as below and set parallel="tests" with parameter browser for each test

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Main Test Suite" parallel="tests" verbose="1">
    <test name="Grid firefox Test">
    <parameter name="browser" value="firefox"/>
        <classes>
        <class name="com.test.ParallelTestA"/>
        </classes>
    </test>
    <test name="Grid chrome Test">
    <parameter name="browser" value="chrome"/>
        <classes>
        <class name="com.test.ParallelTestB"/>
        </classes>
    </test>
</suite>

Once we execute the above code, hub console will display information such as on 'number of nodes available', on which node it has started executing the tests etc.. as below :

grid hub test execution

There are many other parameters for browser settings that we can pass when registering node to hub. When ever we use -browser parameter, the default browsers will be ignored and only what we specify in command line will be used.

Example :

-browser browserName=firefox,version=30,maxInstances=5,platform=WIN8_1

After executing the above command, if you check the grid console, it will just show the browserName, browser version, Max instances (number of instances of same version of browser you can run over the Remote System) and the platform (OS running on the node) that we have used to register the node. Grid console looks like below :

grid registered node info

If the remote machine has multiple versions of Firefox, We can map the location of each binary to a particular version on the same machine to execute with multiple versions as below:

-browser browserName=firefox,version=40,firefox_binary=c:\Program Files\firefox30\firefox,maxInstances=3,platform=WINDOWS -browser browserName=firefox,version=41,firefox_binary=c:\Program Files\firefox40\firefox,maxInstances=3,platform=WINDOWS

We will see more on these parameters in next tutorial.

Selenium Tutorials: 

Comments

Thanks for sharing the info. It's really excellent tutorial

Hi,

While automating hub - node machine, we are getting message from webpage. This message from webpage, we are trying to handle via auto it. while we are call autoit process, it searching in hub machine, instead of node machine. i want to click the node machine, message from webpage.

I guess that we need to pass some parameter in node. but i dont know how to use the parameter and how to call the parameter.

Thanks in advance.

i want to click message from webpage alert click in hub - node machine.

I tried using autoit process, but it searching for the hub macine, instead of node machine.

please help me out.

Great Article, thanks for sharing..

I am not able to execute the same and getting an error:

FAILED CONFIGURATION: @BeforeTest openBrowser
org.testng.TestNGException:
Parameter 'browser' is required by @Configuration on method openBrowser but has not been marked @Optional or defined

Can we setup selenium grid with ruby script ??

Hi,
Why gridconsle(http://localhost:44/) shows default firefox and chrome 5 instances, one browser instance is not enough

Hi,
Can we Run Selenium grid for parallel execution without the use of TestNG

we can use Selenium grid parallel execution using Junit parallelcomputer feature. check it in google.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.