App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide Selenium Grid 4 Tutorial

Selenium Grid 4 Tutorial

By Neha Vaidya, Community Contributor -

The addition of new features to existing products has helped companies drive engagement on their applications. For example, Tesla plans to add Netflix and YouTube streaming capabilities in their cars soon.

Adding advanced features to applications comes with a set of challenges for organizations looking to deliver better software in record time. Automating processes has, naturally, become indispensable to meeting their goals.

Automation Testing is critical in making software deployment faster and bug-free. Achieving this is much easier with Selenium Grid 4 – the latest version of Selenium Grid with more advanced features.

This article runs users through a beginners’ Selenium Grid 4 tutorial.

What is Selenium Grid 4?

Selenium Grid is a smart proxy server that makes it easy to run tests in parallel on multiple machines and manages different browser versions and browser configurations centrally (instead of separately, in individual tests).

Selenium Grid 4 is a fresh implementation and does not share the codebase of the previous version. Grid 4 takes advantage of new technologies in order to facilitate scaling up, while still allowing local execution.

Note: Before going further with this article, have a look at this Selenium 4 article for a better understanding of its latest features adapted.

Selenium Grid 4 Features

Architecture Support

From Selenium 2, the Grid consisted of two processes – Hub and Nodes. Selenium 4 architecture supports four distinct processes – Router, Session Map, Distributor, and Node.

Different Grid Types

Selenium 4 supports three grid types:

  • Standalone Mode
  • Classical Grid (Hub and Node)
  • Fully Distributed (Router, Distributor, Session, and Node)

Docker Support

Grid 4 offers Docker support. The Docker integration does not use Unix domain sockets, so users need to make sure that the daemon is listening on port 2375.

When compared to Selenium Grid 3, this version is easier to use on virtual machines.

Components of Selenium Grid 4

Selenium Grid 4 Architecture

Source

 

  • Router – Listens to the new session request.
  • Distributor – Selects the appropriate node where the test should be executed.
  • Session Map – Maps the session ID to the node.
  • Node – Test machine where the test execution takes place.
  • New Session Queue – Handles all queue operations like adding to manipulate the queue. It has configurable parameters for setting the request timeout and request retry interval.

Advantages of Grid 4

Installing and Configuring Selenium Grid 4

Let’s first understand how to Download Selenium Grid 4. The latest version of Selenium 4 (Alpha) is 4.0.0-alpha-7 and the same can be downloaded from the official Selenium website. This article will demonstrate Selenium Grid 4’s functioning by using Selenium Grid version 4.0.0-alpha, as it is more stable than the other versions.

So download the selenium grid through this link and save it in the same folder where the webdriver is present.

Selenium Grid 4 can be set up in the following modes:

  • Standalone
  • Hub and Node
  • Distributed
  • Docker

1. Standalone

The new Selenium Server Jar contains all the functionalities needed to run a grid. By default, the server will be listening on http://localhost:4444 port – the URL to point RemoteWebDriver tests. The server will detect available drivers that it can use from the System PATH.

java -jar selenium-server-4.0.0-alpha-7.jar standalone

2. Hub and Node

Start the hub using the below command:

java -jar selenium-server-4.0.0-alpha-7.jar hub

Node registration can be done using the command below:

java -jar selenium-server-4.0.0-alpha-7.jar node --detect-drivers true

3. Distributed

Users can start Grid 4 in a fully distributed manner, with each conceptual piece in its own process. First, start the session map responsible for mapping session IDs to the node the session is running on.

java -jar selenium-server-4.0.0-alpha-1.jar sessions

Then the Distributor:

java -jar selenium-server-4.0.0-alpha-1.jar distributor --sessions http://localhost:5556

The Distributor is aware of all the Nodes and their capabilities. Its main role is to receive a new session request and find a suitable Node where the session can be created. After the session is created, the Distributor stores in the Session Map, the relation between the session id and Node where the session is being executed.

Now start the system with a router to expose it to the Web:

java -jar selenium-server-4.0.0-alpha-1.jar router --sessions http://localhost:5556 --distributor http://localhost:5553

This router will be listening to new session requests on

http://localhost:4444

Now, this is all set up, use this command:

curl http://localhost:4444/status

and it should get confirmed.

4. Using Docker

The docker integration doesn’t make use of UNIX domain sockets. So first, ensure the docker daemon is listening on port 2375 using the command below on the console:

socat -ls TCP-LISTEN:2375,reuseaddr,fork UNIX-CONNECT:/var/run/docker.sock

Note: If docker daemon is not listening to port 2375, then follow the below steps:

1. Create daemon.json file in /etc/docker

{"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]}

2. Add

 /etc/systemd/system/docker.service.d/override.conf
[Service] ExecStart= ExecStart=/usr/bin/dockerd

3. Reload the systemd daemon:

systemctl daemon-reload

4. Restart docker:

systemctl restart docker.service

Once that’s done, it’s easy to start the selenium server and have it delegate to docker for creating new instances:

java -jar selenium-server-4.0.0-alpha-1.jar standalone -D selenium/standalone-firefox:latest '{"browserName": "firefox"}' --detect-drivers false

Or just start a node:

java -jar selenium-server-4.0.0-alpha-1.jar node -D selenium/standalone-firefox:latest '{"browserName": "firefox"}'

The next steps from here remain the same with setting up the Grid. You can refer to the Selenium Grid Tutorial for the next steps.

Try Selenium Testing on Cloud Grid for Free

What’s the difference – Grid 3 vs Grid 4

Selenium Grid 3 comes up with only Hub node setup. To set up a grid on Selenium 3, the only option is the Hub-node setup. All machines should have a selenium-standalone jar downloaded as a hub to represent as a hub in the cluster and a node to represent as a node with separate commands.

Whereas, Selenium Grid 4 can be set up using the above-mentioned methods as it is diversified and comes up with additional features. The entire grid setup has been written from scratch with all modern tools and frameworks to support fully distributed testing and comes with extensive Docker support.

And that is how users can get started with Selenium Grid 4, in order to leverage its many advantages.

Teams can leverage cloud-based platforms like BrowserStack that offer a Cloud Selenium Grid of 2000+ real browsers and devices. It empowers teams to run concurrent Selenium tests on desired device-browser combinations online. This makes it convenient for QA engineers to perform comprehensive cross-browser and device testing across platforms. One can also integrate their test pipelines with CI/CD tools like Jenkins, Travis, Circle CI, etc.

Selenium automation seeks to reduce manual testing efforts, increase execution speed, and identify the maximum number of bugs at the earliest. However, in order to get the most out of their Selenium scripts, QAs must follow Selenium best practices. This will also help in establishing a reliable test cycle.

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

Selenium Grid Tutorial : How to Set It Up

Virtual Machine Browser for Cross-Browser Testing

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack