Built.io joins Software AG! Read all about the news.

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:
      <code> gem install calabash-android</code>
    • Run the following command to install gem Cucumber, which will install the Cucumber framework:
      <code> gem install cucumber</code>
  5. Set the environment variables on your machine.
    • Set the Android path in environment variables:
      <code> ANDROID_HOME : C:Androidsdk
      PATH : C:Androidsdkplatform-tools;C:Androidsdktools</code>
    • Set the JAVA path in environment variables:
      <code> JAVA_HOME : C:Program FilesJavajdk1.8.0_20</code>
  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:
      <code> adb -s emulator-5554 install myapp.apk</code>
  8. Start the Android automation process:
    • Create a new directory from the command prompt field.
      <code> md mydir</code>
    • 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:
      <code> calabash-android gen</code>
    • 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:
      <code> calabash-android setup</code>
    • Next, execute the following commands in the command prompt field:
      <code> calabash-android built myapp.apk</code>
    • 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:
      <code> 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:
    <code> calabash-android run myapp.apk</code>
    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.

Like what you read? Join our community to get more technical information, chances to win prizes, and more: built.io/community

Popular Posts

Subscribe to our blog