• info@helpingtesters.com
  • helpingtesters

SUBSCRIBE TO OUR NEWSLATER





Selenium GRID with Webdriver

December 26, 2017
Selenium Grid, Selenium Grid with WebDriver, Selenium Grid and WebDriver, WebDriver with Selenium Grid

Selenium Grid is a part of the Selenium Suite that specializes in running multiple tests at the same time on different machines, different browsers, and different operating systems. 

What are the Advantages of Selenium Grid

 Selenium Grid helps us to execute faster if we have many Test cases and if we are running them parallelly obviously the execution time will get reduced.

  • We can run in different environments, one machine can be Unix based machine others can be the windows based machine. We can run it on Mac as well where we can keep the machines in parallel and run.
  • Using selenium grid we can perform cross-browser testing.
  • Can run a single test on multiple browsers at the same time.
  • Selenium Grid supports distributed test execution. It allows running our tests in a distributed test execution environment.

When can we go for Selenium Grid

We can go for Selenium Grid if we want to run the test cases faster or if we want to run your tests at the same time on different machines, different browsers, and different operating systems.

Introduction to Grid 2

The Selenium Grid is a testing tool which allows you to run the Test cases parallel across multiple machines. i.e running multiple tests at the same time on different machines, different browsers, and different operating systems.

In Selenium Grid we can make the central hub, where we actually connect various machines to this hub and we can run our Testcases parallelly on this machines which save a lot of time especially if we are having 1000 or more Testcases. The each of the machines which we connect to the hub is known as a Node.

The Hub

  • Hub is a computer which is the central point where we can load our tests.
  • The selenium grid has only one hub which is the master of the network.
  • The hub is launched only on a single machine, say, a computer whose O.S is Windows 10 and whose browser is Firefox.

The Nodes

  • Nodes are selenium instances which are used to run tests used by the hub.
  • There can be one or more nodes in a selenium grid.
  • Nodes can be launched on multiple machines with different platforms and browsers.

 

In the above scenario or configuration port no plays a crucial role. 4444 is the default port used by the hub and then it can connect to a node (eg-5000) on which selenium can run. Similarly, on each node, there is a port assigned on which selenium runs.

Note:  It is not mandatory that on one node only one browser will work. We can have multiple browsers running parallel on one node.

Note: On one PC we can create multiple nodes. On this node, we can execute the test cases parallel or serially.

Downloading Jar files

To work with selenium grid we need to download standalone server jar file. Go to the link http://www.seleniumhq.org/download/ and download Selenium Standalone  Server version as below

Go to the location of Jar file and extract the zip file. First, we need to make our machine a Hub. Until and unless we don’t start the hub we cannot start the node.

Starting a Hub

Go to Command Prompt and go to the location where we have standalone jar file lie and type the command. In the below statement, we have started Hub using default parameters, 4444 is the default port used by the hub where Hub listen for new requests through port 4444. 

“java -jar selenium-server-standalone-3.6.0.jar -role hub”

 

After starting the hub, we can view the status of the hub by opening any browser window and navigating to http://localhost:4444/grid/console. On my machine, it is at localhost:4444. 

Starting a Node

Nodes are where our tests run. Each node is a machine whether it can be a virtual machine or physical machine which we register with the hub. Below is the command to register node with a Hub. Go to Command Prompt and go to the location where we have standalone jar file lie and type the command. Note: Do not close the Hub

“java -jar selenium-server-standalone-3.6.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5566″

After executing above command, you should see like below. When the hub is running on the same machine, we use ‘localhost’ for a node. If Hub and Node are running on separate machines, we have to register Node using the hostname of the remote machine running the hub.

By default, when we start the node, we can see that one node is started with port no 5566. On this node by default, when we start the node, it starts total 11 browsers: 5 Firefox, 5 Chrome and 1 Internet Explorer, and return supports up to 5 concurrent tests from above browsers. We can describe the no of browsers we want to run. If we want to reduce or increase the number of browsers with which we work on one node we need to understand the concept of maxSession and maxInstance.

Difference between maxSession and max instance

maxSession and maxInstances are the parameters which we can give with the command while starting a node. For example, if we want to open up a node and work on a Firefox for that we just need to append to the port as shown below

“java -jar selenium-server-standalone-2.29.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName-firefox”

Go to Command Prompt and paste the command and we see that Registering the node to the hub is done.

Go to the Selenium Grid web interface and refresh the console. You should see that only one Mozilla browser.

MaxInstances: It says how many instances of the same version of the browser can run over the Remote System. For example, if we want to open a Firefox browser with maximum 2 instances then we declare the command as follows then we can see that it run 2 instances of Firefox

“java -jar selenium-server-standalone-2.29.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName-firefox, maxInstances=2″

