Intro to Selenium Grid

NOVEMBER 10, 2015 // By Troy Walsh

What is Grid?

Grid, otherwise known as the Selenium standalone server, is a jar file. The grid jar can be run as a hub or node. Nodes register with hubs. Registering lets the hubs know what OS, browsers and browser versions are available on the registering node. Test code uses the remote web driver to connect to the hub. At run time the hub matches a remote driver with a node. If all appropriate nodes are unavailable the hub places the request in a queue until an appropriate node is available.

Setup

Before you can use grid you will need to assure Java is installed on your hub and node machines. You will also need to download the grid jar file. There are two primary ways to configure hubs and nodes. The first is via command line. This works well for simple configurations. The second option is via JSON. The JSON and command line configurations can do the same thing, but JSON is much easier to work with when you are dealing with a large number of options.

Hub Setup

I typically suggest using a simple command line setup for the hub. The only command line option you need to set is the role. There are several other options, but I will not be covering them in this post. 

Example command line:
java -jar selenium-server-standalone-2.48.2.jar -role hub

Node Setup

For the node setup I typically suggest using a JSON configuration. The only things you need to provide the node is the role and the path to the hub. When using the JSON configuration you will also have to pass in the configuration file. Once again there are a lot of different options and I will only be covering a subset of them in this post.

Example command line:

java -jar selenium-server-standalone-2.48.2.jar -role node -nodeConfig nodeSettings.json

Example JSON:

{
"capabilities":
  [
    {
      "browserName": "chrome",
      "maxInstances": 1
    },
    {
      "browserName": "firefox",
      "maxInstances": 1,
      "firefox_binary": "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"
    },
    {
      "browserName": "internet explorer",
      "version": "11",
      "maxInstances": 1
    },
    {
      "browserName": "MicrosoftEdge",
      "maxInstances": 1
    }
  ],
"configuration":
    {
    "nodeTimeout":300000,
    "hubPort":4444,
    "hubHost":"HUB_HOST_MACHINE",
    "nodePolling":2000,
    "registerCycle":10000,
    "maxSession": 1,
    "register":true,
    "cleanUpCycle":3000000,
    "timeout":300000,
    "maxSession":1,
    "Dwebdriver.chrome.driver=C:\\GridSetup\\chromedriver.exe": "",
    "Dwebdriver.ie.driver=C:\\GridSetup\\IEDriverServer.exe": "",
    "Dwebdriver.edge.driver=C:\\Program Files (x86)\\Microsoft Web Driver\\MicrosoftWebDriver.exe": ""
    }
}

Using Grid From Code

In order to use grid you will need to use the remote web driver. The two pieces of data you will need to provide the remote web driver is the path to the hub and desired browser. You can also define what OS and browser version you want, but that it outside the scope of this post.

Example Code:

WebDriver driver = new RemoteWebDriver(new 
Uri("http://HUB_HOST_MACHINE:4444/wd/hub"), DesiredCapabilities.FirefoxDriver());

Streamlining Your Setup

There are several ways to streamline the setup. My preferred method is to create startup batch or bash scripts. This way all you need to do is startup your hub and node machines and you are up and running.

Common Buildouts

Basic Off-Loading

Allow test engineers to run their browser tests on a remote machine

 alt=

Full Buildout

Hub with multiple available nodes

Selenium diagram 2

Enterprise Buildout

Farm configuration where multiple hubs are available – In a later post I will discuss hub matchmaking

Selenium diagram 3

Doing More With Grid - Parallelization

Once you have Grid setup you can utilize parallelization. This can come in many forms. You can have a shared Grid farm that multiple test engineers use. You can also pragmatically run your tests in parallel, many modern test runners support this. Grid also allows you to run tests in parallel on individual nodes, but not all browsers and machines handle parallel exaction on the same machine very well. Parallelization required more forethought than running test in a serial fashion, but when done right it can really cut down your execution time.

Closing Thoughts And Notes

  • Tests run slower on grid than they do locally.
  • There is no hard and fast rule about how many nodes a hub can manage, but if you add too many nodes the hub will slow down or stop working altogether.
  • Running tests in parallel usually cuts down overall execution time, but you often see diminishing returns. Put another way, running 4 test in parallel is usually quicker than running 2, but it won't cut you execution time in half.
  • Parallelization is great, but you need to make sure your tests are written in such a way that they can be run in parallel.
  • Only web driver interactions happen on the nodes. All none Selenium actions, such as reading files, happens on the machine executing the test code.
  • IE requires some setup to work with Selenium, you will need to do this on all IE nodes.
  • Grid can take browser screen shots. IE will only save a black screen if no one is actively connected to the node machine.
  • Grid does not know about every OS version.
    Examples: Windows 7 comes up as Vista and Windows 10 as Windows 8.1.

Troy Walsh is a Principal Consultant of QA at Magenic. This post is republished from his blog and can be found here. If you’d like to contact Magenic directly, email us or call us at 877-277-1044.

Categories // Quality Assurance & Testing, Custom Application Development
Tags // Selenium, Grid

Get Started

Contact Us