Automatic UI testing helps to keep the quality of your software high and helps you notice if your code changes affect the implementation of the actual use cases. When you are creating Android applications you would like to be able to test them without the help of many human testers. Human testing of UI of the same application on multiple devices is a waste of time, so you want to eliminate it through automation, like you would do it with Selenium for web application. For Android applications there is a similar tool for testing calledRobotium.
It is a UI testing tool which simulates touching, clicks, typing, and other users' actions relevant for Android applications. Writing tests using Robotium is very easy as you can see from below.
Bitbar, the company I work for, has created a Continuous Integration environment with automated UI testing for Android applications. Here are some benefits of using it:
- Automated execution of UI tests (created in Robotium) on each commit.
- Testing against many configurations, real devices and targets that emulate real devices
- Visual presentation of test results in human friendly form
- Screen shots from the UI testing process, so you can see how your application looks on real devices
Create Android Test Project:
If you are Eclipse user (and your Eclipse has ADT plugin) you should open project which you want to test and then chose menu File → New → (Other → Android) → Android Test Project. Create test project for existing Android project. To run it select Run As --> Run As Android JUnit Test.
Other non-Eclipse) developers should use
androidtool from Android SDK. (In this example I use it in test project directory) :
android create test-project -p . -m ../HudsonMonitor
- after –p put the path to your test project directory
- after -m put the path to project which you will be testing
- after –n put the name of test project
To build it you should use
ant release(for release) but remember to add robotium.jar file to this project’s libs directory (if you don't do this you will have NoClassDefFoundError for com.jayway.android.robotium.solo.Solo class.
To install the application and the tests on emulator or device use:
adb -s emulator-5554 install -r bin\HudsonMonitor-debug.apk
adb -s emulator-5554 install -r bin\hudson_monitor_test-debug.apk
To run the tests:
adb shell am instrument
but remember to unlock emulator before you run the tests.
Creating Test Cases:
Ok, so now we should create class which will be implementing a test case. The only thing that we should know about the tested project is the name of the package and the name of the main class. Test case class in our test project looks like this:
Our application has four Tabs: “Statistic”, “Details”, “Webpage” and “Configuration”. One of ways to choose one of them is to click an element with text, for example choose “Configuration”:
The first test checks “adding new Hudson” use case. On configuration screen we have two EditTexts and a ListView of monitored Hudson's jobs. In first EditText the test should typing feed URL (ex. hudson:8080) for Hudson and in the second its name (e.g. hudson) which will be shown in our app to identify this Hudson instance. In Robotium widgets on the screen are numerated from top to bottom and from left to right and start form 0, so our code for this action is:
To add this Hudson to HudsonMonitor application we should click on ImageButton with plus sign . It is first ImageButton from top and left so it has number 0:
Then we must check whether the item with this Hudson feed URL has been added to the list:
The entire source code of this test is as follows:
Second test should check details of activity which has a Spinner and a table with details about builds. To choose an item from Spinner we can use the following method:
This method will only select one item from the Spinner, When we need check something on Spinner item's list we should use the following code:
To use this code we must import R from tested Project (HudsonMonitor). Using method
we can test every widget on the screen, but we must have more information about project which we are testing.
Robotium is very easy to use and can help to keep very high quality of your software throughout the project lifecycle. Bitbar has created an automated build and test environment for Android application developers who can build and test their applications on many configurations, real devices and targets that emulate real world devices.
In our Continuous Integration service for Android applications the developer can automate his testing process for multiple emulators of the real android devices.
You can specify your Android targets and all parameters for emulators which will be used for testing your application.
For every developer's code submission (commit or push) our system builds it and tests the application automatically. You can also see the test results in a human friendly form, including screenshots from testing process for each emulator.
If you have problems or questions about Robotium please put it in the blogpost comments and I will try to help you.
If you are interested in our Automated build and test System for Android applications, please contact us at info(at)bitbar.com.