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 chosemenu 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 useandroidtool 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 useantlikeant debugorant 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
    -w com.android.hudson.tests/android.test.InstrumentationTestRunner

    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                         com.android.hudson.HudsonMonitor;                         import                         com.jayway.android.robotium.solo.Solo;                         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                        (                        "                        com.android.hudson                        "                        , 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”:

                                                solo.clickOnText(                        "                        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(R.id.SpinnerDetailList)); solo.clickInList(                        2                        );                    
    复制代码

    To use this code we must import R from tested Project (HudsonMonitor). Using method…OnViewwe can test every widget on the screen, but we must have more information about project which we are testing.

    Summary

    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 atinfo(at)bitbar.com.

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Android(安卓)系统编译环境设置及源代码编译
  3. android 添加混淆
  4. DrawerLayout中加入多个View
  5. AndroidManifest.xml简单解析
  6. Android(安卓)JazzyViewPager
  7. Android(安卓)线性布局 LinearLayout
  8. [置顶] Android实用视图动画及工具系列之六:通用表情栏,仿QQ微信聊
  9. 【Android(安卓)电量优化】电量优化特性 ( Doze 低电耗模式 | St

随机推荐

  1. Android(安卓)sdk content loader 0%的解
  2. 又优化了一下Android(安卓)ListView 异步
  3. AutoCAD即将为Android智能手机和平板电脑
  4. JNI和NDK学习(1)--搭建开发环境
  5. Android(安卓)解决android4.0系统中菜单(
  6. android 中vector的用法
  7. 记录WebView从开始加载到渲染结束的时间
  8. 使用Android辅助服务AccessibilityServic
  9. android studio 中aidl文件的使用并且实
  10. H5无法调起android app 的坑之 scheme 大