Android Robotium测试框架

Robotium是一个扩展于JUnit的开源库,运用多种有用的方法来支持Android UI测试。它提供的强大的自动化黑箱测试范例,可用于Android应用(原生的和混合的)和web测试。只要源代码允许,你就可以通过Robotium写功能、系统和验收测试方案,以及测试应用。

 

1.Robotium---环境搭建

 

http://www.tuicool.com/articles/ZZvUbaz

 

2.常用语法

 

2.1.等待timeout毫秒一个名字为name的Activity启动:waitForActivity(String name, int timeout)

实例:assertTrue("无法启动启动类", solo.waitForActivity ("MainActivity", 30000));

 

2.2.Robotium将睡眠设置的毫秒数:sleep(inttime)

实例:solo.sleep(5000)

 

2.3.清空EditText的内容:clearEditText(android.widget.EditTexteditText)

实例:solo.clearEditText((EditText)solo.getView("edtInsertName"))

 

2.4.根据按钮上的文字点击按钮:clickOnButton(Stringtext)

实例:solo.clickOnButton("^绿色$");

 

2.5.根据文字点击控件:clickOnText(Stringtext)

实例:solo.clickOnText("控件上显示文字");

 

2.6.输入内容:enterText(android.widget.EditTexteditText, String text)

solo.enterText((EditText)solo.getView("edtInsertName"),"说些什么好呢?");

 

2.7.返回:goBack()

 

2.8.截屏并保存为设置的名字:takeScreenshot(Stringname)

默认保存在: /sdcard/Robotium-Screenshots/

 

 

2.9.解锁屏幕:unlockScreen()

 

 

3.编写Robotium测试程序

 

1)在测试方法前覆写父类的setUp()方法: 该方法用来初始化solo,绑定对应的Activity。

@Override  public void setUp() throws Exception {   

super.setUp();   

solo = new Solo(getInstrumentation(), getActivity());   

 

2)在测试方法后覆写父类的tearDown()方法: 

该方法用来清理资源垃圾,关闭activity。  

public void tearDown() throws Exception {    

solo.finishOpenedActivities(); 

}   

 

3)Solo类运用 

Solo类中提供了自动点击、取得、拖拽、搜索等各种方法。 声明Solo类型的成员变量private Solo solo; 

 

 

典型方法: 

① 点击: 

 

clickOnButton(int)—Clicks on a Button with a given index. 

clickOnButton(String)—Clicks on a Button with a given text. 

clickOnCheckBox(int)—Clicks on a CheckBox with a given index. 

clickOnView(View)—Clicks on a given View.  

 

clickOnText(String)—Clicks on a View displaying a given text. 

clickLongOnText(String)—Long clicks on a given View. 

 

clickOnRadioButton(int)—Clicks on a RadioButton with a given index. 

clickOnScreen(float, float)—Clicks on a given coordinate on the screen. 

 

② 取得: 

 

getCurrentActivity()—Returns the current Activity.  

 

getText(String)—Returns a TextView which shows a given text.  

getView(int)—Returns a View with a given id.  

 

getEditText(String)—Returns an EditText which shows a given text.   

getImage(int)—Returns an ImageView with a given index.  

③ 拖拽: 

drag(float, float, float, float, int)—Simulate touching a given location and dragging it to a new location. 

 

④ 搜索: 

 

searchText(String)—Searches for a text string and returns true if at least one item is found with the expected text. 

 

searchEditText(String)—Searches for a text string in the EditText objects located in the current Activity.  

searchButton(String, boolean)—Searches for a Button with the given text string and returns true if at least one Button is found. 

 

4)创建需要的测试方法  可以根据不同目的编写多个测试方法。注意方法名称必须以test开头,程序运行会自动调用以test开头的方法。每次调用测试方法都会运行一次测试工程。

 

 

4.如何获取控件ID-两种方法

 

(1)Android 实用工具Hierarchy Viewer实战

 

是随AndroidSDK发布的工具,位置在tools文件夹下,名为hierarchyviewer.bat

需要运行测试项目,在调试环境下才可以检测到模拟器的

(2)运行命令行记录log,然后点击对应Activity,接着可以在logcat看到

 

 

 

 

 

 

 

5.实战-针对APK进行的测试

 

robotium要求被测应用和测试代码要有一致的签名

 

 被测试项目为demo1,下面是实战的具体步骤

 

5.1. 配置ANDROID_HOME为android sdk的安卓目录,例如:D:\android-sdk

 

5.2. 在path下添加这两个:%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;

 

5.3. 需要把APK重新签名,因为robotium要求被测应用和测试代码要有一致的签名, 所以我们需要把下载到的apk,通过re-sign.jar来产生debug key的apk,这个重新生成的apk就会跟测试项目签名一致了

 

