Selenium Grid 2.0 is used to scale up the test execution, supports distributed test execution, allowing to run automated tests on different machines against different  browsers in parallel. That is, running multiple tests at same time against different  machines running different  browsers  and operating  systems.

Why Selenium-Grid?
  1. To run tests against different browsers, multiple versions of browsers and browsers running on different operating systems.
  2. Parallel execution to reduce the time for the test suite execution.
How Selenium Grid 2.0 different from Grid 1.0

Selenium Grid 2.0 is quite different from Selenium Grid 1.0.  Selenium Grid 2.0 is merged with Selenium-RC server. Now, we only need to download a single .jar file to get the remote Selenium-RC-Server and Selenium-Grid all in one package.

How Selenium Grid 2.0 Works

Selenium Grid 2.0 consists of two parts,

  • Hub
  • Nodes

To start both Hub and Nodes, use Selenium Server executable jar file.

  • Nodes registered to the Hub, so Hub knows the configurations of each nodes that are registered.
  • Using this information, Hub redirects the execution to the available nodes when it received tests to be executed along with information on which browser and ‘platform’ the tests needs to be run.
  • Once a node has been selected, selenium commands initiated by the test are sent to the hub, which passes them to the node assigned to that test.
  • The node runs the browser, and executes the Selenium commands within that browser against the application under test.
How  to use Selenium Grid 2.0

Here are the steps one should follow to use Selenium Grid 2.0

  1. Install Selenium Grid
  2. Start Selenium Hub
  3. Start Selenium Nodes

1.  Installation: 

Note:  Make sure the java executable is on your execution path so you can run it from the command-line.

2. Start Selenium Hub

  • Open command prompt, go to the folder where selenium server executable jar file placed.
  • Run the command, java -jar <selenium server executable file> -role hub
  • This starts selenium hub using default parameter values, like port number 4444.
  • Message “Selenium Grid hub is up and running” shown in console once hub started successfully.

Command to start selenium hub

Selenium Grid console looks as below image after started Selenium Hub. (localhost:4444)

start selenium hub no nodes registered

No nodes are available as nodes are not yet registered to hub.

    Hub Configurations: 

The default port used by the hub is 4444. If another application on your computer is already using this port, or if, you already have a Selenium-Server started, you’ll see the following message in the log output.

If this occurs you can either shutdown the other process that is using port 4444, or you can tell Selenium-Grid to use a different port for its hub.

Use the -port option for changing the port used by the hub. java -jar <selenium server executable file> -role hub – port 4455

Note: Make sure port number given above should not be used by any other process.

To view the ports used by all running programs on your machine use the below command.

Netstat -a

3. Start Selenium Node

  • Open command prompt, move to the folder where selenium server executable jar file is placed and run the command.
    java -jar <selenium server executable file> 
    -role wd -hub http://<IP address on hub is running>:<port number on hub is running>/grid/register
    
  • Message “The node is registered to the hub and ready to use” shown on console once node registered successfully.

Command to start selenium node

  • Assumes the hub has been started above using default parameters. The default port the hub uses to listen for new requests is port 4444.
  • Assume node is running on same machine where Hub is running use ‘localhost’, here is the command to register node to hub.
java -jar <server executable file> -role wd -hub http://localhost:4444/grid/register
  • If running the hub and node on separate machines, simply replace ‘localhost’ with  the host name of the remote machine running the hub.
  • Once node is successfully registered to the hub, the Selenium Grid console looks as below image.

selenium grid console after registered a node

#10 browser instances are available in Selenium Grid per one node (#5 of type  Firefox, #5 of type chrome & #1 of type IE) when registered node with default  parameters as above command.

Node configurations:

1. Set Driver executable file paths 
  • Prior to Selenium 3.0, to execute tests on Firefox browser no need to set driver executable file path to System properties like Chrome browser.
  • To execute tests on Chrome browser, should set chrome driver executable file path to system properties as below when executing in local systems without  Selenium Grid.
System.setProperty("webdriver.chrome.driver","G:\\Drivers\\chromedriver.exe");

Here placed chrome driver in G:\ folder.

To execute tests on Chrome/Internet Explore using Selenium Grid, should provide driver executable file paths as a parameter while registering nodes. Here are the details.

  • For Chrome driver,  -Dwebdriver.chrome.driver= G:\Drivers\chromedriver.exe
  • For Internet Explore driver,  -Dwebdriver.ie.driver= G:\Drivers\IEDriverServer.exe

Note: From Selenium 3.0 to execute on Firefox browser, should pass Gecko Driver executable file, -Dwebdriver.gecko.driver =G:\Drivers\geckodriver.exe

Here is the command to start node with driver executable paths.

java -Dwebdriver.chrome.driver=<Chrome Driver path>
-Dwebdriver.gecko.driver=<Firefox Driver path>
-Dwebdriver.ie.driver=<IE Driver path> -jar <selenium server executable file>
-role wd -hub http://localhost:4444/grid/register
Possible Exception using Seleniun Grid 2.0

