Android 自动化测试—robotium(一)

来源:互联网 发布:mac有什么游戏 编辑:程序博客网 时间:2024/05/21 22:27
Android 的开发可以说已经遍地都是,不说精致的app,只要看些书,看点教学视频,学习二至三个月,都可以随便开发几个小项目,当然只能是自娱自乐的。最近突然想起了,关于android 的自动化测试,于是网上搜了相关资料学习,最后决定先尝试 robotium。

robotium wiki:http://code.google.com/p/robotium/w/list

 

这里有篇文章对于robotium的介绍很贴切:robotium 是 android 自带类 Instrumentation 的一个封装,方便测试人员直接调用封装好的接口,也就是说,实际上我们直接使用Instrumentation 也能够进行自动化测试,但robotium可以简化我们的测试步骤,我们只需要调用某个robotium的API,传几个参数,就等于我们在调用一部分的Instrumentation帮我们实现测试。robotium 就是富二代!!高帅富!!

http://www.51testing.com/?uid-22381-action-viewspace-itemid-238847

 

需要注意:

1.测试项目:例如:HelloWorldTest,Build Path需要导入robotium-solo.jar包

2.Eclipse:3.7 版本,需要勾选Order and Export中的内容

# package com.luwenjie.helloworld.test; #  # import android.test.ActivityInstrumentationTestCase2; # import com.luwenjie.helloworld.HelloWorldActivity; # import com.jayway.android.robotium.solo.Solo; #  # public class HelloWorldTest extends ActivityInstrumentationTestCase2# <HelloWorldActivity>{ #  #     private Solo solo; #  # //需要测试的app是什么?# //这里需要测试com.luwenjie.helloworld包下的HelloWorldActivity这个应用#  #     public HelloWorldTest(){ #          super("com.luwenjie.helloworld", HelloWorldActivity.class); #     } #    # //打开HelloWorld这个应用#  #     public void setUp() throws Exception{ #          solo = new Solo(getInstrumentation(), getActivity()); #     } #  # //执行测试# //searchText(String str):验证字符串是否存在#  #     public void testUI() throws Exception { #         boolean expected = true; #         boolean actual = solo.searchText("Hello") && solo.searchText("World"); #  #         assertEquals("This and/or is are not found", expected, actual); #     } # } 

花了一点时间写了一个计算标准体重的小应用,当然目的是为了测试 robotium 的使用情况。经过一段泡在robotium的API文档上,对一些基本操作也有所了解,开始了更进一步的尝试。

robotium API:http://code.google.com/p/robotium/downloads/list

虽然API文档已经把相关知识点解释的很全,作为学习还需不断使用理解巩固知识

以下先简单介绍一部分API

 

// 单击一个单选按钮

clickOnRadioButton(int index)

index:用来标识哪个RadioButton, 只有1个RadioButton,index = 0 以此类推

 

// 单击一个EditText表单

clickOnEditText(int index)

index: 用来标识哪个EditText,只有1个EditText, index = 0 以此类推

 

// 在EditText中输入Text

enterText(int index, String text)

index: 用来标识哪个EditText

text : 输入的内容

 

// 单击一个按钮

clickOnButton(String name)

name : 按钮的名称

 

// 返回上一页

goBack()

 

// 清空EditText表单

clearEditText(int index)

