Skip to main content

How to Use Selenium Grid

Intro​

If you're looking to run your tests on different browser and operating system combinations, but you're unable to justify using a third-party solution like Sauce Labs then what do you do?

A Solution​

With Selenium Grid you can stand up a simple infrastructure of various browsers on different operating systems to not only distribute test load, but also give you a diversity of browsers to work with.

A brief primer of Selenium GridSelenium Grid is part of the Selenium project. It lets you distribute test execution across several machines. You can connect to it with a remote WebDriver by specifying the browser, browser version, and operating system you want. You specify these values through Selenium Capabilities.There are two main elements to Selenium Grid -- a Hub, and Nodes. First you need to stand up a Hub. Then you can connect (or "register") Nodes to that Hub. Nodes are where your tests will run, and the Hub is responsible for making sure your tests end up on the right one (e.g., the machine with the operating system and browser you specified in your test).

Let's continue with an example.

Example​

Part 1: Grid Setup​

Selenium Grid comes built into the Selenium Server. So to get started we'll need to download the latest version of it from the Selenium releases (it requires Java).

The quickest way to get a Grid running on a single machine is the standalone mode, which starts a Hub and a Node together:

> java -jar selenium-server-<version>.jar standalone
...
INFO [Standalone.execute] - Started Selenium Standalone <version>: http://<your ip>:4444

To spread the load across several machines, start a Hub first:

> java -jar selenium-server-<version>.jar hub
...
INFO [Hub.execute] - Started Selenium Hub <version>: http://<your ip>:4444

And then register Nodes to it (running this command on each machine, with the address of the Hub):

> java -jar selenium-server-<version>.jar node --hub http://<hub address>:4444
...
INFO [NodeServer.execute] - Started Selenium node <version>: http://<your ip>:5555

Each Node detects the browsers installed on its machine, and Selenium Manager takes care of the browser drivers. Once the Grid is running, we can see the available browsers and the running sessions in the Grid UI, at http://localhost:4444/ui.

To refine the list of available browsers, we can specify an additional --driver-implementation parameter when starting a Node. For instance, if we wanted to only offer Safari on a Node, we could specify it with --driver-implementation safari. We could also repeat this parameter if we wanted to specify more than one browser.

java -jar selenium-server-<version>.jar node --hub http://<hub address>:4444 --driver-implementation safari --driver-implementation chrome

There are numerous parameters that we can use at run time. You can see a full list at the Selenium Grid documentation.

Part 2: Test Setup​

To use the Grid, our tests create a Remote WebDriver pointed at the Grid's address (http://localhost:4444 by default, or the GRID_URL environment variable), with the options of the browser we want (Firefox in these examples). The rest of the test is the same as when running locally.

If the Grid is not running, the tests are skipped.

Expected Behavior​

  • Connect to the Grid
  • The Grid determines which Node has the necessary browser/platform combination
  • The Grid connects the test to the Node
  • Browser opens on the Node
  • Test runs
  • Browser closes on the Node

Summary​

If you run into issues, be sure to check out the browser driver documentation for the browser you're working with:

Also, it's worth noting that while Selenium Grid is a great option for scaling your test infrastructure, it by itself will NOT give you parallelization. That is to say, it can handle as many connections as you throw at it (within reason), but you will still need to find a way to execute your tests in parallel.

Happy Testing!

Special Thanks​

Roman Isko
Contributed the initial Java code for this tip.

Authors​

Dave Piacente
Original creator of Elemental Selenium
Diego Molina
Elemental Selenium maintainer