Android Software - Selenium Hub with Appium nodes

This documentation will guide you in installing a mobile testing platform (software part) using a Selenium Hub and Appium nodes for Android phones.

List of prerequisites :

  • a computer with macOS Mojave (minimum version : 10.14.0)
  • administrator rights

What you will need to install :

  • JDK 1.8
  • Android SDK 28
  • Node.js 8.12.0
  • npm 6.4.1
  • Selenium server 3.141.59
  • Appium 1.8.1
  • Appium-Doctor 1.6.0

Tools installation

JDK

Download and install JDK 1.8 from Oracle website and add the environment variable :

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=${JAVA_HOME}/bin:$PATH
source .bash_profile

Android SDK

Download the Android SDK from here and move the downloaded folder to /Application folder. Then, add the environment variable :

export ANDROID_HOME=/Applications/android-sdk-macosx/
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
source .bash_profile

Update the Android SDK following the README instructions in the downloaded folder.

Node.js

Download and install Node.js 8.12.0 from here. You can check the version using :

node -v
npm -v

Homebrew and adb

Install homebrew followig instructions of Homebrew hompage. Then, install adb (included in platform-tools) using this command :

brew cask install android-platform-tools

Selenium server

Install Selenium server 3.141.59 using this command :

brew install selenium-server-standalone@3.141.59

You can check the version using :

selenium-server -version

Appium server

You may install different versions of Appium (for example, Appium 1.13.0 for iOS part and Appium 1.8.1 for Android part), so we recommend that you do not install Appium in global mode. Create a folder /AppiumAndroid somewhere and then run the following command to install Appium :

npm install appium@1.8.1 --chromedriver_version="2.35" --save

If you will use an unique version of Appium, you can install Appium server in global mode using the option -g.

Appium Doctor

Install Appium Doctor :

npm install -g appium-doctor

and then run the following command to check if all prerequisites are installed for Appium :

appium-doctor

Appium node configuration

You will need to create a configuration file for each phone you want to connect. You can find an example of configuration file in /nodes folder. This file is composed as follows. You have to change some information in this sample file :

  • the device name
  • the device UUID
  • the Android version on the device
  • the IP address and the port of the Appium node
  • the IP address and the port of the Selenium hub

How to get UUID ?

Plug your phone into an USB port and run the following command in terminal :

adb devices

It will show you the list of the connected devices and you can get the device UUID :

List of devices attached
A1B2C3D4E5F6G7H8     device

How to get IP address ?

To get the IP address of Selenium hub, run the command below in a terminal on the computer you will start the Selenium server :

ifconfig

If you want to launch an Appium node on a different computer, run this command on the computer you will start the Appium server.

Start the platform

First, you have to run Selenium server in hub mode in a terminal. By default it’s running on 4444 port but you can change it using -port option :

selenium-server -role hub -port 4445

You should see the following message :

11:20:07.109 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
11:20:07.194 INFO [GridLauncherV3.lambda$buildLaunchers$5] - Launching Selenium Grid hub on port 4445
2019-09-04 11:20:07.545:INFO: main: Logging initialized @636ms to org.seleniumhq.jetty9.util.log.StdErrLog
11:20:07.697 INFO [Hub.start] - Selenium Grid hub is up and running
11:20:07.697 INFO [Hub.start] - Nodes should register to http://xx.xx.xx.xx:4445/grid/register/
11:20:07.697 INFO [Hub.start] - Clients should connect to http://xx.xx.xx.xx:4445/wd/hub

You also can check if your hub is running going to http://xx.xx.xx.xx:4445/grid/console

Now, make sure your phone is connected to the computer and run an appium server in node mode in a terminal :

/path/to/appium --nodeconfig /path/to/android-node-config-file.json --port 4701

You should see the following message :

[Appium] Welcome to Appium v1.8.1 (REV 2e793f46beedc3974be1f2ebcaea8ebafbe320d2)
[Appium] Non-default server args:
[Appium]   port: 4701
[Appium]   nodeconfig: /path/to/android-node-config-file.json
[debug] [Appium] Starting auto register thread for grid. Will try to register every 10000 ms.
[Appium] Appium REST http interface listener started on 0.0.0.0:4701
[HTTP] --> GET /wd/hub/status
[HTTP] {}
[debug] [MJSONWP] Calling AppiumDriver.getStatus() with args: []
[debug] [MJSONWP] Responding to client with driver.getStatus() result: {"build":{"version":"1.8.1","revision":"2e793f46beedc3974be1f2ebcaea8ebafbe320d2"}}
[HTTP] <-- GET /wd/hub/status 200 17 ms - 121
[HTTP]
[debug] [Appium] Appium successfully registered with the grid on http://xx.xx.xx.xx:4445
[HTTP] --> GET /wd/hub/status

In the Selenium server terminal, you should see this message attesting the node is well connected to hub :

11:22:59.708 INFO [DefaultGridRegistry.add] - Registered a node http://xx.xx.xxx.xxx:4701

You must see a new node connected in the hub grid : http://xx.xx.xx.xx:4445/grid/console

If the connection is not established, you may have to check the state of the firewall, which can be problematic.

Automate servers launch (optional)

In the /scripts folder you will find a script that will launch the Selenium hub and Appium nodes. After modifying the path, you can start the script running this command in a terminal :

. run-selenium-appium-servers-for-android.sh

To go further, you can schedule the server launch by defining a cron job. To see the list of your cron jobs, run the command :

crontab -l

To add a new entry in the crontab, run this command :

crontab -e

Edit the file, adding this line in order to launch the script at 9AM every week day :

0 9 * * 1-5 . /Users/path/to/run-selenium-appium-servers-for-android.sh

Learn more about cron expression here : https://crontab.guru/

Automate the checking connected devices (optional)

In the /scripts folder, you will find a script that check the connected devices. This script counts the number of devices and send a slack message if there is a missing one. You have to change some information :

  • the expected number of connected devices
  • the slack channel you want to receive a message
  • the alert message
  • the displayed name
  • the slack hook url

To go further, you can schedule this devices checking by defining a cron job. To see the list of your cron jobs, run the command :

crontab -l

To add a new entry in the crontab, run this command :

crontab -e

Edit the file, adding this line in order to check every 10 minutes between 9AM and 6PM every week day :

*/10 9-18 * * 1-5 . /Users/path/to/check-connected-phones.sh

Learn more about cron expression here : https://crontab.guru/

Phones setup (optional)

To improve the use of the platform and make it as autonomous as possible, you can :

  • disable the phone pausing
  • disable the Google voice assistant or remove the access permission to the microphone