Selenium-Grid is used to speed up the execution of a test by using multiple machines to run tests in parallel, it is also used to support running tests against multiple environments, specifically, against different browsers at the same time. Selenium Grid can be used in below scenarios:
- To run your tests against multiple browsers, multiple versions of browser, and browsers running on different operating systems.
- To reduce the time it takes for the test suite to complete a test pass.
In this article, we are going to explain the step-by-step guide to set up the Selenium Grid.
Installation is simple. Download Selenium Server jar file from Selenium’s official website which was formerly known as Selenium RC Server and save it at any location on the local disk.
Here is the download page in selenium HQ: http://www.seleniumhq.org/download/
The user would see a file like "selenium-server-standalone-3.141.59.jar" in the download folder after download is completed.
To ease out the configuration we have attached a Grid Setup zip file which is having all required files under it, download and extract Grid Setup folder, copy extracted Grid Setup folder to C drive.
The last part is to copy the above-downloaded Selenium Server jar file and past it under C:\Grid Setup folder.
Running Selenium Hub
The Hub is the central server that manages nodes, takes instructions from the client and executes them remotely on the nodes and manages test threads. Each Selenium Grid consists of exactly one hub. The hub needs to be reachable from the respective clients (i.e. CI server, Developer machine etc.) The hub will connect one or more nodes that tests will be delegated to.
Let's learn how to setup HUB:
Navigate to C:\Grid Setup folder, you will see below files under it.
Right-click on hub.bat file and edit it to make sure that the selenium server name (in blue below) is exactly the same as you downloaded after update saves and closes it. Please note that the bat file consists of a command that takes the jar file and refers hubconfig.json file to get configurations to need to run HUB. We have created a default config to run in bare minimum configuration, but user can update it based on their requirements (Read more about selenium config).
After saving double click on hub.bat file to run it, once running it should be shown as below.
To verify whether hub is running, open the browser and navigate to http://localhost:4444/grid/console. The console gives the information what is available on the Hub. As of now, it will be blank, as there is no machine connected to it. User can also click the Console link and then click View Config.
Running Selenium Node
Nodes are different Selenium instances that will execute tests on individual computer systems. There can be many nodes in a grid. A node on Windows might have the capability of offering Internet Explorer as a browser option, whereas this wouldn't be possible on Linux or Mac.
Getting files ready
Selenium nodes are best suited to set up in multiple machines(virtual machines typically) however, user can set up a node on the same machine as on hub. Here we are going to setup a node on the same machine as a hub, user can follow the same steps to setup a node on a different machine.
Let's navigate to C:\Grid Setup folder which was downloaded earlier and you will see below files under it.
Let's go thru these files and their usage one by one:
First three files in the above screenshot are browser driver.exe files, node will use these files to intact with browser, please update them to the latest version using below latest Driver Download links:
Node Config File
Node.json file consist of configurations and capabilities for the node, below are few important configurations:
- "browserName": "chrome" - It is the browser name where the tests are to be triggered
- "maxInstances": 5 - Maximum number of Chrome instances that can be run.
- "hub": "http://localhost:4444"-localhost:4444 can be replaced by hub IP/machine name in case hub resides in different machine
Node require below information to be able to connect hub, user has to find below two details from hub machine and supply them to "hub": "http://localhost:4444" configuration:
IP address/Machine name of HUB machine
The port on which the hub is running (by default it is 4444)
If you are trying this on local machine within 'Grid Setup' folder then you can use 'localhost' as machine name and 4444 as a port of hub.
Now, right-click on node.bat file and edit it to make sure that the selenium server name (in blue below) is exactly the same as you downloaded after update save and close it.
Please note that the .bat file consists of a command that takes jar file, browser drivers (mentioned above) and node.json file to run the node. Please run the node.bat file under C:\Grid Setup folder by double-clicking it.
Once the nodes is successfully registered to the HUB, navigate the URL http://localhost:4444/grid/console (or http://<IP Address of the HUB machine>:4444/grid/console when the hub is remote machine) and the hub should now display the node which is attached to it.
User can run multiple nodes on multiple machines by setting up Grid Setup folder on multiple machines and editing node config pointing towards the hub.
Configure you Leapwork environment to start your test using this article. You are now ready to run your first test in the selenium grid.
For any clarification, please contact our Priority Support.