Selenium Grid

Selenium Grid is a smart proxy server
that allows Selenium tests to route commands to remote web browser instances.
Its aim is to provide an easy way to run tests in parallel on multiple machines.

With Selenium Grid,
one server acts as the hub that routes JSON formatted test commands
to one or more registered Grid nodes.
Tests contact the hub to obtain access to remote browser instances.
The hub has a list of registered servers that it provides access to,
and allows us to control these instances.

Selenium Grid allows us to run tests in parallel on multiple machines,
and to manage different browser versions and browser configurations centrally
(instead of in each individual test).

Selenium Grid isn't a silver bullet.
It solves a subset of common delegation and distribution problems,
but will for example not manage your infrastructure
and might not suit your specific needs.

Purposes and main functionality of a Selenium Grid

Central entry point for all tests

Management and control of the nodes / environment where the browsers run on

Scaling

Running tests in parallel

Cross platform testing

Load balancing

Components of a Selenium Grid

Hub

Intermediary and manager

Accepts requests to run tests

Takes instructions from client and executes them remotely on the nodes

Manages threads

A hub is a central point where all your tests are sent to.
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.

Nodes

This is where the browsers live

Registers itself to the hub and commincates its capabilities

Receives requests from the hub and executes them

Nodes are different Selenium instances
that will execute tests on individual computer systems.
There can be many nodes in a grid.
The machines which are nodes need do not need to be the same platform
or have the same browser selection as that of the hub or the other nodes.
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.

Setting up your own Selenium Grid

To use Selenium Grid,
you need to maintain your own infrastructure for the nodes.
As this can be a cumbersome and time intense effort,
many organizations use IaaS providers
such as Amazon EC2 and Google Compute
to provide this infrastructure.

Other options include using providers such as Sauce Labs or Testing Bot who provide a Selenium Grid as a service in the cloud.
It is certainly possible to also run nodes on your own hardware.
This chapter will go into detail about the option of running your own grid,
complete with its own node infrastructure.

Quick start

This example will show you how to start the Selenium 2 Grid Hub,
and register both a WebDriver node and a Selenium 1 RC legacy node.
We’ll also show you how to call the grid from Java.
The hub and nodes are shown here running on the same machine,
but of course you can copy the selenium-server-standalone to multiple machines.

The selenium-server-standalone package includes the hub,
WebDriver, and legacy RC needed to run the grid.
ant is not required anymore.
You can download the selenium-server-standalone-.jar from
http://www.seleniumhq.org/download/.

Step 1: Start the hub

The hub is the central point that will receive test requests
and distribute them to the right nodes.
The distribution is done on a capabilities basis,
meaning a test requiring a set of capabilities
will only be distributed to nodes offering that set or subset of capabilities.

Because a test's desired capabilities are just what the name implies, desired,
the hub cannot guarantee that it will locate a node
fully matching the requested desired capabilities set.

Open a command prompt
and navigate to the directory where you copied
the selenium-server-standalone.jar file.
You start the hub by passing the -role hub flag
to the standalone server:

To change the default port,
you can add the optional -port flag
with an integer representing the port to listen to when you run the command.
Also, all of the other options you see in the JSON config file (seen below)
are possible command-line flags.

You certainly can get by with only the simple command shown above,
but if you need more advanced configuration,
then you may also for convenience specify a JSON format config file
to configure the hub when you start it.
You can do it like so:

Step 2: Start the nodes

Regardless of whether you want to run a grid with new WebDriver functionality,
or a grid with Selenium 1 RC functionality,
or both at the same time,
you use the same selenium-server-standalone.jar file to start the nodes:

If a port is not specified through the -port flag,
a free port will be chosen. You can run multiple nodes on one machine
but if you do so, you need to be aware of your systems memory resources
and problems with screenshots if your tests take them.

Configuration of node with options

As mentioned, for backwards compatibility
“wd” and “rc” roles are still a valid subset of the “node” role.
But those roles limit the types of remote connections to their corresponding API,
while “node” allows both RC and WebDriver remote connections.

Passing JVM properties (using the -D flag
before the -jar argument)
on the command line as well,
and these will be picked up and propagated to the nodes:

-Dwebdriver.chrome.driver=chromedriver.exe

Configuration of node with JSON

You can also start grid nodes that are configured
with a JSON configuration file

A note about the -host flag

For both hub and node, if the -host flag is not specified,
0.0.0.0 will be used by default. This will bind to all the
public (non-loopback) IPv4 interfaces of the machine. If you have a special
network configuration or any component that creates extra network interfaces,
it is advised to set the -host flag with a value that allows the
hub/node to be reachable from a different machine.

Docker Selenium

Docker provides a convenient way to
provision and scale Selenium Grid infrastructure in a unit known as a container. Containers are standardised units of software that contain everything required
to run the desired application including all dependencies in a reliable and repeatable
way on different machines.
The Selenium project maintains a set of Docker images which you can download
and run to get a working grid up and running quickly. Nodes are available for
both Firefox and Chrome. Full details of how to provision a grid can be found
within the Docker Selenium
repository.

Prerequisite

The only requirement to run a grid is to have Docker installed and working.
Install Docker