5.4. 下载完后,需要配置ANDROID_HOME,就是安卓SDK的位置,然后把APK拉到图标上,就会自动生成一个debug key的apk,如果无法直接单击re-sign.jar运行,需要切换到放置该jar文件的目录,cmd执行java -jar re-sign.jar产生新apk的过程中会弹出一个信息框,记得截下图,因为里面有两个信息我们等会的代码中需要用到

 

5.5. 安装产生的apk。然后打开模拟器(模拟器器一定要打开才能安装成功),然后打开命令行  adb install mitalk_debug.apk(新生成apk的名称) , 或者双击apk文件也可以安装

 

安装成功就可以再模拟器里看到该应用的图标了

 

5.6. 打开Eclipse,点击File->New一个AndroidTest Project  TestDemo1, 然后点击下一步的时候选择Thisproject(因为我们测试的是APK),然后选择要在哪个android版本上测试

 

5.7. 在该项目下创建一个包,com.example.demo1.test,在该包下创建TestDemo1Apk类,如下

 

package com.example.demo1.test;

 

import com.robotium.solo.Solo;

 

importandroid.test.ActivityInstrumentationTestCase2;

import android.widget.EditText;

 

@SuppressWarnings("rawtypes")

public class TestDemo1Apk extendsActivityInstrumentationTestCase2 {

 

       private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME ="com.example.demo1.MainActivity";//启动类

 

       private static Class<?> launcherActivityClass;

       static{

                try {

                        launcherActivityClass =Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);

                } catch (ClassNotFoundExceptione) {

                        throw newRuntimeException(e);

                }

       }

       

       @SuppressWarnings("unchecked")

       public TestDemo1Apk() throws ClassNotFoundException {

                super(launcherActivityClass);

       }

       

       private Solo solo;

       

       @Override

       protected void setUp() throws Exception {

                solo = newSolo(getInstrumentation(), getActivity());

       }

 

 

             public void testcase001() throwsException {

                        //等待  Activity "MainActivity" 启动

                       assertTrue("无法启动启动类",solo.waitForActivity("MainActivity", 30000));

           solo.sleep(5000);

 

         //输入文字:"131243"

           solo.enterText((EditText)solo.getView("edtInsertName"), "说些什么好呢?");

           solo.sleep(2000);

           

           //清空输入框的内容

           solo.clearEditText((EditText)solo.getView("edtInsertName"));

           

           

           //按下按钮 "绿色"

           solo.clickOnButton("^绿色$");

           solo.sleep(2000);

 

           //按下按钮 "黄色"

           solo.clickOnButton("^黄色$");

           solo.sleep(2000);

 

           //按下按钮 "蓝色"

           solo.clickOnButton("^蓝色$");

           solo.sleep(2000);

 

 

           //按下 TextView "看我变变变~~~"

           solo.clickOnText("^看我变变变~~~$");

           solo.sleep(5000);     

           

             }

            

 

  @Override

  public void tearDown() throws Exception {

                solo.finishOpenedActivities();

 

  }

 

 

}

 

 

5.8.右键该项目,选择property然后选择javabuild path, 选择 Add JARs,选择下到的robotium.jar

 

Add Library,点击Junit,选择Junit4

 

 

 

 

 

 

 

6.运行多个测试用例

http://jingyan.baidu.com/article/948f59240f687cd80ff5f921.html

 

 

 

 

7.命令行运行Android Robotium自动化用例或单元测试用例

http://blog.csdn.net/wirelessqa/article/details/8999433

 

1)运行所有的测试用例

举个栗子:运行测试工程下的所有用例

 

adb shell am instrument -wcom.taobao.taobao.test/android.test.InstrumentationTestRunner

 

 

2)运行单个测试类或某个TestSuite

举个栗子:运行测试类com.taobao.taobao.test.TestRegister

 

 

adb shell am instrument -e classcom.taobao.taobao.test.TestRegister -wcom.taobao.taobao.test/android.test.InstrumentationTestRunner

 

 

3)运行某个测试类里面的某个测试方法

举个栗子:运行com.taobao.taobao.test.TestRegister中的测试方法testRegister

 

adb shell am instrument -e classcom.taobao.taobao.test.TestRegister#testRegister -wcom.taobao.taobao.test/android.test.InstrumentationTestRunner

 

4)运行两个不同的测试类或类中的方法

举个栗子:运行com.taobao.taobao.test.TestLogin

 

和com.taobao.taobao.test.TestRegister类中的方法testRegister

 

 

 

 

adb shell am instrument -e classcom.taobao.taobao.test.TestLogin,com.taobao.taobao.test.TestRegister#testRegister  -wcom.taobao.taobao.test/android.test.InstrumentationTestRunner

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章