• Background Image

    Introduction To Selenium Grid: Text Version

    January 15, 2018

January 15, 2018

Introduction To Selenium Grid: Text Version

In this lesson, we will take a closer look at how you can use a Selenium Grid to make executing your tests at scale easier.

What Is Selenium Grid?

Selenium Grid can be used to run Selenium tests parallel and on one or multiple machines. This can help with:

  • Reducing the overall time of test execution
  • Running tests on browsers on different operating systems
  • Creating a dedicated test rig
  • Executing tests as part of the build process

How does it work?

Selenium Grid works a little bit different than executing tests on the local machine. While you can use the same testing code to execute tests against a Selenium Grid you will need to use the RemoteDriver instead of the browser specific drivers like FirefoxDriver or ChromeDriver.

This remote driver will execute the tests against a Selenium Hub which automatically delegates the test execution to one or more Selenium Nodes. These Selenium Nodes can support different browsers and can be run on different operating systems so that you can execute tests against browsers on Windows, macOS, and Linux with the same testing code.

Selenium Grid

Setup a Selenium Hub

Running a Selenium Hub is quite simple, all you need to do is follow these 4 steps.

  • 1. Download Java
  • 2. Download Selenium Standalone Server at SeleniumHQ
  • 3. Start the Hub with the command line:
    java -jar selenium-server-standalone.jar -role hub
  • 4. Grid console available at http://localhost:4444/grid/console

Now a Selenium Hub without any Nodes is not that useful so we will need to set up at least one Selenium Node. Keep in mind that the Hub and Nodes can run on different machines.

Setup a Selenium Node

To run a Selenium Node you first need to create a configuration file that tells Selenium which browsers this node can run tests in. The configuration is a simple JSON file an looks something like this:

{
	"capabilities": [{
		"browserName": "firefox",
		"platform": "WINDOWS",
		"maxInstances": 1
	}],
	"maxSession": 5,
	"port": 5555,
	"register": true
}

Now we can start our Selenium Node with this command line (node.json is our configuration file):

java -jar selenium-server-standalone.jar -role node -hub http://localhost:4444/grid/register -nodeConfig node.json

If your Selenium Hub is running on a different machine simply change localhost:4444 to the correct hostname and port.

Running A Test

To run the above test against the Selenium Grid we only need to change the driver object, all the other test code will stay exactly the same.

  • [java]
    URL gridUrl = new URL("http://localhost:4444/wd/hub");

    // You can select another browser by changing the options
    Capabilities capabilities = new FirefoxOptions();

    // Launches a new Firefox instance on the TestGrid server
    WebDriver driver = new RemoteWebDriver(gridUrl, capabilities);

    driver.navigate().to("http://selenium.academy");

    driver.quit();
    [/java]

  • [csharp]
    Uri gridUrl = new Uri("http://localhost:4444/wd/hub");

    // You can select another browser by changing the DesiredCapabilities
    ICapabilities capabilities = new FirefoxOptions().ToCapabilities();

    // Launches a new Firefox instance on the TestGrid server
    IWebDriver driver = new RemoteWebDriver(gridUrl, capabilities);

    driver.Quit();
    [/csharp]

  • [ruby]
    gridUrl = "http://localhost:4444/wd/hub";

    # You can select another browser by changing the Capabilities
    capabilities = Selenium::WebDriver::Remote::Capabilities.firefox();

    # Launches a new Firefox instance on the TestGrid server
    driver = Selenium::WebDriver.for(:remote, :url =>gridUrl, :desired_capabilities => capabilities)
    [/ruby]

  • [python]
    gridUrl = ‘http://localhost:4444/wd/hub’

    # You can select another browser by changing the DesiredCapabilities
    capabilities = webdriver.DesiredCapabilities.FIREFOX

    # Launches a new Firefox instance on the TestGrid server
    driver = webdriver.Remote(command_executor = gridUrl, desired_capabilities = capabilities)
    [/python]

  • [javascript]
    var gridUrl = ‘http://localhost:4444/wd/hub’;

    // You can select another browser by changing the DesiredCapabilities
    var capabilities = webdriver.Capabilities.firefox();

    // Launches a new Firefox instance on the TestGrid server
    var driver = new webdriver.Builder().withCapabilities(capabilities)
    .usingServer(gridUrl)
    .build();
    [/javascript]

This test case will execute against the Selenium Grid. Of course, you need to modify the URL if your Grid runs on a separate machine. To run the test against different browsers simply change the capabilities object as necessary.

Third-Party Ressources

In many cases, it is not worth the trouble to go through the above process yourself to set everything up. Many third-party solutions out there can provide you with an easy to use Selenium Grid. Normally nearly no changes to your test code are necessary.

SAAS Selenium Grid Provider

The companies below can provide you with access to their Selenium Grid in the cloud:

On-Premise Selenium Grid Provider

Aside from the SAAS solutions for running a Selenium Grid there is also a tool called BrowseEmAll which makes the setup of a local Selenium Grid much easier. If you need a local Selenium Grid but want to avoid the maintenance and setup work check it out.

Next Steps

In the next lesson you will learn how to use the Watir framework in your Ruby tests.