Robotium_Automated UI testing for Android applications with Robotium

转载 2012年03月30日 09:16:13

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
  • 在每次提交后自动执行UI测试(在Robotium上创建)
  • 针动不同的设置测试:真机和模拟器
  • 以可显化的方式显示测试结果
  • 从UI测试进程的屏幕截图可以看出你的程序在真机上运行的情况

    As an example of using Robotium I show you how to create tests for one of my Android applications used for monitoring of Hudson's jobs.

    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 android tool from Android SDK. (In this example I use it in test project directory) :

    android create test-project -p . -m ../HudsonMonitor
    -n HudsonMonitorTest

    - 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 like ant debug or 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 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:

    import; import; import android.test.ActivityInstrumentationTestCase2; import android.test.suitebuilder.annotation.Smoke; public class HudsonMonitorTest extends ActivityInstrumentationTestCase2{ private Solo solo; // this is Robotium class used to support test cases that span over multiple activities. public HudsonMonitorTest() { super("", HudsonMonitor.class); } public void setUp() throws Exception { super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); //takes in the instrumentation and the start activity. } @Override public void tearDown() throws Exception { try { solo.finalize(); } catch (Throwable e) { e.printStackTrace(); } getActivity().finish(); super.tearDown(); } }

    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:

    // 0 because it is the first EditText on screen solo.enterText(0, "hudson:8080"); // 1 because it is the second solo.enterText(1, "hudson");

    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:

    // for ImageButton use only clickOnImageButton method, // but for simple button use clickOnButton solo.clickOnImageButton(0);

    Then we must check whether the item with this Hudson feed URL has been added to the list:

    boolean expected = true; boolean actual = solo.searchText("(http://hudson:8080)"); assertEquals("Can't add new Hudson Instance", expected, actual);

    The entire source code of this test is as follows:

    @Smoke public void testAddNewHudson() throws Exception { solo.clickOnText("Configuration"); solo.enterText(0, "hudson:8080"); solo.enterText(1, "hudson"); solo.clickOnImageButton(0); boolean expected = true; boolean actual = solo.searchText("(http://hudson:8080)"); assertEquals("Can't add new Hudson Instance", expected, actual); }

    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:

    solo.pressSpinnerItem(0, 1);

    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:

    solo.clickOnView(solo.getCurrentActivity(). findViewById(; solo.clickInList(2);

    To use this code we must import R from tested Project (HudsonMonitor). Using method …OnView 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)

Android平台Robotium UI测试详解

Android平台Robotium UI测试详解Robotium 框架工作原理及实践Robotium 是什么一款面向Android端的开源自动化测试框架,,Robotium是基于Instrumenta...
  • cfy137000
  • cfy137000
  • 2017年03月09日 21:02
  • 871

Performance Testing Guidance for Web Applications
  • xdstone1
  • xdstone1
  • 2009年12月22日 10:11
  • 1335

Android automated testing (Robotium)

In the previous GWT project we worked with acceptance tests and the Robot Framework. The question I ...
  • gzh0222
  • gzh0222
  • 2012年02月07日 14:44
  • 1466

Android studio Robotium环境搭建-实测

robotium Recorder是付费的不然感觉只能用一周,下面是用robotium进行android工程的环境搭建。 借鉴了:
  • Blues1021
  • Blues1021
  • 2016年03月14日 11:43
  • 4320

Android Studio环境下基于Robotium的自动化黑盒测试

### 能力要求 1. 能使用Java、C++或其他程序设计语言编写程序 2. 能使用Python进行简单的脚本编写 3. 能使用一些简单的**Linux命令** *(Android Kernal i...
  • huashuncai123
  • huashuncai123
  • 2016年07月10日 16:41
  • 2583


Robotium是我认可度最高的一个android自动化测试工具,因为我在这里收获了UI自动化第一份自信。 这里不讲签名、创建工程等内容,直接对碰到的问题做一个总结,面试的时候通过这些问题可以看出是...
  • liaobingyun4
  • liaobingyun4
  • 2015年02月05日 22:35
  • 239


本人之前曾经撰文描述Appium和UIAutomator框架是如何定位Android界面上的控件的。 UIAutomator定位Android控件的方法实践和建议 Appium基于安卓的各种Fin...
  • zhubaitian
  • zhubaitian
  • 2014年10月05日 16:27
  • 9173


  • qingchunjun
  • qingchunjun
  • 2015年01月10日 09:40
  • 11275

Android studio 下的Robotium自动化测试

一、前言 Android Studio 是一个Android开发环境,作为官方主推的开发环境和停止对其他开发IDE的支持,Android Studio将成为今后唯一的android开发环境。...
  • qq_35081366
  • qq_35081366
  • 2016年12月29日 16:46
  • 1944

SpringBoot之Create Your Simple Application&TESTS

Application.javapackage com.zhu;import org.springframework.boot.SpringApplication; import org.spring...
  • zt_fucker
  • zt_fucker
  • 2017年03月02日 12:38
  • 257
您举报文章:Robotium_Automated UI testing for Android applications with Robotium