Robotframework Android Automation

Introduction

This is going to be a very quick tutorial to demonstrate that Robotframewok can be used to automate Android based smart phone applications. In this tutorial we are going to use Robotframework Android library which sits on top of Calabash Android framework. I tried it myself and was able to make it run however the downside is that it was not as reliable as I was expecting. For example, the setup procedure is flaky and takes time but I think it can be solved by separating the setup part from the test case part. The part that I did not personally like is clicking on text was not working consistently which is bad. Before we start I would like to repeat by saying I did not put much time on this experiment and I can not be so sure if the "not so good" results I got were because of the tools stack selection or were just my own mistakes. I would leave it to the reader to explore it more. The idea is to show that Robotframework is such an amazing automation tool that can be extended to do all kinds of test automation. As far as Android automation is concerned one can try other tools for example Robotium, Monkey Runner or even Sikuli. Let us get started by following the steps below:

Installation

  1. I am assuming Windows 7 operating system
  2. I assume you have Robotframework and RIDE already installed. For detailed information about installing these tools please refer to the following post
  3. Install Robotframework Android library:
    pip install --upgrade robotframework-androidlibrary
  4. Robotframework Android library is based on Calabash Android which is in turn based on Ruby. You can get Ruby from here
  5. Add Ruby bin directory to your path environment variable for example:
    C:\\Ruby193\\bin to PATH environment variable
  6. Install Calabash Android:
    gem install --version '= 0.3.2' calabash-android
  7. Install Android SDK. You can get it from here
  8. Set the environment variable ANDROID_HOME and make it point to android SDK for example:
    C:\\Users\\username\\AppData\\Local\\Android\\android-sdk
  9. Add JDK bin directory to path environment variable for example:
    C:\\Program Files (x86)\\Java\\jdk1.6.0_34\\bin
  10. Download zip command for windows from here and save it into some directory that is included in system path
  11. Create a key store which is used in signing JAR files:
    keytool -genkey -v 
    -keystore C:\\Users\\username\\.android\\debug.keystore 
    -storepass android -alias androiddebugkey -keypass android 
    -dname \"CN=Android Debug,O=Android,C=US\"
  12. For some reason I got an error while resigning the android APK file. I fixed that by removing:
    -sigalg MD5withRSA -digestalg SHA1

    from:

    C:\\Ruby193\\lib\\ruby\gems\\1.9.1\\gems\\calabash-android-0.3.2\\bin\\calabash-android-build.rb

    as I mentioned earlier this is nothing but an experiment and I did not invest enough time
    to see if this could have any side effects. I just wanted to get rid off the annoying error
    message.

Android SDK Commands

Behind the scenes Robotframework Android library and Calabash Android framework invoke commands provided by Android SDK. Take a look at the following commands in order to get a feeling of what they do. I am going to be using the ApiDemos.apk sample application. You can download the apk file from here

1. Make sure the following bat file exists:

%ANDROID_HOME%\\tools\\android.bat

2. To search for SDK updates:

%ANDROID_HOME%\\tools\\android list sdk --no-ui

3. To install updates apply:

%ANDROID_HOME%\\tools\\android update sdk --filter 1,2 --no-ui

where 1 and 2 are update numbers (you can get the numbers by applying the command in step 2)

4. Create an Android virtual device:

%ANDROID_HOME%\\tools\\android --silent create avd --name EM_NAME --target android-8 --force

where android-8 is the target API number and force is used to overwrite existing configuration

5. Start emulator

%ANDROID_HOME%\\tools\\emulator -avd EM_NAME -no-window

no-wondow option is used to supress the GUI and run it heedlessly

6. Invoke Android Debug Bridge (adb command):

%ANDROID_HOME%\\platform-tools\\adb wait-for-device

It is a command line tool that lets you communicate with an emulator instance or connected Android device. For more information about adb command click here.

7. If you want to download an installed app (in the form of an APK file) from emulator or device:

%ANDROID_HOME%\\platform-tools\\adb pull ApiDemos.apk

8. To unsign an APK file you can apply the command:

zip -d ApiDemos.apk META-INF\\*

9. In order to resign an APK file:

jarsigner -verbose 
-keystore C:\\Users\\usename\\.android\\debug.keystore 
-storepass android -keypass android ApiDemos.apk androiddebugkey

10. To build an instrumentation app:

calabash-android build ApiDemos.apk

11. To uninstall the app:

%ANDROID_HOME%\\platform-tools\\adb uninstall com.example.android.apis
%ANDROID_HOME%\\platform-tools\\adb uninstall com.example.android.apis.test

12. To install the app:

