Cucumber and calabash for android app testing

This article will cover how cucumber and calabash can be used for android testing. Code used in this post can be found on github using following urls.

Android App
Functional Test Step

The app which we are going to test has only two screens.

  1. Item List Screen – Default screen of app, it has a list of items.
  2. Item Detail Screen – Clicking on an item in the list will take to the detail screen

You will need the following ruby gems to setup cucumber and calabash.

calabash-android – This is the main gem to setup the testing environment for android. It will generate a sample project for you with minimal configuration.
You should have ANDROID_HOME set to your android-sdk path in order to use calabash-android. Put the below line in your bashrc or zshrc file.

Generating scaffold using calabash-android

As you can see when you run calabash-android gen it asks a question before creating the file structure. Hit enter key and it will create the directories for you. The directory structure is shown below.

If you don’t understand the above file structure and want to know what each directory means then visit this link.

Now you don’t have to touch any of the support directory files, because calabash does a good job in default configuration itself.

Replace the my_first.feature with your own features. I have added the following feature to my feature file.

To define your own steps go to calabash_steps.rb file and write your own steps. In my case my steps looks like this.

require ‘calabash-android/calabash_steps’ – this line includes calabash’s predefined steps in our steps and then you can use the predefined steps to press, type or any basic operation. You can find the predefined steps in calabash’s github page.

Query an element on screen

To query and element on the screen use the query method of calabash. You have to pass the path to the element you want to query. For example

If you want to query a text, which will be present inside a TextView then your query will be following
query(‘TextView marked:id_of_text_view’) This function returns the array of elements to get your item out of it use .first method of ruby or [0]
You can use the above query for all kind of elements like ListView etc.

Running Tests

To run the test type the following command. Remember that you have to run the command from outside of the features directory.

Once you supply the apk file to calabash it takes care of installing that app in the simulator and uninstalling it before each feature.

Leave a Reply

Your email address will not be published. Required fields are marked *