Writing and Coding Calabash-Android Test Cases

Building on my calabash-android setup post, I want to explain how to start writing calabash-android test cases in the ‘feature’ files and their respective code in ‘step definition’ files.

After you install the Calabash-Android gem then a directory structure is saved at the following path:

[code language=”text”]C:Ruby193librubygems1.9.1gemscalabash-android-0.4.3[/code]

In this folder: “C:Ruby193librubygems1.9.1gemscalabash-android-0.4.3libcalabash-androidsteps” you should find various .rb files which contain the pre-written code for various mobile app events such as touch, swipe, scroll down, enter text in input field number and wait for events.

Such pre-written steps help a lot in writing test cases in the plain English format of Cucumber.

Feature File and Step Definition

Here is an example of a feature file and step definition content for testing login.

Feature file content

[code language=”text”]
Scenario: Login Functionality
Given I am on the Login page
When I enter invalid username and password
And I press Login button
Then Error message should appear stating “Invalid username or password”

Step definition file content

[code language=”text”]
Given (/^I am on the Splash Screen$/) do
wait_for(180) {element_exists("button id:")}
##This row will wait for 180 seconds before loading the login page after installing the app on your mobile device or emulator.

When (/^I enter invalid username and password$/) do
steps %{
Then I enter "" into input field number 1
## This row will enter text in the username field (If it is the first input or text field on the page.)

Then I enter "" into input field number 2
## This row will enter text in the password field (If it is the second input or text field on the page.)

Then I wait for 5 seconds
## This row will make the app wait for 5 seconds

And (/^I press Login button$/) do
steps %{

Then I press the "Log in" button
## This row will press the button with text “Login” on it

Then I wait for 5 seconds

Then (/^Error message should appear stating “Invalid username or password”$/) do
steps %{
Then I see “Invalid username and password”
# This row will check for the text: “Invalid username and password” on the page.

Now calabash-android will read these step definitions and will work with the app according to them.

In this way calabash-android and its pre-defined libraries easily automates the mobile app testing on an emulator or real device.

Common Calabash for Android Issues

Challenge: Check for text that appears twice on the same page but with the second instance not in the viewable area.

Solution: Check that the text appears correctly in the first instance then scroll down the page and check for the second instance.

Feature file content

[code language=”text”]
Then I see “abcdef”
This I scroll down
Then I see “abcdef”

Pros of using calabash-android:

  1. It is absolutely free.
  2. As it is based on the Cucumber framework the test cases are very easily created in simple language.
  3. Support for all the basic events and movements on the mobile are present in the libraries.
  4.  It has a thriving forum and Google Group: “Calabash Android”.

Cons of using calabash-android:

  1. It takes time to run on an emulator or device as it always installs the app first before starting the first scenario.
  2. If a step fails then the subsequent tests in the scenario are skipped.
  3. 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.
  4. We must have the code of the app for identifying the ids of various elements.