Built.io Blog

Start Automating Android Apps Using Cucumber


What is Cucumber?

Cucumber is a testing framework which supports behavior driven development. It’s used to write test cases that are easily readable. The test cases are written in a feature file, and Cucumber executes the test on an Android device or an emulator.

In this post, we will cover how to automate Android applications using Cucumber.

Automating your Android App Using Cucumber

  1. Java installation – Install the latest version of JDK on your machine.
  2. Ruby installation – Install the latest version of Ruby on your machine.
  3. Setting up Android emulator – Install android-sdk to set up the emulator.
  4. Install the required gems:
    • Run the following command to install the gem calabash-android on your system:
       gem install calabash-android
    • Run the following command to install gem Cucumber, which will install the Cucumber framework:
       gem install cucumber
  5. Set the environment variables on your machine.
    • Set the Android path in environment variables:
       ANDROID_HOME : C:Androidsdk
      PATH : C:Androidsdkplatform-tools;C:Androidsdktools
    • Set the JAVA path in environment variables:
       JAVA_HOME : C:Program FilesJavajdk1.8.0_20
  6. Set up the Android emulator and verify it. Start the Android emulator and in the command prompt field, type adb devices. This will list the Android emulator in the command prompt field.
  7. Install the application on a mobile device.
    1. Paste the application apk file in the platform-tools directory.
    2. Type the following command in the command prompt field to install the application on emulator:
       adb -s emulator-5554 install myapp.apk
  8. Start the Android automation process:
    • Create a new directory from the command prompt field.
       md mydir
    • By executing the following commands in command prompt field, you will generate the folder structure which will contain the feature file, step_definitions and the support directory:
       calabash-android gen
    • Sign the apk file by executing the following command in the command prompt field. Enter a valid keystore location, password and alias after entering the following into the command field:
       calabash-android setup
    • Next, execute the following commands in the command prompt field:
       calabash-android built myapp.apk
    • Now, write your code in the feature file:
      • Feature: Sign-in page
      • Scenario: Enter valid credentials and go to dashboard
      • Given I am on "Sign in" with Google page
      • When I enter valid username and password
      • Then Click sign in button
    • Now write the following code in the calabash_steps.rb file in the step_definitions folder:
       require 'calabash-android/calabash_steps'
      Given /^I am on "Sign in" with Google page$/ do
      query("* text:'loginscreen' index:0")
      When /^When I enter valid username and password$/ do
      enter_text("webView css:'#Email'", "abc@gmail.com")
      enter_text("webView css:'#Passwd'","test123")
      Then /^Then Click sign in button$/ do
      touch("webView css:'#signIn'")
  9. In the command prompt field, navigate to the folder where the feature file is situated, and type the following command to execute the start automation:
     calabash-android run myapp.apk
    This command will start the app on emulator and will execute the all the steps in feature file. You have successfully automated an Android app with Cucumber.

Have a question? Leave it in the comments and we'll be sure to respond!

Subscribe to our blog