Selenium Grid returns below exception driver executable file paths are not provided while registering nodes.

Exception in thread main org.openqa.selenium.remote.UnreachableBrowser Exception: Could not start a new session. Possible causes are invalid address  of the remote server or browser start-up failure
How to execute tests using Selenium Grid 2.0

Assume using selenium-server-standalone-3.0.0-beta3.jar file.

  • Make sure start hub and ready for use by executing below command

 java -jar selenium-server-standalone-3.0.0-beta3.jar -role hub

  • Make sure to register the nodes by executing below command
    java -Dwebdriver.chrome.driver=G:\Drivers\chromedriver.exe   -Dwebdriver.gecko.driver=G:\Drivers\geckodriver.exe
    -jar selenium-server-standalone-3.0.0-beta3.jar -role wd 
    -hub http://localhost:4444/grid/register
    
  • Instantiate DesiredCapabilites object by providing browser details on which test should be executed.
  • Instantiate RemoteWebDriver by providing Selenium Hub URL and DesiredCapabilites object.
    WebDriver driver=new RemoteWebDriver(new URL("http://localhost: 4444/wd/hub"),cap);
    
 Code to execute script on Chrome:  
WebDriver driver;
DesiredCapabilities  cap=DesiredCapabilities.chrome(); 
driver=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub") ,cap);
driver.get("http://seleniumhq.org");
 Code to execute script on Firefox:  
WebDriver driver;
DesiredCapabilities cap=DesiredCapabilities.firefox(); 
driver=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap); 
driver.get("http://seleniumhq.org");
2.  To set browser capabilities

Automation scripts can be executed on multiple browser versions and platforms by registering browser detail nodes to browser parameter.

  • browserName=Name of the browser like firefox/chrome/internet explorer
  • version=browser version number
  • maxInstances= How many number of instances available for execution.
  • platform=name of the Operating System on node it is running like MAC/VISTA/XP/WIN8/WIN10 & ANY an be used to run on any Operating System.

Below is the configuration to execute on Chrome version 53 and on any operating system.

-browser "browserName=chrome,version=53,maxInstances=5,platform=ANY”

Complete command to register node to hub with browser details and driver executable files.

java -Dwebdriver.chrome.driver=G:\Drivers\chromedriver.exe
-webdriver.gecko.driver=G:\Drivers\geckodriver.exe
-jar selenium-server-standalone-3.0.0-beta3.jar -role wd
-hub http://localhost:4444/grid/register 
-browser "browserName=chrome,version=53,maxInstances=5,platform=ANY
How to execute on specific browser version and platform
  • Instantiate DesiredCapabilites object by providing browser name, version and Platform.
DesiredCapabilities cap=DesiredCapabilities.chrome(); cap.setPlatform(Platform.ANY);
cap.setVersion("53"); 
driver=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);driver.get("http://seleniumhq.org");
3. Use JSON file to register nodes

Suppose to run tests against multiple browsers in same node, should provide all browser details on which we need to run the automation tests. We can use JSON for the same.

Here is the sample JSON file named as “windows_node_FF47_Chrome53_IE11

{
 "capabilities":
 [
 {
	 "browserName":"firefox",
	 "version":"47",
	 "Platform":"VISTA",
	 "maxInstances":5
 },
 {
	 "browserName":"chrome",
	 "version":"53",
	 "Platform":"VISTA",
	 "maxInstances":5
 },
 {
	 "browserName":"internet explorer",
	 "version":"11",
	 "platform":"VISTA",
	 "maxInstances":3
 }
 ],
	 "configuration":
	 {
	 "maxSession":7
	 }
}
  • Above json given browser details to execute tests on Firefox version #47, Chrome version #53 and Internet explorer version #11 on windows platform.
  • And configures maxSession to control maximum number of session at same time can start independently in different browsers on a node.
  • Here it supports 7 sessions at a time among all the browsers in any order, that would be like 3 chrome, 3 firefox & 1 IE or 4 chrome, 1 firefox & 2 IE ..etc.

How to use JSON file

java-Dwebdriver.chrome.driver=G:\Drivers\chromedriver.exe 
-Dwebdriver.gecko.driver=G:\Drivers\geckodriver.exe
-Dwebdriver.ie.driver=G:\Drivers\IEDriverServer.exe
-jar selenium-server-standalone-3.0.0-beta3.jar -role wd
-hub http://localhost:4444/grid/register 
-nodeConfig windows_node_FF47_Chrome53_IE11.json

Below is the screenshot captured after registering nodes in Selenium Grid 2.0.

Node registered to selenium grid with json file

 

CSS psuedo class selector to identify an element on web page
Selenium Page Object Generator Chrome Extension
0