Calabash: Functional Testing For Mobile Apps

Calabash is an automated testing technology for Android and iOS native and hybrid applications. Calabash is a free open source project, developed and maintained by Xamarin.
While Calabash is completely free, Xamarin provides a number of commercial services centered around Calabash and quality assurance for mobile, namely Xamarin Test Cloud consisting of hosted test-execution environments which let you execute Calabash tests on a large number of Android and iOS devices.

How Calabash works in Android:
When a Calabash Android test is executed both your local computer and a device is involved. The device might be an emulator or an actual physical device. The setup looks like this:

5

Features: The feature files describe the user-stories you want to test. You can test one or more features in one test run.

Step Definitions: Calabash Android comes with a set of predefined step which you can find here. These steps are generic and made to get you up and running fast. When you get more into Calabash you can implement your own custom steps that use the business terms your project uses like I transfer money to my spending’s account or I play “Never Gonna Give You Up”.

Your app: You don’t have to make modifications to your app before testing it.

Instrumentation Test Server: This is another app that will be installed and executed the device. This app is based on ActivityInstrumentationTestCase2 from the Android SDK. It is generated by the Calabash Android framework.

Calabash-android Setup

The First and foremost things is to have all the prerequisites tools installed in your system. The below are the step by step explanation of how to quickly setup your development environment for functional testing of mobile apps (android) using Cucumber and Calabash-android.

Pre-Requisites:

  1. You need to have Ruby installed. Verify your installation by running ruby -v in a terminal – it should print “ruby 1.8.7” (or higher).
    If you are on Windows you can get Ruby from RubyInstaller.org
    http://rubyinstaller.org/downloads/

6       2.    During installation check the 2nd and 3rd options i.e.

  • Add Ruby executables to your PATH
  • Associate .rb and .rbw files with this Ruby installation

7

  1. You should have the Android SDK installed and the environment variable ANDROID_HOME should be pointing to it. Refer here
  2. Install Cucumber
    Navigate to Command Prompt and type “gem install cucumber
  3. Install Calabash-android
    Navigate to Command Prompt and type “gem install calabash-android

Automating an application using Calabash

  • Make sure that the android emulator is running. Refer here.
  • Create a folder on the desktop (e.g. testapp)
  • Download a test app from the internet and place it in the “testapp” folder
  • For this example I have used the GO Contacts EX.apk downloaded from http://www.appsapk.com/go-contacts-ex/
  • Open the command prompt and navigate to the “testapp” folder8
  • Type  “calabash-android gen” and hit enter twice9
  • features folder will automatically get created in the “testapp” folder10
  • The features folder consists of the following file and directories
    • step_definitions (folder)
    • support (folder)
    • my_first.features (.feature file)
  • 11
  • Edit “my_first.feature” file and replace the content with the following BDD statements

Feature:  As a user of GO Contacts EX.apk
I want to verify the addition and deletion of contacts in the application
Scenario: As a valid user I can add a friend into the contacts list
Given I press button with id “dial_btn_num9”
Given I press button with id “dial_btn_num9”
Given I press button with id “dial_btn_num7”
Given I press button with id “dial_btn_num5”
Given I press button with id “dial_btn_num3”
Given I select new contacts with id “newnumber_title”
Given I should wait for the text “New contact” to appear
When I enter the text “myfirstnumber” into input field number 1
When I enter the text “testnum” into input field number 2
* I press button with id “btn_done”
* I should wait for the text “No Record” to appear
Then I should see the text “No Record”
Then the expected value “No Record” should be equal to the actual value

“my_first.feature” file will now look something like this:

12

  • Edit the step_definitions folder and edit the calabash_steps.rb ruby file (open in a notepad)

Replace the content of the file with the following code:

When /^I enter the text “([^\”]*)” into input field number (\d+)$/ do |text, number|
performAction(‘enter_text_into_numbered_field’,text, number)
end

Given /^I press button with id “([^\”]*)”$/ do |button_id|
performAction(‘click_on_view_by_id’,button_id)
end

Given /^I select new contacts with id “([^\”]*)”$/ do |view_id|
performAction(‘click_on_view_by_id’,view_id)
end

Given /^I should wait for the text “([^\”]*)” to appear$/ do |text|
performAction(‘wait_for_text’, text)
end

Then /^I should see the text “([^\”]*)”$/ do |text|
performAction(‘assert_text’, text, true)
end

Then /^the expected text “([^\”]*)” should be equal to the actual text$/ do | expected_value |
actual_value =  performAction(‘get_text_by_id’,”recentemppty”)[‘message’]
raise “The current value is #{actual_value}” unless( actual_value == expected_value)
end

“calabash_steps.rb” ruby file will now look something like this:

13

  • Navigate to Command Prompt and type calabash-android resign “GO Contacts EX.apk”
  • 14
  • Next type calabash-android run “GO Contacts EX.apk” in the command prompt

Note: Make sure that the mobile emulator is unlocked, before running the test cases

Calabash-Android Pros and Cons

Pros

  • It is an Open-Source Tool – No Licensing Fees.
  • Since – The calabash-android is based on the Cucumber framework. The test cases can be easily created in real simple language.
  • Support for all the basic events and movements on the mobile are present in the libraries.
  • It has a thriving forum and Google Group: “Calabash Android”.

Cons

  • It takes time to run on an emulator or device as it always installs the app first before starting the first scenario.
  • If a step fails then the subsequent tests in the scenario are skipped
  • It is still in its nascent stage. Support for many complex scenarios or events is not supported. For that either you have to code your way in Ruby or wait for these supports to appear on the scene.
  • We must have the code of the app for identifying the ids of various elements.

Leave a comment