Cukes and Apples: App Automation with Ruby and Appium

(Part One)

This post will be the first of a series that demonstrates how to build robust mobile test automation using Ruby, Cucumber, and Appium. The initial implementation is relatively simple – a good place to start, but not mature. Over this series, we will upgrade this Ruby Cucumber test suite to add capability and improve ease of use. This post will focus on introducing tools and setting up our project.

Workspace Setup

I’m using a Windows PC and Android phone at this time. Some things I write will be specific to that platform configuration and would differ for users of Mac and iOS. I will point out the differences where I can but focus primarily on Android implementation. I hope to include much more information about Mac and iOS in a future entry about cross-platform support.

Install Ruby

As a Windows user, I use RubyInstaller to install Ruby. For this series, I selected Ruby 2.6. In this case, as I usually do, I chose the recommended version (as shown below) for greatest gem compatibility.

Download RubyInstaller from the following location and install with default options selected:

If you are a Mac user, you have Ruby installed by default, but an older version. You can install a newer version of Ruby with a version manager like rbenv or rvm. I have used rbenv and I recommend it. Check out the installation instructions here:

Install RubyMine

You don’t need RubyMine, but I do recommend it. I use it myself, so my examples will show it. I’m using RubyMine 2020.1.

Download RubyMine from the following location and install with default options selected:

Install Appium

If you download Appium from the Appium website, you get Appium Desktop, which augments Appium with a graphical interface and tools for inspecting elements in mobile apps. I’m using Appium Desktop 1.15.1 (exe).

Download Appium from the following location and install with default options selected:

The install process is very straightforward for Appium Desktop, but the Getting Started page in the official Appium documentation explains how you can also install Appium (without the GUI) using NPM.

Set Up a Device

We can test our Appium installation against a device or a simulator – for this post, I will be using a real Android phone. If you are using Mac and iOS, I recommend starting with an iOS simulator.

To allow automation on an Android device, you must allow USB debugging in the developer options. The process of enabling developer options varies for different phones, so you will need to find documentation specific to your phone. For mine, I had to launch the Settings app and tap on the Build Number seven times. The following messages were displayed while tapping and afterward.

Once developer options are enabled, you should find a switch for USB debugging under Developer Options. Toggle that on.

Be mindful of popups that appear when your device is connected to a computer. Requests for access, like the one below, can prevent Appium from controlling the device.

Install the Android SDK

The Android SDK includes adb.exe, which allows us to query for device names and control connected Android devices. To acquire the Android SDK, we must install Android Studio.

Download Android Studio from the following location and install with default options selected, or choose custom installation to install device emulators with the Android Virtual Device option.

After installing the Android SDK, you will need add it to your system path. Create an ANDROID_HOME environment variable for the SDK install location.

And then add the following two directories to your Path variable.

With the Android SDK installed, it should be possible to retrieve the name of your connected device.

Check Appium

We can verify our device setup and Appium configuration by using Appium to launch an app on a device.

When you first launch Appium, you should be able to select Start Server without making any other configuration changes. After the Start Server button is selected, Appium displays a message that confirms the server is running.

Create a New Project

Create an empty Ruby project and add a Gemfile which includes both the cucumber and appium_lib gems.

Use Bundler to install cucumber and appium_lib.

Note: This installation process may produce specific errors later. If you see LoadErrors with the message “Cannot load such file” and references to ffi or eventmachine, I recommend uninstalling the offending gem, then reinstalling it with the platform argument.

ex. “gem install eventmachine –platform ruby”

A LoadError, as mentioned above

Cucumber Directories

Create the following directories for a Cucumber test suite, and create a Ruby file named env.rb in features/support

  • features/gherkin
  • features/step_definitions
  • features/support

Bringing It All Together

Open up env.rb. We are going to use it to require the Appium gem ‘appium_lib’, and then write a simple script to prove our workspace setup was successful.

The screenshot above features all of the code we need to verify that Ruby, Cucumber, and Appium are all cooperating. When Cucumber starts, env.rb will be executed, an Appium driver will be created, and an app will be launched.

Where did the capabilities information come from? The value associated with deviceName was identified with adb. The appPackage and appActivity are both references to the Google Play Store app, but any app will do.

Mac and iOS users will ignore the appPackage and appActivity capabilities – use bundleId instead. See this documentation:

Did It Work?

As shown in the screenshots below, the Cucumber process executed successfully (with no scenarios) and the Google Play app was launched on my phone.

Coming Up Next

There is more work required before our mobile test suite will be functional, and a lot more before it’s mature. Today, we covered workspace setup. In the future, we hope to deliver some or all of the following topics:

  • Full integration of Appium and Cucumber – managing the driver and capabilities across tests, writing steps for mobile automation
  • Implementing the Page Object pattern – building mobile page objects to organize information and behavior
  • Cross-platform mobile automation – creating flexible execution mechanisms, page objects that cover multiple platforms, tags for platform-specific execution


Published by Joshua Russell

My name is Josh, and I’m a test automation developer in Columbus, Ohio. I’ve developed test automation for applications on several platforms - web applications, Android and iOS apps, mainframe systems, Windows desktop applications, and more - to serve the needs of insurance, banking, and retail.

Leave a Reply

%d bloggers like this: