How To Use Selenium Grid
The Problem
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 like Sauce Labs or Browser Stack 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 Selenium Grid primer
Selenium Grid lets you distribute test execution across several machines, and you connect to it using Selenium Remote -- specifying the browser type, version, platform, etc. through the use of Selenium Remote and it's Capabilities
.
There are two main elements to Selenium Grid -- a hub, and nodes. You first need to stand up a hub. You can then register nodes to it.
Nodes are where your tests will run, and the hub is responsible for making sure your tests end up on the right one.
An Example
Part 1: Grid Setup
Selenium Grid comes baked into the Selenium Standalone Server. So to get started we'll need to grab the latest version from here.
Once we have it we can start the hub.
java -jar selenium-server-standalone-2.42.0.jar -role hub
After that we can register nodes to it.
java -jar selenium-server-standalone-2.42.0.jar -role node -hub http://localhost:4444/grid/register
NOTE: This example only demonstrates a single node on the same machine as the hub. To span nodes across multiple machines you will need to place the standalone server on each machine and launch it with the same registration command (replacinghttp://localhost
with the location of your hub, and specifying additional parameters as needed).
Now that the grid is running we can view the available browsers by visiting our Grid's console at http://localhost:4444/grid/console
.
To refine the list of available browsers, we can specify an additional-browser
parameter when registering the node. For instance, if we wanted to just offer safari on a node, we could specify it with -browser browserName=safari
, which would look like this:
java -jar selenium-server-standalone-2.42.0.jar -role node -browser browserName=safari -hub http://localhost:4444/grid/register
We could also repeat this parameter again if we wanted to explicitly specify more than one browser.
java -jar selenium-server-standalone-2.42.0.jar -role node -browser browserName=safari -browser browserName=chrome -browser browserName=firefox -hub http://localhost:4444/grid/register
There are numerous parameters that we can use at run time. You can see a full list here.
Part 2: Test Setup
Let's wire up a simple test script to use our new Grid.
First, we'll need to require our necessary libraries and wire up setup
,teardown
, and run
methods. After that it's just a simple matter of adding in some Selenium commands and an assertion to make sure the browser is doing what we expect.
# filename: grid.rb
require 'selenium-webdriver'
require 'rspec-expectations'
include RSpec::Matchers
def setup
@driver = Selenium::WebDriver.for(
:remote,
url: 'http://localhost:4444/wd/hub',
desired_capabilities: :firefox) # you can also use :chrome, :safari, etc.
end
def teardown
@driver.quit
end
def run
setup
yield
teardown
end
run do
@driver.get 'http://the-internet.herokuapp.com'
expect(@driver.title).to eq('The Internet')
end
The thing to note in this configuration is that we're using Selenium Remote (e.g., Selenium::WebDriver.for(:remote,
) to run against the grid. And we are telling the grid which browser we want through thedesired_capabilities
(e.g., :firefox
).
An alternative way to configure this would be to create a Selenium Remote Capabilities object for the browser we want, modify it as needed, and pass that it to desired_capabilities
.
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps[:platform] = :mac # you can also use :any, :win, or :x
@driver = Selenium::WebDriver.for(
:remote,
url: 'http://localhost:4444/wd/hub',
desired_capabilities: caps)
You can see a full list of the available Selenium Remote Capabilities here.
When we run this script (ruby grid.rb
) then the test should run using Chrome through the grid.
Expected Behavior
- The hub will determine which node has the necessary browser/platform combination
- A browser will open on that node
- The test will run using browser and close when finished
Outro
If you're looking to set up Selenium Grid to work with Internet Explorer or Chrome, be sure to read up on how to set them up since there is some additional configuration required for each. And if you run into issues with other browsers, be sure to check out their driver documentation as well:
Lastly, 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 however many connections you throw at it, but you will still need to find a way to execute your tests in parallel. You can see some previous write-ups on how to accomplish thishere.
Happy Testing!