What does Default “Supports up to 5 concurrent tests” mean

It means that at one-time maximum 5 browsers would be opened in the current session. Based on we can increase or decrease the configuration by using the command -maxSession

MaxSession: It says how many browsers can run in parallel at a time in the remote system.For example, if we want to open a Firefox browser with maximum 2 instances and 3 sessions then we declare the command as

“java -jar selenium-server-standalone-2.29.0.jar -role webdriver -hub” “http://localhost:4444/grid/register -port 5556 -browser browserName-firefox, maxInstances=2  -maxSession 3″. Go to the Selenium Grid web interface and refresh the console. You should see that it changes to 3

 

Selenium Grid Practical Examples

Example 1: Running Test on single node on multiple browsers

Step1: Go to Eclipse and create a New Project “Right Click on Package Explorer->New->Java Project”

Step2: Create a Package under Project and click on Finish

 

Step3: Create a Class

 

package testcases;

import java.net.URL;

import java.net.MalformedURLException;

import org.openqa.selenium.By;

import org.openqa.selenium.Platform;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

public class Google_Search {

@Test(dataProvider=”getData”)

public void searchTest(String u, String p, String b) throws MalformedURLException {

System.out.println(b);

DesiredCapabilities cap = null;

if(b.equals(“firefox”)) {

cap = DesiredCapabilities.firefox();

cap.setBrowserName(“firefox”);

cap.setPlatform(Platform.ANY);

} else if (b.equals(“chrome”)) {

cap = DesiredCapabilities.chrome();

cap.setBrowserName(“chrome”);

cap.setPlatform(Platform.ANY);

} else if (b.equals(“iexplore”)) {

cap = DesiredCapabilities.internetExplorer();

cap.setBrowserName(“iexplore”);

cap.setPlatform(Platform.WINDOWS);

}

//Give URL of Hub

RemoteWebDriver driver = new RemoteWebDriver (new URL(“http://localhost:4444/wd/hub”),cap);

driver.get(“http://gmail.com”);

driver.findElement(By.id(“Email”)).sendKeys(“u”);          driver.findElement(By.id(“Passwd”)).sendKeys(“p”);

driver.quit();

}

@DataProvider(parallel=true)

public Object[][] getData() {

Object data[][]= new Object[2][3];

//row 1

data[0][0]=”U1”;

data[0][1]=”P1”;

data[0][2]=”firefox”;

//row 2

data[1][0]=”U2”;

data[1][1]=”P2”;

data[1][2]=”chrome”;

return data;

}

}

testng.xml

<suite name = “Selenium Grid with webdriver” verbose= “3” parallel=”tests” thread-count=”2” >

<test name = “Selenium Grid”>

<classes>

<class name = “testcases.Google_Search”/>

</classes>

</test>

</suite>

Before running the program we need to run the hub and start the node as well.

“java -jar selenium-server-standalone-2.29.0.jar -role webdriver -hub”

“http://localhost:4444/grid/register -port 5556 -browser browserName=firefox, maxInstances=3  -browser  browserName=chrome,  maxInstances=3 -browser  browserName=iexlpore, maxInstances=1 -maxSession 3″

Right Click on testng.xml and Run the program as TestNG Suite

Output:

Example 2: Running single Test on multiple nodes. On Single machine create two nodes on one node run it on firefox and on other node run it on chrome

package testcases;

import java.net.URL;

import java.net.MalformedURLException;

import org.openqa.selenium.By;

import org.openqa.selenium.Platform;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

public class Google_Search {

@Test(dataProvider=”getData”)

public void searchTest(String u, String p, String b) throws MalformedURLException {

System.out.println(b);

DesiredCapabilities cap = null;

if(b.equals(“firefox”)) {

cap = DesiredCapabilities.firefox();

cap.setBrowserName(“firefox”);

cap.setPlatform(Platform.ANY);

} else if (b.equals(“chrome”)) {

cap = DesiredCapabilities.chrome();

cap.setBrowserName(“chrome”);

cap.setPlatform(Platform.ANY);

} else if (b.equals(“iexplore”)) {

cap = DesiredCapabilities.internetExplorer();

cap.setBrowserName(“iexplore”);

cap.setPlatform(Platform.WINDOWS);

}

//Give URL of Hub

RemoteWebDriver driver = new RemoteWebDriver (new URL(“http://localhost:4444/wd/hub”),cap);

driver.get(“http://gmail.com”);

driver.findElement(By.id(“Email”)).sendKeys(“u”);          driver.findElement(By.id(“Passwd”)).sendKeys(“p”);

driver.quit();

}

@DataProvider(parallel=true)

public Object[][] getData() {

Object data[][]= new Object[2][3];

//row 1

data[0][0]=”U1”;

data[0][1]=”P1”;

data[0][2]=”firefox”;

//row 2

data[1][0]=”U2”;

data[1][1]=”P2”;

data[1][2]=”chrome”;

return data;

}

}

testng.xml

<suite name = “Selenium Grid with webdriver” verbose= “3” parallel=”tests” thread-count=”2” >

<test name = “Selenium Grid”>

<classes>

<class name = “testcases.Google_Search”/>

</classes>

</test>

</suite>

Before running the program we need to run the hub and start the node as well. Go to Command Prompt and start the hub:

“java -jar selenium-server-standalone-2.29.0.jar -role webdriver -hub”

Go to Command Prompt and start the node:

“http://localhost:4444/grid/register -port 5557 -browser browserName=firefox, maxInstances=3  -browser  browserName=chrome,  maxInstances=3 -browser  browserName=iexlpore, maxInstances=1 -maxSession 3″

Go to Command Prompt and start up another node:

“http://localhost:4444/grid/register -port 5558 -browser browserName=firefox, maxInstances=3  -browser  browserName=chrome,  maxInstances=3 -browser  browserName=iexlpore, maxInstances=1 -maxSession 3″

Note: When we start another node make sure that we change the port no.

Right Click on testng.xml and Run the program as TestNG Suite. Go to the Selenium Grid web interface and refresh the console. You should see that 2 nodes running one on Mozilla and other nodes on chrome.

Configuring JSON file format to initialize/configure hub and nodes

Initially, we start the grid by giving a command to start a hub and a node and on the node, we give the total no to browsers,maxInstances, and maxSessions as a part of the command. To overcome this we have JSON file where we keep the specifications of a node like browserName, maxInstances, maxSessions, port number and timeout and similarly for a hub as well we can describe our own JSON file.

How to read properties from JSON file to start a node and a hub

We need to create JSON file for hub and node. Now we are going to see the JSON that is needed for the hub.
a) Open any text editor like notepad
b) Past content in the editor from JSON for Hub code block.
c) Save this with hub.json in the same place where you have your selenium-server-standalone jar file

Configuring hub using below JSON:-

{

“port”: 4444,

“newSessionWaitTimeout”: -1,

“servlets” : [],

“withoutServlets”: [],

“custom”: {},

“capabilityMatcher”: “org.openqa.grid.internal.utils.DefaultCapabilityMatcher”,

“throwOnCapabilityNotPresent”: true,

“cleanUpCycle”: 3000,

“role”: “hub”,

“debug”: false,

“browserTimeout”: 0,

“timeout”: 2000

}

We start a hub with the command

java -Dwebdriver.chrome.driver=H:\chromedriver.exe -jar selenium-server-standalone-2.30.0.jar -role hub and at the end of the command append the command -hubConfig hub.json

-hubConfig is a new parameter that is being followed by the json file that has been created for the hub. We have to specify a JSON format config file to configure the hub/node and start it.

Command:

“java -Dwebdriver.chrome.driver=H:\chromedriver.exe -jar selenium-server-standalone-2.30.0.jar -role hub -hubConfig hub.json”

Copy the above command and give it on the command prompt to start a hub

Now open your browser and enter the URL with localhost with the port number. In our case, it was with http://localhost:4444/grid/console. You should see something like below, which indicates Grid configured successfully and is Up and running

As said, hub/node can be configured in 2 different ways, one is by specifying  command line parameters, and the other way is by specifying a JSON file

Configuring nodes using below JSON

Node1 JSON

{

“capabilities”:

[

{

“browserName”: “firefox”,

“maxInstances”: 1

},

{

“browserName”: “chrome”,

“maxInstances”: 1

},

],

“configuration”:

{

“nodeTimeout”:120,

“port”:5555,

“hubPort”:4444,

“hubHost”:”localhost”,

“nodePolling”:2000,

“registerCycle”:10000,

“register”:true,

“cleanUpCycle”:2000,

“timeout”: 30000,

“maxSession”:5,

}

}

Go to command prompt and paste the command to start up a node1

“java -jar selenium-server-standalone-2.30.0.jar -role rc -nodeConfig node1.json”

Go to Localhost localhost:4444/grid/console and refresh the console we can see that first node as started on which one firefox and one chrome are available because on node1 JSON we had given one firefox and one chrome maxInstance.

I have been in software testing for a decade. Maintaining HelpingTesters.com website to make a common platform to share my knowledge with everyone or where others can share to help out other testers. If you are willing to help than let me know at info@HelpingTesters.com

About the author

HelpingTestersTeam administrator

I have been in software testing for a decade. Maintaining HelpingTesters.com website to make a common platform to share my knowledge with everyone or where others can share to help out other testers. If you are willing to help than let me know at info@HelpingTesters.com

Leave a Reply

Your email address will not be published.