index: 用来标识哪个EditText

   1. package com.luwenjie.standweight.test;    2.     3. import android.test.ActivityInstrumentationTestCase2;    4. import com.luwenjie.standweight.StandWeightActivity;    5. import com.jayway.android.robotium.solo.Solo;    6.     7. public class weightText extends ActivityInstrumentationTestCase2<StandWeightActivity> {    8.     private Solo solo;    9.     public weightText() {   10.         super("com.luwenjie.standweight", StandWeightActivity.class);   11.     }   12.        13.     public void setUp() throws Exception{   14.          solo = new Solo(getInstrumentation(), getActivity());      15.     }   16.        17.     public void testUI() throws Exception {   18.         boolean expected = true;   19.            20.         //验证男孩180cm的标准体重为70公斤   21.         solo.clickOnRadioButton(0);   22.         solo.clickOnEditText(0);   23.         solo.enterText(0, "180");   24.         solo.clickOnButton("计算");   25.         boolean actual1 = solo.searchText("70.00");   26.         assertEquals("This and/or is are not found", expected, actual1);   27.            28.         //返回清空editText表单   29.         solo.goBack();   30.         solo.clearEditText(0);   31.            32.         //验证女孩160cm的标准体重为70公斤   33.         solo.clickOnRadioButton(1);   34.         solo.clickOnEditText(0);   35.         solo.enterText(0, "160");   36.         solo.clickOnButton("计算");   37.         boolean actual2 = solo.searchText("54.00");   38.         assertEquals("This and/or is are not found", expected, actual2);   39.     }   40. } 

为了更好的尝试Robotium的AIP,自己编写了部分android控件,提供测试。

EditText 控件:

操作步骤:

1.单击 EditText 控件

2.输入文字内容:This is EditTextActivity

3.单击 Submit 按钮

验证:页面返回文字:This is EditTextActivity

   1. public void testUI() throws Exception {    2.     this.EditText();    3. }    4.     5. public void EditText(){    6.     boolean expected = true;    7.     solo.clickOnButton("EditText");    8.     solo.enterText(0, "This is EditTextActivity");    9.     solo.clickOnButton("Submit");   10.     boolean actual = solo.searchText("This is EditTextActivity");   11.     assertEquals("This is not found",expected,actual);   12.     solo.goBack();   13. } 

assertEquals 拥有三个参数

assertEquals(String message,boolean expected, boolean actual)

message:出错时返回的信息

expected:预期结果,是个布尔值

actual:实际结果,也是个布尔值

如果 expected 和 actual 的值相同时(可以都为false),测试通过,否则失败。


CheckBox 控件:

单击CheckBox1勾选              再次单击CheckBox取消勾选

     

操作步骤:

1. 单击 CheckBox 1 复选框

验证:返回提示: Check Box 1被选中

2. 再次单击 Check Box1 复选框(达到取消勾选效果)

验证:返回提示:Check Box 1取消选中

   1. public void testUI() throws Exception {    2.     this.CheckBox();    3. }    4.     5. public void CheckBox(){    6.     boolean expected = true;    7.     solo.clickOnButton("CheckBox");    8.         9.     solo.clickOnCheckBox(0);   10.     boolean actual = solo.searchText("Check Box 1被选中");   11.     assertEquals("This is not found",expected,actual);   12.        13.     solo.clickOnCheckBox(0);   14.     boolean actual2 = solo.searchText("Check Box 1取消选中");   15.     assertEquals("This is not found",expected,actual2);   16.     solo.goBack();   17. } 

这里solo.clickOnCheckBox(0) 代表第一个CheckBox元素:Check Box 1

以此类推,1 代表第二个CheckBox元素:Check Box 2

CheckBox实现:http://luwenjie.blog.51cto.com/925779/915848

Spinner 控件:

默认显示:【选择的是:北京】

操作步骤:

1.点击【城市】下拉框

2.选择【上海】

验证:TextView 显示:【选择的是:上海】

   1. public void testUI() throws Exception {    2.        this.Spinner();    3. }    4.     5. public void Spinner(){    6.     solo.clickOnButton("Spinner");    7.     boolean actual = solo.isSpinnerTextSelected(0,"北京");    8.     9.     solo.pressSpinnerItem(0, 1);   10.     boolean actual1 = solo.searchText("选择的是:上海");   11.     assertEquals("This is not found",true, actual1);   12. } 

isSpinnerTextSelected 拥有两个参数

public boolean isSpinnerTextSelected(int index, String text)

index:定位选择的Spinner,第一个为0

text:所选择的Spinner存在的文本

这个API返回的是一个布尔值,当满足条件时返回true

 

pressSpinnerItem 拥有两个参数

public void pressSpinnerItem(int spinnerIndex, int itemIndex)

spinnerIndex:定位要使用的Spinner,第一个为0

intemIndex:定位所要按下的下拉项,如图:北京=0 上海=1 天津=2 ....


原创粉丝点击