Working with Selenium 3 Grid

Selenium 3 Grid

Selenium-Grid allows you run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.
http://www.seleniumhq.org/docs/07_selenium_grid.jsp#what-is-selenium-grid

Getting the Necessary Files

This post will cover a Selenium 3 Grid setup with a Windows hub and an OSX Safari node. I highly recommend having a readme.txt file to each folder so that you can record the version number and URL for each file. If you change the file names you are going to have a bad time.

Windows

For your convenience I have uploaded a .zip of the necessary files for Windows. Download Link: Selenium_Windows.zip

Download the following files and place them in C:\Selenium

Set Your Path File

  1. Right-click “This PC” and select “Properties”
  2. In the left navigation menu, select “Advanced system settings”
  3. Select the “Advanced” tab
  4. Select “Environment Variables…”
  5. Under “System variables” select “Path” and then select “Edit…”
  6. Select “New”
  7. Enter C:\Selenium\
  8. Select “OK”

OSX

Download the following files and place them in ~/Documents/Selenium

Enable Automation Playback in Safari

  1. Ensure that the Develop menu is available.
    • It can be turned on by opening Safari preferences (Safari > Preferences in the menu bar), going to the Advanced tab, and ensuring that the Show Develop menu in menu bar checkbox is checked.
  2. Enable Remote Automation in the Develop menu.
    • This is toggled via Develop > Allow Remote Automation in the menu bar.
  3. Authorize safaridriver to launch the webdriverd service which hosts the local web server.
    • To permit this, run /usr/bin/safaridriver once manually and complete the authentication prompt.

Creating the Necessary Scripts

Windows – Start Grid Hub

Create a new file called start-grid-hub.bat and paste in the following:

cd C:\Selenium
java -jar selenium-server-standalone-3.3.1.jar -role hub

Note: Be sure to replace 3.3.1 with the current version number.

Windows – Start Grid Node

Create a new file called start-grid-node.bat and paste in the following:

cd C:\Selenium
java -Dwebdriver.chrome.driver=C:\Selenium\chromedriver.exe -Dwebdriver.gecko.driver=C:\Selenium\geckodriver.exe -Dwebdriver.edge.driver=C:\Selenium\MicrosoftWebDriver.exe -jar selenium-server-standalone-3.3.1.jar -role node -hub http://localhost:4444/grid/register

Note: Be sure to replace 3.3.1 with the current version number and localhost with your hub’s machine name or IP.

OSX – Start Grid Node

Create a new file called start-grid-node.sh and paste in the following:

#!/bin/bash
cd ~/Documents/Selenium/
java -jar selenium-server-standalone-3.3.1.jar -browser browserName=safari,platform=MAC -role node -hub http://localhost:4444/grid/register

Note: Be sure to replace 3.3.1 with the current version number and localhost with your hub’s machine name or IP.

WebDriver Setup

To create a driver that uses the remote instance of Safari add the following to your code:

DesiredCapabilities capabilities = DesiredCapabilities.Safari();
capabilities.SetCapability(CapabilityType.BrowserName, "safari");
capabilities.SetCapability(CapabilityType.Platform, "MAC");
driver = new RemoteWebDriver(hubUri, capabilities);

Note: The code (above) is C# so you may have to massage it a bit to work in other languages.

Running the Test

  1. On the Windows machine, run start-grid-hub.bat
  2. On the OSX machine:
    1. Open Terminal
    2. Drag-and-drop the start-grid-node.sh file into the Terminal window and then press [Enter]
  3. On the Windows machine, run your test(s)

Fix webdriver.chrome.driver system property Error

Background – webdriver.chrome.driver system property

I was following along Selenium’s quick-start guide but kept getting an error, “The path to the driver executable must be set by the webdriver.chrome.driver system property“. Most of the answers I found were for Java and simply set the value in code. I tried a similar fix in C# to no avail. Finally, I found a solution that works that sets the values as command-line parameters when I launch the Node.

Files

All files were placed in C:\Selenium. An entry was added to my Windows PATH variable by:

  1. Right-click “This PC” and select “Properties”
  2. In the left navigation menu, select “Advanced system settings”
  3. Select the “Advanced” tab
  4. Select “Environment Variables…”
  5. Under “System variables” select “Path” and then select “Edit…”
  6. Select “New”
  7. Enter C:\Selenium\
  8. Select “OK”

Start the Hub

The following script will start the Grid Hub (on the default port of 4444).

cd C:\Selenium
java -jar selenium-server-standalone-3.0.0.jar -role hub

Start the Node

The following script will start a Grid Node (on the default port of 5555). It will also register the locations of the Chrome and Firefox WebDrivers (respectively).

cd C:\Selenium
java -Dwebdriver.chrome.driver=C:\Selenium\chromedriver.exe -Dwebdriver.gecko.driver=C:\Selenium\geckodriver.exe -jar selenium-server-standalone-3.0.0.jar -role node -hub http://localhost:4444/grid/register