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

Handling Pop-ups with Selenium IDE and Java

Problem – Selenium Popup

You need to test an anchor whose target is _blank.

Example

http://www.htmlcodetutorial.com/linking/_A_TARGET_95y98y108y97y110y107y.html

<a href="newwindow.html" target="_blank">a new window</a>

Solution in IDE

  1. Open Firefox
  2. Click the Selenium icon to the right of the address bar
  3. Enter http://www.htmlcodetutorial.com/ as your Base URL
  4. Click the record button
    1. Navigate to http://www.htmlcodetutorial.com/linking/_A_TARGET_95y98y108y97y110y107y.html
    2. Left-click the a new window link
    3. Right-click on the page and select verifyTitle A Whole New Window – HTML Code Tutorial
    4. Close the new window/tab
    5. Right-click on the original window/tab page and select verifyTitle HTML _blank – HTML Code Tutorial
  5. In Selenium IDE, click the record button to stop recording
  6. Add a pause 1000 step after the click step to give the window/tab time to load
  7. Add a selectWindow A Whole New Window – HTML Code Tutorial step after the pause to connect to the new window/tab
  8. Add a close step after the verifyTitle A Whole New Window – HTML Code Tutorial step
  9. Add a selectWindow HTML _blank – HTML Code Tutorial step after the close step
  10. Select Play Current Test Case to verify the test

Your test should look like this:

Capture

Solution in Java

  1. Export the test from the IDE solution (above)
    • Click File -> Export Test Case As… and select Java/JUnit4/Webdriver
    • Save as test01.java in your project’s src folder
    • Refresh your project in Eclipse (F5)
    • Fix any errors in the code and save
      • Remove package com.example.tests;
  2.  Open Test01.java
    • Scroll down some and you will see some comments, like // ERROR: Caught exception [ERROR: Unsupported command [selectWindow | A Whole New Window – HTML Code Tutorial | ]]
    • Some of Selenium IDE’s functions don’t have WebDriver equivalents just yet
  3. Before the link click event add String parentHandle = driver.getWindowHandle();
    • This will get the parent window handle
  4. After the link click event add for (String winHandle : driver.getWindowHandles()) {driver.switchTo().window(winHandle);}
    • This will switch us to the child window
  5. After the close event add driver.switchTo().window(parentHandle);
    • This will switch us back to the parent window
  6. Right-click somewhere in the code file and select Run As -> JUnit Test
    • Or, click the green run button on the Eclipse toolbar
    • A browser window should open and run the test
    • The test results should display on the left pane of the Eclipse window

Your code for @test should look like:

@Test
public void test01() throws Exception {
	driver.get(baseUrl + "linking/_A_TARGET_95y98y108y97y110y107y.html");
	String parentHandle = driver.getWindowHandle();
	driver.findElement(By.linkText("a new window")).click();
	for (String winHandle : driver.getWindowHandles()) {
		driver.switchTo().window(winHandle);
	}
	try {
	  assertEquals("A Whole New Window - HTML Code Tutorial", driver.getTitle());
	} catch (Error e) {
	  verificationErrors.append(e.toString());
	}
	driver.close();
	driver.switchTo().window(parentHandle);
	try {
	  assertEquals("HTML _blank - HTML Code Tutorial", driver.getTitle());
	} catch (Error e) {
	  verificationErrors.append(e.toString());
	}
}

Setup Selenium, JUnit, and Eclipse

Update

This article is still relevant but a more comprehensive guide exists at http://timothycope.com/c-selenium-automation. This new guide also covers Page Object Design Pattern (POM) and Selenium Server (Grid).

Background

Selenium automates browsers. That’s it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.
http://docs.seleniumhq.org/

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
http://junit.org/

Eclipse is a platform that has been designed from the ground up for building integrated web and application development tooling. By design, the platform does not provide a great deal of end user functionality by itself. The value of the platform is what it encourages: rapid development of integrated features based on a plug-in model.
https://www.eclipse.org/

Setup Eclipse

  1. Go to https://github.com/junit-team/junit/wiki/Download-and-Install and download “junit.jar”
    • (Optional) You can also grab the “hamcrest-core.jar”
      • “junit.jar” contains common functions of hamcrest, which can be expanded by using the core library
  2. Go to http://www.seleniumhq.org/download/ and download “Selenium Server” and “Selenium Client”
    • We will be using the Java client, which comes in an archive. We are only concerned with the .jar not including “srcs” in the file name
  3. Go to https://www.eclipse.org/downloads/ and download the version you need for your operating system
    • I use Eclipse IDE for Java Developers
  4. Extract the archive
    • (Optional) Move the extracted folder to your application folder
  5. Run the executable for Eclipse
  6. Select a workspace
  7. Click File -> New -> Java Project
    • For standard IDE Find/Select Java Project
  8. Name your project and click Finish
    • The default settings are good for this tutorial
  9. Create a new folder called “lib” in your project to hold the external assemblies
    • The name does not matter, but should make sense
  10. Drag-and-Drop the .jar files for Selenium Server, Selenium Client, and JUnit into the new folder
    • When prompted, select Copy Files
  11. Expand the lib folder and select the .jar files
  12. Right-click and select Build Path -> Add to Build Path
    • This will create a new folder called “Referenced Libraries” that contains the binaries for the .jar files

Creating Your First Test

Until you become a Subject Matter Expert you will likely be using the Selenium IDE for Firefox to write your tests. You can download the IDE from http://www.seleniumhq.org/download/

  1. Open Firefox
  2. Click the Selenium icon to the right of the address bar
  3. Enter https://www.google.com/ as your Base URL
  4. Click the record button
    1. Navigate to https://www.google.com/
    2. Type Tim
    3. Click Search button
    4. Right-click on the page and select waitForTitle Tim – Google Search from the context menu
    5. Click the record button to stop recording
  5. Click the Play Current Test button
    • Ensure all steps are highlighted in green, meaning the test passed
  6. Click File -> Export Test Case As… and select Java/JUnit4/Webdriver
  7. Save as test01.java in your project’s src folder
  8. Refresh your project in Eclipse (F5)
  9. Fix any errors in the code and save
    • Remove package com.example.tests;
    • Rename public class Test01 to test01
  10. Right-click somewhere in the code file and select Run As -> JUnit Test
    • Or, click the green run button on the Eclipse toolbar
  11. A browser window should open and run the test
  12. The test results should display on the left pane of the Eclipse window