Cucumber is a behavior-driven development (BDD) framework that allows users to describe app behavior in plain text. Cucumber works by defining feature specs in Gherkin and then writing Ruby step definitions to translate plaintext Gherkin steps into actions. For more information on Cucumber, see the docs linked above or the Originate Blog Post on User-level Feature Specs With Cucumber.
For mobile applications, step definitions issue Appium commands to control mobile devices.
In order to use Cucumber and Appium, we’ll first need to install a couple of dependencies.
Add the above to a Gemfile in your app’s root directory and run
bundle install. If you don’t have bundler already installed on your computer, then run
gem install bundler first. This will give us the gems we need to start working with Cucumber and Appium in Ruby.
Appium offers both a standard command-line server and an OS X Appium desktop application (which wraps their server up in a nice GUI and comes bundled with a useful inspector tool). Only the command-line server is necessary, however the GUI offers some additional features that may prove useful.
Describing how to write feature specs with Cucumber and Appium is no easy task. Specifications can vary greatly from project to project. As such, we’ll use a simple example to show you how it works. Check out the official Ruby documentation for a full list of Appium commands.
As of June 1, 2015, the
scroll_to(text) method is broken on Android and fails to scroll to the appropriate element.
swipe method works based off of
:end_y coordinates which vary greatly from device to device. A swipe that lands you in the correct spot on your local emulator may not work as well on CircleCI.
To run your feature specs locally, simply open a separate shell and start the app in the background.
Then run the Cucumber features from your project’s root directory.
Once you have your tests up and running locally, it is time to start adding them to your CircleCI build.
By default, CirleCI uses either 1.9.3-p448 or 1.8.7-p358 as their default version of Ruby. If your project necessitates a specific version of Ruby, you can manually specify prior to the build. In our experience, the Appium feature steps work best on Ruby 2.2, so add the following to your circle file.
In order to run our feature specs, add the following to your circle file to install Appium and the Ruby gems that are listed in our Gemfile.
- npm install -g appium
- bundle install
The above snippet starts the Appium server in the background and runs the cucumber feature specs.
Your CircleCI environment should now be ready to run your feature specs each time there is a new build.
The above guide and the linked feature specification example should be enough to get you started writing integration tests with Cucumber and Appium. Hopefully, if all goes well, you will be seeing green!
The complete feature spec code built from this example can be found here.