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.
- To run tests against different browsers, multiple versions of browsers and browsers running on different operating systems.
- 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,
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
- Install Selenium Grid
- Start Selenium Hub
- Start Selenium Nodes
- Download selenium server jar from here SeleniumHq Download page.
- Place it in a folder in local drive.
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.
Selenium Grid console looks as below image after started Selenium Hub. (localhost:4444)
No nodes are available as nodes are not yet registered to hub.
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.
3. Start Selenium Node
- Open command prompt, move to the folder where selenium server executable jar file is placed and run the command.
- Message “The node is registered to the hub and ready to use” shown on console once node registered successfully.
- 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.
- 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.
#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.
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.
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.
Possible Exception using Seleniun Grid 2.0
Selenium Grid returns below exception driver executable file paths are not provided while registering nodes.
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
- Instantiate DesiredCapabilites object by providing browser details on which test should be executed.
- Instantiate RemoteWebDriver by providing Selenium Hub URL and DesiredCapabilites object.
Code to execute script on Chrome:
Code to execute script on Firefox:
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.
Complete command to register node to hub with browser details and driver executable files.
How to execute on specific browser version and platform
- Instantiate DesiredCapabilites object by providing browser name, version and Platform.
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”
- 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
Below is the screenshot captured after registering nodes in Selenium Grid 2.0.