Home » Blog » Running Selenium Tests on Raspberry Pi – Chromium, Java, TestNG
Running-Selenium-Tests-on-Raspberry-Pi-Chromium-Java-XVFB

Running Selenium Tests on Raspberry Pi – Chromium, Java, TestNG

In my previous post I have written about how you can run Selenium tests using Firefox and XVFB. While there is nothing wrong with running tests in Firefox, I found that performance was quite sluggish with average execution time of 5x when compared to a standard Intel Core i3 machine. Since the only other alternative possible at the time of writing this article was Chromium, I started by browsing various forums and finally found a way to run Selenium tests on you R.Pi using Java and TestNG. I also managed to retrieve the screenshots via RemoteWebDriver to the host machine executing the tests. Please follow the below steps to get started.

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-raspberry-pi-3

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 http://selenium-release.storage.googleapis.com/3.5/selenium-server-standalone-3.5.2.jar
  3. Chromium Browser: To install Chromium Browser type the following command in the terminal: sudo apt-get install chromium-browser. Answer with a positive response if asked about authorization. Enter “Y” when asked about disk space.
  4. 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

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.5.2.jar -role hub selenium-grid=hub-352If 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. Important: We need to specify the “webdriver.chrome.driver” path location along with XVFB. Setting the path via System.setProperty() does not work.  Type the following command in Pi terminal: Display=:1 xvfb-run java -Dwebdriver.chrome.driver="/usr/lib/chromium-browser/chromedriver" -jar selenium-server-standalone-3.5.2.jar -role webdriver -hub http://192.168.1.2:4444/grid/register/
  3. hub register chromium

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

selenium-grid-hub-352-registered

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: selenium-server-standalone-3.5.2.jar. Apart from this you will also need to setup TestNG. Installation instruction for TestNG can be found here: http://testng.org/doc/eclipse.html

The test will perform a Google search for a specified keyword and display the time required to render the search results.

Java-setup-raspberry-pi

 

Java source code for this tutorial:

package org.test;

import org.testng.annotations.Test;
import java.io.File;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SeleniumPiTestNG {

  public class ScreenShotRemoteWebDriver extends RemoteWebDriver implements TakesScreenshot {
    public ScreenShotRemoteWebDriver(URL url, DesiredCapabilities dc) {
      super(url, dc);
    }

    @Override
    public <X> X getScreenshotAs(OutputType<X> target)
        throws WebDriverException {
      if ((Boolean) getCapabilities().getCapability(CapabilityType.TAKES_SCREENSHOT)) {
        return target.convertFromBase64Png(execute(DriverCommand.SCREENSHOT).getValue().toString());
      }
      return null;
    }
  }

  @Test
  public void remoteWebDriverScreenshotTest() throws Exception {

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setPlatform(Platform.LINUX);
    capabilities.setBrowserName("chrome");
    capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
    ScreenShotRemoteWebDriver driver = new ScreenShotRemoteWebDriver(new URL("http://192.168.1.6:5555/wd/hub"), capabilities);
    Dimension dimobj = new Dimension(1280,720);
    driver.manage().window().setSize(dimobj);

    try {
      driver.get("https://www.google.com");
      WebElement lstib = (new WebDriverWait(driver, 15))
          .until(ExpectedConditions.presenceOfElementLocated(By.id("lst-ib")));
      lstib.sendKeys("Selenium Pi");
      lstib.sendKeys(Keys.RETURN);
      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);
      File f = driver.getScreenshotAs(OutputType.FILE);
      File o = new File("target/resultStats.png");
      FileUtils.copyFile(f, o);
    } catch (Exception e) {
      System.out.println(e.getMessage());
    } finally {
      driver.quit();
    }
  }
}

TestNG Execution Raspberry Pi:

selenium-execution-chromium-raspberry-pi

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. Saved screenshot can be accessed from the project folder under “target” directory.