Selenium Grid is a proxy server that enables the user to run tests in parallel on multiple machines. It manages different browser versions and configurations centrally. Selenium Grid allows the selenium test scripts to route the commands to remote web browsers. Two versions of Selenium Grid are the older ‘Grid 1’ and the newer ‘Grid 2’. We will only be concerned about Grid 2 because Grid 1 is gradually being despised by the Selenium Team. It uses a hub-node concept where the test is run only on a single machine called a hub, but parallel execution will be done by different machines called nodes.
With Selenium Grid, there is one server acting as the hub that routes the JSON formatted test commands to one or more registered Grid nodes. Tests contact the hub for accessing the remote browser instances. Hub has the list of all the registered servers to which it has to provide access and allows us to control these instances. Two important definitions related to selenium grid are:
Hub: The hub is the central point that kicks off the tests or a point where you load your tests. A Grid has only one hub and it is launched from one system. The hub will connect one or more registered nodes and the tests will be delegated to these registered nodes only. The tests will be run on the machine containing the hub but the browser on the node gets automated.
Node: Nodes are the Selenium instances that will execute the tests you have loaded on the hub on individual computer systems. A grid can have many nodes. The machines running the nodes need not be on the same platform as that of the hub or have the same browser as that of the hub or the other nodes.
When The Selenium Grid Is To Be Used?
Selenium Grid must be used when you want to do either one or both of the following:
1. Tests can be run against different browsers, operating systems, and machines all at the same time: For this, you need to ensure whether the application being tested is fully compatible with a wide range of browser-OS combinations.
2. Save the time in your test suites’ execution: If Selenium Grid is run, say, 3 tests at a time, then the whole suite would be finished around 3 times faster.
Testing The Framework For Parallel Execution:
A testing framework runs a sample Grid test case as it supports parallel test execution. Selenium Grid uses TestNG for running parallel tests. TestNG is quite similar to JUnit and it supports various test methods, parallel testing, and multi-threaded testing. TestNG being a threading model supports running multiple instances of the same test. After the creation of hub and nodes, TestNG must be configured in eclipse.
[ Related Read: Intro to TestNG Framework ]
Steps To Launch A Hub and A Node:
1. Go to the machine where you want to launch the hub. Navigate to the machine’s ‘C’ drive using the command prompt as it is the directory where the Selenium server is placed.
2. Type ‘java -jar selenium-server-standalone-2.30.0.jar -role hub’ on the command prompt.
3. The hub should be launched successfully.
4. After setting up the hub, a node needs to be launched.
5. Now go to the machine which will act as a node and launch a command prompt there.
6. After navigating to the machine’s Drive ‘C’, type
C:\> java -jar selenium-server-standalone-2.30.0.jar -role hub
webdriver –hub http://192.168.1.3:4444/grid/register – port 5566
where 192.168.1.3 is the IP address of the machine where the hub is running. We have used port 5566 while you can choose the port number as desired.
At this point, a simple grid has been configured. Now a test can be run remotely on the machine acting as a node.
Points To Be Kept In Mind For Best Practice:
1. Only one machine should act as “One node”.
2. Only one browser must be run by “One node”.
3. For parallel execution of various threads, a driver must be created as the ‘ThreadLocal’ variable.