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
- I am assuming Windows 7 operating system
- I assume you have Robotframework and RIDE already installed. For detailed information about installing these tools please refer to the following post
- Install Robotframework Android library:
1pip install --upgrade robotframework-androidlibrary - Robotframework Android library is based on Calabash Android which is in turn based on Ruby. You can get Ruby from here
- Add Ruby bin directory to your path environment variable for example:
1C:\\Ruby193\\bin to PATH environment variable - Install Calabash Android:
1gem install --version '= 0.3.2' calabash-android - Install Android SDK. You can get it from here
- Set the environment variable ANDROID_HOME and make it point to android SDK for example:
1C:\\Users\\username\\AppData\\Local\\Android\\android-sdk - Add JDK bin directory to path environment variable for example:
1C:\\Program Files (x86)\\Java\\jdk1.6.0_34\\bin - Download zip command for windows from here and save it into some directory that is included in system path
- Create a key store which is used in signing JAR files:
1234keytool -genkey -v-keystore C:\\Users\\username\\.android\\debug.keystore-storepass android -alias androiddebugkey -keypass android-dname \"CN=Android Debug,O=Android,C=US\" - For some reason I got an error while resigning the android APK file. I fixed that by removing:
1-sigalg MD5withRSA -digestalg SHA1
from:
1C:\\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:
1 |
%ANDROID_HOME%\\tools\\android.bat |
2. To search for SDK updates:
1 |
%ANDROID_HOME%\\tools\\android list sdk --no-ui |
3. To install updates apply:
1 |
%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:
1 |
%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
1 |
%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):
1 |
%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:
1 |
%ANDROID_HOME%\\platform-tools\\adb pull ApiDemos.apk |
8. To unsign an APK file you can apply the command:
1 |
zip -d ApiDemos.apk META-INF\\* |
9. In order to resign an APK file:
1 2 3 |
jarsigner -verbose -keystore C:\\Users\\usename\\.android\\debug.keystore -storepass android -keypass android ApiDemos.apk androiddebugkey |
10. To build an instrumentation app:
1 |
calabash-android build ApiDemos.apk |
11. To uninstall the app:
1 |
%ANDROID_HOME%\\platform-tools\\adb uninstall com.example.android.apis |
1 |
%ANDROID_HOME%\\platform-tools\\adb uninstall com.example.android.apis.test |
12. To install the app:
1 |
%ANDROID_HOME%\\platform-tools\\adb install -r C:\\robot\\suites\\apidemos\\test_servers\\myapp.apk |
1 |
%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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
*** 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.
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.