How to Setup a Hub and Node for Selenium Grid
- Selenium Grid allows us to execute our tests in multiple machines (physical / virtual) and multiple browsers with different versions, which dramatically speeds up test execution and helps in reducing total amount of time required for test execution.
- For example, if we have a script that takes 100 minutes to execute sequentially , we could break that down to 10 short tests script run across 10 machines, and can complete them in 10 minutes without copying your test code to the other machine.
- To get started with Selenium Grid, make sure you have Java installed and configured it and For selenium, you need to download selenium server and place it in a directory.
- A grid consists of a single hub, and one or more nodes, Hub and Node are the two main elements that you come across when using grid
- Hub the Hub is the central point which will receive all the test requests along with information on which browser, platform (i.e. WINDOWS, LINUX, etc) and where the test should be run. Based on the request received, it will distribute them to the registered nodes.
- To start a hub with default parameters, we can run the below command from a command-line. Just navigate to the directory where your selenium jar file is available and execute the below statement. You can open command prompt from the same folder using 'Press Shift and Right Click' you see an option 'Open command window here'.
- In the above statement, we have started Hub using default parameters, So the default port will be 4444 and Hub listen for new requests is port 4444. This is why port 4444 was used in the URL for locating the hub. You can also change the default port, by adding the optional parameter -port when you run the command example: -port 5555.
- After starting the hub, we can view the status of the hub by opening any browser window and navigating
If you have used any other port, you need to mention that port value instead of default port 4444.
- Nodes are where our tests will run, each Node is machine (can be a physical machine / virtual machine) that we register with the Hub, when we register Node, Hub will get to know about the node, and it will display browser and configuration details of the node that we used to register node with parameters.
Below is the command to register node with a Hub. If we are not specifying any parameters when starting node, it defaults to 5555 whenever "-role" option is provided and is not hub
- After executing above command, you should see something like below. When the hub is running in the same machine, we use ‘localhost’ for 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, it starts total 11 browsers : 5 Firefox, 5 Chrome and 1 Internet Explorer and it has same set of browsers for Selenium Remote Control (legacy). The maximum number of concurrent tests is default to 5.
- We can change this and other browser settings as well by passing the parameters to each -browser switch (each switch represents a node based on your parameters). If you use the -browser parameter, the default browsers will be ignored and only what you specify command line will be used. The node can be configured in two different ways, One is by specifying command line parameters, the other is by specifying by a json file. We will discuss more in detail on these parameters in next articles.
- For now, this is how it looks when we register without specifying any parameters
- When you mouse hover on the browser icons, it will show config information, here "seleniumProtocol": "Selenium" which provides the mechanism for Selenium RC (Remote control / Selenium 1) and "seleniumProtocol": "WebDriver" is for Selenium WebDriver. After registering the node, You can see the difference in grid console as above.
- We have done with starting the Hub and Registering Nodes with the Hub. Now we need to run our tests with Selenium Grid, For webdriver nodes, we need to use the RemoteWebDriver and DesiredCapabilities object to define which browser, version of the browser and platform (OS - Windows / LINUX etc) that we want to run our tests.
- Based on preferences that we set in the DesiredCapabilities instance, the Hub will point our tests to a node that matches with these preferences. If we specify capabilities that do not exist on our grid then there will be no match and the test will fail to run.
- Why we need to use RemoteWebdriver Not the webdriver?
If we use driver (FirefoxDriver / ChromeDriver / or other) not RemoteWebDriver, it will just assume that the communication to the browser is local. Example: - Webdriver driver = new FirefoxDriver(); Using this, driver will access Firefox browser which is available on the local machine.
If we use RemoteWebDriver, it requires us to specify where the Selenium Server is located and on which web browser we want to execute our tests. For example,
- Here in above statement , it is specified that Selenium Server is running on localhost with the default port 4444 and execute on firefox browser. In the same fashion, we can run selenium server on one machine as Hub and execute selenium tests on other machine by registering to the node by specifying parameters.
- In the below example, Hub will point the test to a node which is running on the Windows machine (local machine) with Firefox browser as my node is executing in windows machine and capabilities specified is Only firefox browser.
- In the above test, BeforeClass method open firefox browser and , verifies page title of google. Once done, it will quit the remote webdriver.
Below is the testng.xml file :
- After executing test as TestNG, you can view the console for details such as on which browser the tests are executed along with version and platform details if specified any.