Distributed Test Execution — Selenium Grid 3

Manish kumar
Dec 6, 2019 · 5 min read

Warm Regards to all the readers.

As I have learned and implemented selenium grid 3 in one of my projects. I wanted to share my learning in as simple way as possible with all of you so you can implement the same without facing any problem.

Before we jump into the setup for selenium grid 3, first let us go through the introduction of selenium Grid.

Selenium Grid Introduction:

Selenium Grid is an approach through which we can run our test cases in distributed environment.

Suppose if a user wants to execute the test cases on different browsers, platforms, Operating systems, then Selenium Grid is the best approach. Selenium Grid will execute all the test cases in the mentioned browser, platform, Operating system in parallel fashion.

To Setup Selenium Grid we need to have a hub machine and node machines.

HUB:

HUB machine is the controller, which controls the execution of the test cases. It directs the execution of the test cases to the node machine as per the test case configuration. There can be only one HUB machine.

NODE:

NODE machine is the machine where the test case execution happens, initiated by Hub. There can be multiple NODE machines.

Selenium Grid 3 Setup

Without wasting time, let us move forward on the setup part.

To setup HUB and NODE, we have some commands which we need to execute on the respective machines but before that you need to download selenium server executable file.

The executable which I used is “selenium-server-standalone-3.141.59.exe

Approach 1: Selenium Grid 3 setup without json config file

HUB setup:

  1. Open the command prompt on the HUB machine.
  2. Navigate to selenium server executable path in the command prompt.
  3. Now run the below command

java -jar selenium-server-standalone-3.141.59.jar -role hub

4. Now navigate to below mentioned path to verify that the hub is running.

http://localhost:4444/grid/console

NOTE: By default, hub works on port 4444. HUB port is configurable by providing the port parameter in the HUB execution command.

NODE setup:

  1. Open the command prompt on the Node machine.
  2. Navigate to selenium server executable path in the command prompt.
  3. Now run the below command

java -Dwebdriver.gecko.driver=”path to geckodriver.exe” -jar selenium-server-standalone-3.141.59.jar -role node -hub http://HubMachineIP:4444/grid/register -port 5555

4. Now navigate to below mentioned path to verify that the node is registered successfully.

http://localhost:4444/grid/console

As you can see that multiple browser icons are configured in the grid after running node command. Browser is also configurable for the node by providing the –browser parameter along with browserName as chrome, firefox etc in the node command.

Example:

java -Dwebdriver.gecko.driver=”path to geckodriver.exe” -jar selenium-server-standalone-3.141.59.jar -role node -hub http://HubMachineIP:4444/grid/register -port 5555 -browser browserName=firefox

NOTE:

1. Before selenium grid 3 version, we don’t need to set -Dwebdriver.gecko.driver property while using Firefox but in selenium grid 3 we have to set this property.

2. If you want to execute node on the same machine as hub, then provide same HubMachineIP to register at hub.

Approach 2: Selenium Grid 3 setup with json config file

HUB setup:

  1. Open the command prompt on the HUB machine.
  2. Navigate to selenium server executable path in the command prompt.
  3. Now run the below command

java -jar selenium-server-standalone-3.141.59.jar -role hub –hubConfig “pathToYourHubJsonFile\hub.json”

4. Now navigate to below mentioned path to verify that the hub is running.

http://localhost:4444/grid/console

Sample JSON (hub.json){
"port": 4444,
"newSessionWaitTimeout":-1,
"servlets": [
"com.groupon.seleniumgridextras.grid.servlets.SeleniumGridExtrasServlet",
"com.groupon.seleniumgridextras.grid.servlets.ProxyStatusJsonServlet"
],
"capabilityMatcher":"org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
"throwOnCapabilityNotPresent": true,
"nodePolling": 5000,
"cleanUpCycle": 50000,
"browserTimeout": 30000,
"timeout": 30000,
"maxSession": 5
}

NODE setup

  1. Open the command prompt on the Node machine.
  2. Navigate to selenium server executable path in the command prompt.
  3. Now run the below command

java -Dwebdriver.gecko.driver=”path to geckodriver.exe” -jar selenium-server-standalone-3.141.59.jar -role node -nodeConfig “pathToYourNodeJsonFile\node.json”

Sample JSON (node.json){
"capabilities":
[
{
"browserName": "firefox",
"maxInstances": 5,
"seleniumProtocol": "WebDriver",
"version": "70",
"platform": "WINDOWS"
}
],
"cleanUpCycle":50000,
"timeout":30000,
"browserTimeout":30000,
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 5,
"port": portForNode,
"host": "nodeIp",
"register": true,
"registerCycle": 5000,
"hubPort": 4444,
"hubHost": "hubHostIP"
}

NOTE: The parameters mentioned in hub.json and node.json files are for sample purposes. You can use these parameters as per your framework configurations.

Example Code:

I have added a simple sample code to understand how the selenium grid execution will work.

Firefox browser will open up, then google URL will be hit and search ‘Java Tutorials’. Please change HubIpAddress with your hub ip in below code.

package SeleniumGridSetup;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class SeleniumGridTest
{
WebDriver driver;
String baseURL = “https://www.google.com";
String nodeURL = “http://HubIpAddress:4444/wd/hub";
@BeforeTest
public void setUp() throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setBrowserName(“firefox”);
driver = new RemoteWebDriver(new URL(nodeURL), capability);
}
@AfterTest
public void afterTest() {
driver.quit();
}
@Test
public void sampleTest() throws InterruptedException {
driver.get(baseURL);
WebElement search = driver.findElement(By.name(“q”));
search.sendKeys(“Java Tutorials”);
Thread.sleep(4000);
}
}

So the same way you can initiate multiple nodes with different configurations like browsers, OS etc and create your automation framework to handle the test cases execution on these nodes.

Common Errors:

At the closure of this blog let me share some common error messages which we observe while implementing selenium grid.

  1. If webdriver.gecko.driver property is not set then, below mentioned error will be observed.

org.openqa.selenium.SessionNotCreatedException: Unable to create new service: GeckoDriverService

Driver info: driver.version: unknown

2. If you start the node on machine with a port which is already used by some other process then below message will be observed.

Port xxxx is busy, please choose a free port and specify it using -port option

3. Add configuration in json file as per the machine available for grid setup else you might observe error as below.

Error forwarding the new session Request timed out

Hope this blog will help you in selenium grid 3 setup.

Xebia Engineering Blog

The Xebia Engineering Blog

Manish kumar

Written by

Xebia Engineering Blog

The Xebia Engineering Blog

More From Medium

More from Xebia Engineering Blog

More from Xebia Engineering Blog

More from Xebia Engineering Blog

More from Xebia Engineering Blog

Istio Ingress Request Flow

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade