Selenium Grid Tutorial : Learn to Set It Up
By Garima Tiwari, Community Contributor and Pradeep Krishnakumar, Manager - August 2, 2019
Frequently adding new features to existing products has helped companies drive engagement on their applications. For example, PubG added features like newer maps, gameplay enhancements to drive more new users. Even Tesla plans to add Netflix and YouTube streaming capabilities in their cars soon.
Naturally, adding features to applications comes with its own set of challenges on organizations that are looking to deliver better software in record time. Automating processes have become a natural next step. Automation Testing is critical is making software deployment faster and bug-free. Back in 2016, 73% of respondents to the European Software Testing Benchmark Report said that they want to make automation testing 50% of their entire test suite in the next 5 years. 81% of them said that they see RoI on test automation within a year.
Automation testing has not only improved delivery timelines but has also ensured an enhanced scale at which the testing is performed. With this approach, organizations can deliver software more efficiently. From a technical perspective, this is done by performing cross browser compatibility tests by covering a set of different browsers, devices, and tools as a part of the testing process.
From designing an automated test solution to implementing, it is a time-consuming process due to a large number of browser versions, operating systems, and devices. Selenium Grid, which is a part of Selenium, makes this easier by offering concurrent execution.
This guide aims to offer a detailed overview of Selenium Grid when to use it, and a step-by-step tutorial for set up, installation and using it for cross browser testing.
To get started, let’s understand about Selenium –
Selenium suite comprises of 4 components:
Let’s get started to learn about Selenium Grid in detail.
What is Selenium Grid?
Selenium Grid is a smart proxy server that makes it easy to run tests in parallel on multiple machines. This is done by routing commands to remote web browser instances, where one server acts as the hub. This hub routes test commands that are in JSON format to multiple registered Grid nodes.
Hub conducts a concurrent execution of tests on multiple machines, managing different browsers centrally, instead of conducting different tests for each of them. Selenium Grid makes cross browser testing easy as a single test can be carried on multiple machines and browsers, all together, making it easy to analyze and compare the results.
The two major components of Selenium Grid are:
- Hub is a server that accepts the access requests from the WebDriver client, routing the JSON test commands to the remote drives on nodes. It takes instructions from the client and executes them remotely on the various nodes in parallel
- Node is a remote device that consists of a native OS and a remote WebDriver. It receives requests from the hub in the form of JSON test commands and executes them using WebDriver
When to use Selenium Grid?
Testers should use Selenium Grid in the following circumstances:
- To run tests on multiple browsers and their versions, different devices, and operating systems
- To reduce the time that a test suite takes to complete a test
Selenium Grid improves the turnaround time of the test results. This difference happens to be significant, especially when the test suite is large and takes more time to run. It offers flexibility and widens the coverage of testing in a limited time. Since the virtual infrastructure is in use, maintenance becomes easy.
Getting started with Selenium Grid browser testing
Here are the steps to follow for setting up Selenium Grid –
Step 1: Installation
Before getting started, download the Selenium Server Standalone package. This package is a jar file, which includes the Hub, WebDriver and legacy RC that is needed to run the Grid. To get started with Selenium Grid, it is essential to have Java already installed, and set up the environment variables.
Step 2: Start Hub
Hub is the central point in the Selenium Grid that routes the JSON test commands to the nodes. It receives test requests from the client and routes it to the required nodes. To set up the Hub, open the command prompt, and navigate to the directory, where the Selenium Server Standalone jar file is stored (downloaded in Step 1)
Type the following command
java -jar selenium-server-standalone-<version>.jar -role hub
This would start the hub automatically using port 4444 by default. Testers can change the default port by adding an optional parameter port, using
-host <IP | hostname>
while running the command. Testers need not specify the hostname as it can be automatically determined until you are using an exotic network configuration or networking with VPN, where specifying the host becomes necessary.
To view the status of the hub, open a browser window and navigate to https://localhost:4444/grid/console
Step 3: Start Nodes
Whether testers are looking to running a grid with new WebDriver functionality or with the Selenium 1 RC functionality or running both of them simultaneously, testers have to use the same Selenium Server Standalone jar file, to start the nodes. To start nodes open the command prompt and navigate to the directory, where the Selenium Server Standalone jar file is stored.
Type the following command
java -jar selenium-server-standalone-<version>.jar -role node -hub https://localhost:4444/grid/register
When -role option that is provided is not specified, and it is not the hub, the default port is 5555. So, it is important to define the -role to be a node in this case.
Step 4: Configure Nodes
When testers start the nodes, by default, it allows 11 browsers, i.e., 5 Firefox, 5 Chrome, and 1 Internet Explorer for concurrent use. It also allows testers to conduct a maximum of 5 concurrent tests by default.
Testers can change this and other browser settings, by configuring nodes. This can be done by passing parameters to each of the -browser switches that represent a node, based on the parameters.
As soon as the -browser parameter is used, the default browser settings shall be ignored and only the parameters that are specified in the command line shall be used.
Let us understand this with an example to set 4 Firefox version 4 nodes on a Windows machine.
In a case where the machine has multiple versions of Firefox that you wish to use, you can map the location of each binary to the compatible version on the same machine.
Let us understand this by the following example where we have two versions of firefox, namely 3.6 and 4 on the same Windows machine that have to be used at 5 and 4 instances respectively.
-browser browserName=firefox,version=3.6,firefox_binary=/home/myhomedir/firefox36/firefox,maxInstances=5,platform=WINDOWS -browser browserName=firefox,version=4,firefox_binary=/home/myhomedir/firefox4/firefox,maxInstances=4,platform=WINDOWS
This way, testers can configure the nodes as per their cross browser testing requirements, using the combination of browsers, their versions, and operating systems.
Step 5: Using Selenium Grid to run tests
Once the Selenium Grid setup is done by following the above 4 steps, testers can access the grid to run tests. If Selenium 1 RC nodes are being used, testers can use DefaultSelenium object and pass the same in the hub formation using the following command.
Selenium selenium = new DefaultSelenium(“localhost”, 4444, “*firefox”, “https://www.browserstack.com”);
If you are using Remote WebDriver nodes, then use RemoteWebDriver and DesiredCapabilities object to define the browser, version, and platform. For this, create the target browser capabilities to run the test on.
DesiredCapabilities capability = DesiredCapabilities.firefox();
Once created, pass this set of browser capabilities into the RemoteWebDriver object
WebDriver driver = new RemoteWebDriver(new URL("https://localhost:4444/wd/hub"), capability);
Once this is done, the hub would assign the test to a matching node, if all the requested capabilities meet. If you wish to request any specific capabilities on the grid, specify them before you pass it to the WebDriver object in the following pattern
capability.setBrowserName(); capability.setPlatform(); capability.setVersion() capability.setCapability(,);
The capabilities, if it does not exist on the grid, returns no match and thus, the test would fail to run.
Let us understand this using an example, considering a node is registered with the setting
Then, it is a match with the following set of capabilities defined for the test
capability.setBrowserName(“firefox” ); capability.setPlatform(“WINDOWS”); capability.setVersion(“4”);
It would also match with the following set of capabilities defined for the test
capability.setBrowserName(“firefox” ); capability.setVersion(“4”);
Point to be noted is that the capabilities which are not specified for the test would be ignored, such as in the above example where the platform parameter is not specified and it gets a match.
Using these steps, testers can easily set up, configure, and perform tests on Selenium Grid for concurrent execution of test suites.
Selenium Grid offers the convenience to perform concurrent testing on several browsers, browser versions, and machines. Testers can ensure cross platform compatibility of their web application by using BrowserStack Automate, where teams can get instant access to a Selenium Grid of 2000+ browsers on real desktops, iOS, and Android devices.
This would help deliver a consistent end-user experience that conforms with the continuous delivery approach, using Selenium Grid.