Android的自动测试研究,我相信有很多在接触android的人当中,也在网上不断的所搜和研究究竟有没有能应用android自动化测试手段,我也是如此,经过一番寻找研究有幸接触到了Robotium这个东东,他能一定程度上给我们一些想要的效果,现将小试牛刀的一些东东分享出来.
User scenario testing for Android
Robotium is a test framework created to make it easy to write powerful and robust automatic black-box test cases for Android applications. With the support of Robotium, test case developers can write function, system and acceptance test scenarios, spanning multiple Android activities.
Robotium has full support for Activities, Dialogs, Toasts, Menus and Context Menus.
上边的一段英文是官方一段文字,也就是我们利用Robotium可以做一些对android的一些自动化测试工作,并且他是可跨越多个Activites,且容易写一些测试case的,一定程度上能做一些黑盒的自动化工作,在这里先简单跑一个notpad的例子,看看跑起来的效果是什么样的,后续继续做些更详细的。
官方地址:http://code.google.com/p/robotium/
一、确保环境
1.已经安装eclipse
2.Eclipse已经配置好android相关环境
3.存在已经创建模拟器
二、步骤
1.启动Eclipse执行 New --> Project --> Android Project --> Create Project from existing sample --> NotePad将自带的例子导入进来.
2.在官方down栏下下载三个文件,两个是所需要的jar包,另一个是notepad的测试例子
(当前最新的是2.1Featured,这里就不提供下载连接了版本更新的还是比较快的)
3.然后执行File --> Import --> Existing Project into workspace --> Select archive file --> ExampleTestProject_v2.1.zip将例子导入进来(如果错误可能需要你在这个项目上将刚才说需要的两个jar导入project --> Properties --> Java Build Path --> Add (external) Jar)
4.右键选择刚才导入的test例子执行Run As --> Run As Android JUnit Test.
如果没有意外的话会自动启动模拟器并且运行起来了.
上一张截图看看样子把
再一睹为快测试的代码:
private Solo solo;
public NotePadTest() {
super("com.example.android.notepad", NotesList.class);
}
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
@Smoke
public void testAddNote() throws Exception {
solo.clickOnMenuItem("Add note");
solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); //Assert that NoteEditor activity is opened
solo.enterText(0, "Note 1"); //In text field 0, add Note 1
solo.goBack(); //Go back
solo.clickOnMenuItem("Add note"); //Clicks on menu item
solo.enterText(0, "Note 2"); //In text field 0, add Note 2
solo.goBackToActivity("NotesList"); //Go back to first activity named "NotesList"
boolean expected = true;
boolean actual = solo.searchText("Note 1") && solo.searchText("Note 2");
assertEquals("Note 1 and/or Note 2 are not found", expected, actual); //Assert that Note 1 & Note 2 are found
}
@Smoke
public void testEditNote() throws Exception {
solo.clickInList(2); // Clicks on the second list line
solo.setActivityOrientation(Solo.LANDSCAPE); // Change orientation of activity
solo.clickOnMenuItem("Edit title"); // Change title
solo.enterText(0, " test"); //In first text field (0), add test.
solo.goBackToActivity("NotesList");
boolean expected = true;
boolean actual = solo.searchText("(?i).*?note 1 test"); // (Regexp) case insensitive // insensitive
assertEquals("Note 1 test is not found", expected, actual); //Assert that Note 1 test is found
}
@Smoke
public void testRemoveNote() throws Exception {
solo.clickOnText("(?i).*?test.*"); //(Regexp) case insensitive/text that contains "test"
solo.clickOnMenuItem("Delete"); //Delete Note 1 test
boolean expected = false; //Note 1 test & Note 2 should not be found
boolean actual = solo.searchText("Note 1 test");
assertEquals("Note 1 Test is found", expected, actual); //Assert that Note 1 test is not found
solo.clickLongOnText("Note 2");
solo.clickOnText("(?i).*?Delete.*"); //Clicks on Delete in the context menu
actual = solo.searchText("Note 2");
assertEquals("Note 2 is found", expected, actual); //Assert that Note 2 is not found
}
@Override
public void tearDown() throws Exception {
try {
solo.finalize(); //Robotium will finish all the activities that have been open
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}