How to Combine Cucumber with Mobile Test Automation Frameworks

  April 26, 2016

The Behaviour-Driven Development (BDD) method gathers supporters for many obvious reasons as it is readable (by everyone), understandable (across teams and functions) and accountable (metric-driven for success/failures). Cucumber is a great example of an optimized tool to create such tests with other open test automation frameworks. This allows basically anyone (developers, QA, testers, etc.) to write specifications in any spoken language that then gets executed as automated tests against the mobile app(s). Furthermore, this sort of BDD approach incorporates the main standards of test-driven development (TDD) and provides a shared view and process for collaboration between all stakeholders.

Let’s look at how to set up Cucumber and how to combine it for some standard test automation frameworks (Appium / Selenium).

Download Our Tutorial to Master Cucumber/Calabash for Mobile App Testing.

cucumber test automation

Cucumber for Test Automation

The Cucumber framework is a neat tool that developers can use to test their stuff. In the mobile context, Cucumber is also usable and for example, is used with Calabash. Basically, Cucumber can run automated acceptance tests written in a behavior-driven development (BDD) style and is available with Ruby. In addition, there are other Cucumber projects available for some other platforms/programming languages, but we’ll take a look at Ruby side of things here.

One of the best parts of Cucumber is that it also provides documentation and feature specification for mobile app testing. When the test script is written in an understandable language it can also serve as a design, development and test specification – and that’s what makes Cucumber highly usable across functions.

Cucumber can be also used to enable large-scale test automation with real mobile devices. There are myriad of different test automation frameworks that can get you there instantly but also combined together with Cucumber. Cucumber behavior-driven development together with one of the best test automation frameworks today – Appium – is a great combination and can help to you automate testing of your apps, write nicely documented specifications and make clarity for test execution.

Prerequisites and Installations

First and foremost, check that your Ruby environment is up-to-date, by using the following terminal line:

$ ruby -v

Things may work pretty well on some older Ruby versions, but to get all the best features and the most stable experience, we recommend using the latest (e.g. 2.2.4 or newer).

RVM – Ruby Version Manager

In case you don’t have or have an old version of Ruby or you want to use multiple versions of Ruby, RVM (Ruby Version Manager) is a neat command-line tool which allows quick and easy installations, management of multiple versions and Ruby environments with interpreters and any set of gems. You can simply get it installed with these instructions:

$ curl -sSL https://get.rvm.io | bash -s stable
$ rvm install ruby

List of all installed versions of Ruby and set one of those as default one:

$ rvm list
$ rvm --default use 2.1.1

The upgrade of RVM can be done with the following lines:

$ rvm get head
$ rvm autolibs homebrew
$ rvm install ruby

Note that this takes care of Homebrew installation as well. Another alternative to install Homebrew is done as follows:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

After setting up Homebrew you can (and you should) check every now and then if the environment is up to snuff:

$ brew doctor

This should give you Your system is ready to brew but Your Homebrew is outdated is also very common and then keeping Brew up-to-date can be simply done with an update:

$ brew update

Again, it’s worth of checking that it’s Ruby is properly installed by printing out the Ruby version:

$ ruby -v

Update RubyGems and Bundler:

$ gem update --system
$ gem install --no-rdoc --no-ri bundler
$ gem update
$ gem cleanup

Check that RubyGems is >= 2.1.5

$ gem --version

Platform-Specific Tools

Depending on which platform – OS X, Linux or Windows – you’re about to run your tests, you need to set up all required development tools (Xcode for iOS, Android Studio) and other stuff. For detailed instructions on how to setup Appium for any platform, with the desired programming language and all dependencies, take a look at our Appium tutorial.

Cucumber Installation

Setting up Cucumber is pretty straightforward.

Create a directory for test example:

$ mkdir cucumber-test
cd cucumber-test

Then create a file under this folder to include the following lines:

source "https://www.rubygems.org"
gem "cucumber"
gem "selenium-webdriver"
gem "rspec"
gem "rspec-expectations"

Now, run bundler at the Ruby example directory to install dependencies:

$ bundle install

Now, we have installed everything that it takes to start writing tests and running those. For detailed information on how to write Cucumber tests, you can check from our tutorial. This tutorial gives instructions to write Calabash tests but overall the syntax is the same and tests, steps and features are done the same way.

Happy Testing!