%ANDROID_HOME%\\platform-tools\\adb install -r C:\\robot\\suites\\apidemos\\test_servers\\myapp.apk
%ANDROID_HOME%\\platform-tools\\adb install -r C:\\robot\\suites\\apidemos\\ApiDemos.apk

where [C:\\robot\\suites\\apidemos] is some directory containing the sample APK file. In this case it is the same directory containing robotframework test suite while [test_servers] is the directory created by calabash Android when building the instrumentation app (in this case: myapp.apk)

Robotframework Syntax

Here is how the Robotframework test suite look like. Please note that the text was formatted to better fit in the page so if you just copy and paste, it might need minor editing to work with Robotframework. For example some long lines were broken into multiple lines:

*** Settings ***
Suite Setup       Setup Suite
Test Timeout       10 minutes
# This is used to invoke Android calls
Library       AndroidLibrary
# This is used to execute system calls
Library       OperatingSystem

*** Variables ***
${EMULATOR_NAME}       my-emulator
# Android target API level
${API_LEVEL}       8
${HEADLESS}       False

*** Test Cases ***
# Simple test case to touch text then varify some text on screen
Touch Text
Capture Screenshot
Touch Text       Text
Capture Screenshot
Touch Text       Linkify
Capture Screenshot
Screen Should Contain       http
Capture Screenshot

*** Keywords ***
# Helper keyword to execute system commands
# The keyword Run And Return Rc And Output is imported
# from the OperatingSystem Robotframework built in library
Execute
       [Arguments]       ${command}
       ${rc}       ${output} =       
       Run And Return Rc And Output       ${command}
       Should Be Equal As Integers       ${rc}       0

# Th Setup Suite keyword is a one to one mapping to
# the commands in the previos section
# It does the following:
# 1. Make sure the Android SDK exists
# 2. Create the virtual device
# 3. Start the emulator
# 4. Sign the APK file
# 5. Build the instrumentation app
# 6. Install the application under test
# 7. Start then connect with test server
Setup Suite
       Android SDK Should Exist
       # Update Android SDK
       Create Android Virtual Device
       ${HEADLESS_BOOL}=       Convert To Boolean       ${HEADLESS}
       Start Emulator       
       ${EMULATOR_NAME}       no_window=${HEADLESS_BOOL}
       # Pull ApiDemos.apk from Device
       Re-Sign ApiDemos.apk with Debug Keystore
       Build Instrumentation App
       Install App
       Wait For Device
       Start Testserver with apk       ApiDemos.apk
       Wait Until Keyword Succeeds       
       1min       5sec       Connect To Testserver

Android SDK Should Exist
       File Should Exist       %{ANDROID_HOME}\\tools\\android.bat

Update Android SDK
       Execute       
       %{ANDROID_HOME}\\tools\\android list sdk --no-ui
       Execute       
       %{ANDROID_HOME}\\tools\\android update sdk --filter 1,2 --no-ui

Create Android Virtual Device
       Execute       echo no | %{ANDROID_HOME}\\tools\\android 
     --silent create avd --name ${EM_NAME} 
     --target android-${API_LEVEL} --force

Pull ApiDemos.apk from Device
       [Timeout]       5 minutes
       Wait For Device
       Remove File       ApiDemos.apk
       Wait Until Keyword Succeeds       30 seconds       5 seconds
       Execute       
       %{ANDROID_HOME}\\platform-tools\\adb pull ApiDemos.apk

Re-Sign ApiDemos.apk with Debug Keystore
       File Should Exist       ApiDemos.apk
       Execute       zip -d ApiDemos.apk META-INF\\*
       Execute       jarsigner -verbose 
      -keystore C:\\Users\\username\\.android\\debug.keystore 
      -storepass android -keypass android ApiDemos.apk androiddebugkey

Build Instrumentation App
       Remove Directory       
       ${EXECDIR}\\test_servers\\       recursive=True
       Execute       calabash-android build ApiDemos.apk
       ${TEST_SERVERS}=       
       List Directory       ${EXECDIR}\\test_servers\\

# Get the generated instrumentation APK file name
Set Global Variable       ${TEST_APK}       ${TEST_SERVERS[0]}

Install App
       [Timeout]       5 minutes
       Uninstall Application       com.example.android.apis
       Uninstall Application       com.example.android.apis.test

# Path to the generated instrumentation APK file
Install Application       ${EXECDIR}\\test_servers\\${TEST_APK}

# Path to the application APK file
Install Application       ${EXECDIR}\\ApiDemos.apk

Robotframework Android Library Keywords

Here is the full list of Robotframework Android library supported keywords.

Please use the comments section below for questions, corrections or feedback. Thanks for reading.

Search Terms...
  1. Could you maybe put a link to a downloadable version? I’m still having problems to get this to work, currently failing at the keywords, execute section.

Leave a Reply

%d bloggers like this: