Home » Blog » Run Selenium Tests on Raspberry Pi Headless via Selenium Grid
Running Selenium Tests on Raspberry Pi

Run Selenium Tests on Raspberry Pi Headless via Selenium Grid

Before getting into the actual topic let’s begin with the obvious question first. What are the advantages of using RemoteWebDriver over WebDriver? Well, the main benefit of using RemoteWebdriver over Webdriver is that we can perform execution of automation suites/batches on multiple machines at same time. However, deploying multiple medium configuration PC’s for the sole purpose of executing selenium tests can be quite expensive if you look at the ROI (System procurement costs + Cost of electricity / CPU utilization). What if we could replace these costly nodes with cheap SBC’s like Raspberry Pi which requires very little power to operate and are low on maintenance. Let’s get started with the same.

Getting Started With Raspberry Pi

In order for the Raspberry Pi to run Selenium tests we need to do some little configuration before its ready to be deployed as a node. The first thing that we need to do is install Ubuntu Mate on Raspberry Pi 3. It is available on the official site at ubuntu-mate.org. Once the download is completed you’ll need to unzip the file so that you get an .img file. Use a tool like Win32DiskImager or Etcher to write the image to your microSD card. These steps are similar to how you would burn your Raspbian image to your microSD card.

Unlike Raspbian you wont be able to SSH into the OS on first boot simply by placing a file named “ssh” onto the boot partition of the SD card. The problem with a Ubuntu MATE is that it requires you to have a monitor, keyboard and mouse for the first boot — as the graphical installer (ubiquity) awaits to complete the initial process (language, keyboard, user account, then probably the SSH key generations). In order to configure SSH on Ubuntu Mate please follow the steps here: https://www.raspinews.com/enable-ssh-on-boot-raspberry-pi-ubuntu-mate

ubuntu mate ssh boot

Prerequisites on Ubuntu Mate

  1. Java: The first thing that we need is Java installed in our Raspberry Pi. Ubuntu Mate ships with a default open JRE installed which is good enough for running Selenium tests. However, if you want to install the latest version of Oracle JDK/JRE  you can do so by following the step here: https://www.raspinews.com/installing-oracle-java-jdk-8-on-raspberry-pi
  2. Selenium Jar: Next we need to download the selenium standalone server jar file that will spin up the node of our selenium to connect/register to a selenium hub server running on the host machine. You can download the jar using wget.
    sudo wget https://selenium-release.storage.googleapis.com/3.4/selenium-server-standalone-3.4.0.jar
  3. GeckoDriver: GeckoDriver acts like the link between your tests in Selenium and Gecko-based browsers i.e. Iceweasel in our case. Since we are running Ubuntu on Raspberry Pi (ARMv8) we need to download packages for ARM CPU Architecture.
    Download the GeckoDriver using wget command: sudo wget https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.18.0-arm7hf.tar.gz Extract the file with: tar -xvzf geckodriver-v0.18.0-arm7hf.tar.gzMove the geckodriver to /usr/local/bin/: sudo mv geckodriver /usr/local/bin/
  4. Iceweasel: On Debian systems (including Debian and Raspbian on the Raspberry Pi), the implementation of Firefox is rebranded as “Iceweasel”. To install it type the following command in the terminal: sudo apt-get install iceweasel Answer with a positive response if asked about authorization. Enter “Y” when asked about disk space.
  5. XVFB: Finally we require something called xvfb or otherwise called as the X virtual framebuffer. In contrast to other display servers, Xvfb performs all graphical operations in virtual memory without showing any screen output. From the point of view of the client, it acts exactly like any other X display server, serving requests and sending events and errors as appropriate. However, no output is shown. It’s more like running a graphical operation in a headless environment where you really don’t need to have your raspberry pi connected to your monitor or TV so you can run your test even without having a display device connected to your Raspberry Pi. To install it type the following command in the terminal: sudo apt-get install xvfb

 

ubuntu mate selenium setup
Selenium Jars and GeckoDriver Setup

 

iceweasel setup
Iceweasel Setup
xvfb setup
XVFB Setup

Configuration

Selenium Grid config

Once you’re done with all the prerequisites, the next step is to configure the Pi and the Host system to enable running Selenium tests on the Pi.

  1. The first step is to make the Windows 10 system act as hub so that we can use it to register our nodes (Pi). Download the latest standalone selenium jar from here. Once you have downloaded the jar file type the following command in Windows 10 command prompt: java -jar selenium-server-standalone-3.4.0.jar -role hub hub Selenium Grid Hub windowsIf successful you should be able to see that the Selenium Grid Hub is up and running. It will also display the URL that we should be using to register our nodes.
  2. Next we need to configure the Pi as a node by registering it with the the selenium server which is up and running in the Windows 10 system. Also, this is where we would be using the XVFB to run the jar application in a virtual display driver.  Type the following command in Pi terminal: Display=:1 xvfb-run java -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub http://192.168.1.2:4444/grid/register/ Selenium Grid Raspberry Pi

If successful you should see node registered message in Windows 10 command prompt (Registered a node http://192.168.1.5:5555). node registered raspberry pi

Sample Selenium Grid Eclipse Project

For this tutorial I will be using the latest version of Eclipse which is Eclipse oxygen. You can download the IDE from here. Next create a new “Java Project” and add the required external Jar files for this tutorial. client-combined-3.4.0-nodeps.jarselenium-server-standalone-3.4.0.jar. The test will perform a Google search for a specified keyword and display the time required to render the search results.

External Jars SeleniumPi

Java source code for this tutorial:
package org.test;

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SeleniumPiTest {

  public static RemoteWebDriver driver;

  public static void main(String[] args) {
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    System.setProperty("webdriver.gecko.driver", "/usr/local/bin/geckodriver");
    capabilities.setCapability("marionette", false);
    capabilities.setPlatform(Platform.LINUX);
    capabilities.setBrowserName("firefox");
    try {
      driver = new RemoteWebDriver(capabilities);
      driver.get("https://www.google.com");
      driver.findElement(By.id("lst-ib")).sendKeys("Selenium Pi");
      driver.findElement(By.name("btnG")).click();
      Thread.sleep(5000);
      WebElement resultsec = (new WebDriverWait(driver, 15))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));
      String resultStats = resultsec.getText();
      System.out.println("Browser title: " + driver.getTitle());
      System.out.println("Google result stats: " + resultStats);
    } catch (Exception e) {
      System.out.println(e.getMessage());
    } finally {
      driver.quit();
    }
  }
}
Execution:

Right click on SeleniumPiTest.java file in the Package Explorer and select Run As > Java Application. This will launch the selected class as a local Java application. However the actual selenium test will be performed on Raspberry Pi itself and not on the host machine. The output of the test can be viewed in the Eclipse console.