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

Share

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

Prerequisites on Ubuntu Mate

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

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

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.

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

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.

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 hubIf 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.

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/

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

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.

TestNG Execution 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.