Listen to this blog
Selenium Grid helps you to leverage parallel execution and cross-platform testing at the same time. You would most definitely know how much valuable time this can save. If you are running a race against time on a project with a quick deadline then this right here can be a lifesaver. So let’s get to know more about Selenium Grid and how to make this wonder possible.
If you have multiple testbeds (i.e, Virtual Machines or Desktop PCs which are allocated for automated script execution), then you can use Selenium Grid to send Selenium Commands and Session creation request to a centralized server (i.e., Hub) which receives the client requests and assign it to the applicable nodes.
In this blog article, you will learn what is Selenium Grid, how to use it, and all the prominent Selenium Grid 4 features.
Remote WebDriver (Selenium Standalone)
Before learning Selenium Grid, you should be familiar with ‘Selenium Remote WebDriver’.
Let’s say you have Selenium Scripts on a Windows machine and want to run the scripts on a Safari browser on a Mac machine. The older approach needed you to move the scripts from the Windows machine to the Mac machine and then only execute them on a Safari browser.
But now, if you know how to use Selenium Remote WebDriver, you can execute the scripts from Windows itself by pointing to the Mac machine Selenium Server URL. This technique makes the otherwise tedious process very simple. Let’s dive deeper into it, shall we?
Download Selenium Server
Download the Selenium Server Jar file from Selenium.dev website.
Start Selenium Server
Use the below command to start the Selenium server.
java -jar selenium-server-standalone-3.141.59.jar
By default, Selenium Standalone Server runs on port 4444. You can use the below snippet to launch the browser.
DesiredCapabilities capability = new DesiredCapabilities(); capability.setBrowserName("chrome"); RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capability);
Selenium Standalone Server Configuration
- BrowserTimeout – You can use this option to make the WebDriver Browser Session wait while a WebDriver command is being executed. Let’s say driver.get(url) is being executed with BrowserTimeout=60 secs. The WebDriver Session will wait for 60 secs and quit the session if the command is not complete within the given timeframe. Note: If the BrowserTimeout is not specified when starting the server, then the session will wait indefinitely if a command request does not get a response.
- Config – If you have the Standalone Server configurations in a Json file, you can use the config option to mention the json file path. If the config option is not used, it will take the default values.
- Port – As we mentioned earlier, Selenium Server’s default port is 4444. If you would like to override, you can do it using the Port option.
- Role – Selenium WebDriver has three roles for the server. (Standalone, Hub, and Node)
- Timeout/SessionTimeout – The Selenium Standalone server has multiple session slots. Each slot should be released for other test executions. Let’s say a session is idle for 240 seconds (i.e., The Remote WebDriver session is not receiving any commands from the client for 240 seconds). In this case, the standalone server will release the session slot for other tests. 240 seconds is not a default value. You can mention any value based on your requirement.
Why Selenium Grid?
Selenium Standalone server is not sufficient for distributed execution. If you have 5 testbeds for automated test script execution, then you have to manage 5 Selenium Servers. Moreover, each server has its own URL. So you need to update the test suites with 5 server URLs as well. All of this can be easily simplified when using the Selenium Grid.
Selenium Grid has only two components, one is Hub and another one is Node. All your tests point Hub. The Hub acts as a centralized server that is responsible for assigning a Node for WebDriver session initialization and command execution requests.
Let’s assume you have three Windows machines and one Mac machine. One of the Windows machines is used for Hub & its nodes. The other three machines (2 Windows & 1 Mac) are used for the other Node setup.
Refer to the below diagram.
To run your Selenium test scripts on Safari using a Mac machine, you can use any of the following port numbers 5564, 5565, and 5566 as per the diagram. Since this is a Selenium Grid Architecture, you can use the Hub URL instead of Node’s URL.
When you use the Hub URL, all the session creations and command requests will go to the Hub. The Hub will decide which node is suitable for the client request. Desired Capabilities plays a crucial role in node allocation.
If you are planning to add or remove any node, you need not change the Remote URL in your test suite, because all your scripts are using Hub URL not Node URL.
Selenium Grid helps you in setting up a cross-browser testing infrastructure and it eases the Selenium Server management using Hub and Node roles. When you have a simple execution architecture, you can go with the Selenium Standalone server. If you are planning for parallel execution, cross-browser testing execution, and performance test script execution on local browsers, then the Selenium Grid setup is a must.
As an automation testing company, we use Selenium Grid for multiple automation testing projects to enable parallel execution.
Starting a Selenium Grid Hub is easy. Just run the below command in Terminal/Command Prompt and it will start Hub on port 4444.
java -jar selenium-server-standalone-3.141.59.jar -role hub
- Matcher/CapabilityMatcher – Hub has its own logic to select a node to start a WebDriver session. However, you can override this logic using the Matcher option. All you need to do is create a class and write the logic. Refer DefaultCapablityMatcher Java class to write a custom matcher.
- CleanUpCycle – Hub needs to check & kill the idle sessions at frequent intervals. How often the cleanup needs to be carried out is determined using the CleanUpCycle option. The default value is 5000 milliseconds.
- Log – If you want all the Hub logs in a file instead of STDOUT, then pass the filename in the log option.
- MaxSession – MaxSession is used at the Hub level to mention how many WebDriver sessions are allowed in parallel on a node. The default max session at Node level is 5. If Hub has Max Session configuration, then it will override the Node’s Max Session.
- NewSessionWaitTimeout – Sometimes Nodes may not be available for a new session request. You can use the NewSessionWaitTimeout option to make your test wait for the specified milliseconds instead of waiting for a Node indefinitely.
- ThrowOnCapabilityNotPresent – If ThrowOnCapabilityNotPresent=true and no node is available for the specified desired capabilities, then your test will throw an error else the test will wait until the supporting node is registered under the hub.
- Registry – Hub’s Kernel logic is written in DefaultGridRegistry.java which is responsible to check free & used nodes and assign nodes to the incoming client requests. If you would like to override this logic, use the Registry option.
- JettyThreads – The default value is 200. Let’s say you are going to attach 50 nodes under a Hub. Then JettyThreads=200 is not sufficient. You need to increase the JettyThreads. 50 nodes mean each node supports 5 max sessions by default. So 50 X 30 = 250 threads are required. Use JettyThreads/JettyMaxThreads to increase the thread pool.
Once the Hub is started, you can start and attach a node from any machine. All you need is the Hub’s URL.
Use the below command to register a node.
java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://ip-address:4444/grid/register/
- Capabilities – This is an optional parameter. By default, you will get 5 Chrome, 5 Firefox, 1 Internet Explorer instance. If you want all the 11 instances as Chrome browser, then use the capabilities parameter while creating a node. Example:
java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://ip-address:4444/grid/register/ -capabilities browserName=Chrome, maxInstances=11
- NodePolling – Hub needs to check whether the registered nodes are alive in a frequent interval. By default, Hub will check the nodes’ availability every 5 seconds.
- DownPollingLimit – Let’s say DownPollingLimit=3. If the node is not responding in three-node polling, then the node will be marked as Down in Hub. The default value for DownPollingLimit is 2 attempts.
- Register – This is a boolean parameter. If the Hub is unavailable, then the nodes will try to re-register with Hub. If register=false, the nodes will never try to re-register.
- RegisterCycle – How often the nodes can try to re-register with Hub is determined using the RegisterCycle parameter. The default value is 5000 milliseconds. Every 5 seconds the nodes will try to re-register if the Hub is unavailable.
- UnregisterIfStillDownAfter – If a node is down for more than 60 seconds, then the node will stop attempting to re-register with Hub. If you would like to increase/decrease the default 60 seconds, then use the UnregisterIfStillDownAfter parameter.
Hub and nodes creation initiated on Command Prompts/Terminal Windows. Checking the status of Hub and Nodes in the Terminal Window is a cumbersome task. Selenium Grid has a Web GUI to view the registered nodes and Hub configuration. Once the hub has started, you can view the Hub configuration in the following Grid Console URL http://localhost:4444/grid/console.
If you use VisGrid Selenium Grid UI, you don’t need to start Hub & Nodes in Command Prompt. Stopping/Managing Hub & Nodes is easy with VisGrid. As a leading Selenium Testing Services provider, we at Codoid created this UI to ease the Selenium Grid setup. However, after using VisGrid internally for a month, we released it as freeware. Now, VisGrid is widely used by the test automation community.
You can manage multiple nodes in a single window. Moreover, Selenium Browser Drivers come with the VisGrid package.
Selenium Grid 4
As we mentioned before, by using Selenium Server Jar you can setup Standalone Server and Selenium Grid Hub & Node. Hub takes care of the below-mentioned tasks.
- Identify a suitable node to create a WebDriver Session.
- Find a node with a session ID to assign a WebDriver command execution request from the client.
- Check whether the registered nodes are alive using NodePolling.
Hub plays a crucial role in the Selenium Grid set up. If a machine is dedicated for Hub and you are registering local nodes, then the memory consumption to operate Hub and execute Selenium commands on the same machine will be high.
When you are planning for a complex Selenium Grid execution architecture, you need to go for Selenium Grid 4’s Distributed execution setup. What is new in Selenium Grid 4 you might wonder. The Hub’s processes are separated in Selenium Grid 4. So instead of overloading the Hub, you can setup each task of Hub in different machines.
Selenium Grid 4 is not released yet. But there is no need to worry as you can try it in the Selenium Alpha version.
Sessions – In Selenium Grid 4, first you need to start sessions.
java -jar selenium-server-4.0.0-alpha-5.jar sessions
Sessions are used to store Node URI and Session ID.
Distributor – Distributor is responsible to start a new WebDriver session on a suitable node and update the Session ID in Sessions Map against the node URI.
java -jar selenium-server-4.0.0-alpha-5.jar distributor --sessions http://localhost:5556
Router – Router receives WebDriver command requests and a new session request from the client. If the request is for a new session, then it will be redirected to the Distributor. If the request is for command execution, then Router will query the node URI using a Session ID in Sessions.
Once the node URI is received from Sessions, the Router will redirect the command execution to the node.
java -jar selenium-server-4.0.0-alpha-5.jar router --sessions http://localhost:5556 --distributor http://localhost:5553
Node – In Selenium Grid 4 distributed execution set up, Node will be registered under Router. If you have setup the router in a separate machine, you can mention the host parameter to point the router while starting the node.
java -jar selenium-server-4.0.0-alpha-5.jar node --detect-drivers
In Selenium Grid 4, you will get a new GUI to view nodes. However, it is not released in the Alpha version yet.
So the biggest takeaways from this Selenium Grid Tutorial are learning about Selenium Grid 3 & 4 and all the important configurations. If you are trying to achieve parallel execution in test automation, creating independent automated tests and elegant Selenium Grid setups are a must.
If the Selenium Standalone server is sufficient for you, then the Selenium Grid is not necessary at this point. However, knowledge of the Selenium grid will help you to setup distributed execution when you want to scale up the automated test execution.