Android的自动测试研究--Robotium

发表于:2013-05-29来源:Csdn作者:xyz_lmn点击数: 标签:Android
Robotium的jar包和程序例子地址: http://code.google.com/p/robotium/ Android的自动测试研究,我相信有很多在接触android的人当中,

  Robotium的jar包和程序例子地址:

  http://code.google.com/p/robotium/

  Android的自动测试研究,我相信有很多在接触android的人当中,也在网上不断的搜索和研究究竟有没有能应用android自动化测试手段,我也是如此,经过一番寻找研究有幸接触到了Robotium这个东东,他能一定程度上给我们一些想要的效果,现将小试牛刀的一些东东分享出来.

  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的例子,看看跑起来的效果是什么样的,后续继续做些更详细的。

  一、确保环境

  1.已经安装eclipse

  2.Eclipse已经配置好android相关环境

  3.存在已经创建模拟器

  二、步骤

  1.启动Eclipse执行 New --> Project --> Android Project --> Create Project from existing sample --> NotePad将自带的例子导入进来.

  2.在官方down栏下下载三个文件,两个是所需要的jar包,另一个是notepad的测试例子

  (当前最新的是2.5Featured,这里就不提供下载连接了版本更新的还是比较快的)

  3.然后执行File --> Import --> Existing Project into workspace --> Select archive file --> ExampleTestProject_v2.5.zip将例子导入进来(如果错误可能需要你在这个项目上将刚才说需要的两个jar导入project --> Properties --> Java Build Path --> Add (external) Jar)

  4.右键选择刚才导入的test例子执行Run As --> Run As Android JUnit Test.

  如果没有意外的话会自动启动模拟器并且运行起来了.

  http://www.devdiv.com/data/attachment/album/201110/11/155452zmygarwywa9e9zwv.png

  再欣赏一下代码:

  public class NotePadTest extends ActivityInstrumentationTestCase2{

  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");

  //Assert that NoteEditor activity is opened

  solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor");

  //In text field 0, add Note 1

  solo.enterText(0, "Note 1");

  solo.goBack();

  //Clicks on menu item

  solo.clickOnMenuItem("Add note");

  //In text field 0, add Note 2

  solo.enterText(0, "Note 2");

  //Go back to first activity named "NotesList"

  solo.goBackToActivity("NotesList");

  // solo.

  boolean expected = true;

  boolean actual = solo.searchText("Note 1") && solo.searchText("Note 2");

  //Assert that Note 1 & Note 2 are found

  assertEquals("Note 1 and/or Note 2 are not found", expected, actual);

  }

  @Smoke

  public void testEditNote() throws Exception {

  // Click on the second list line

  solo.clickInList(2);

  // Change orientation of activity

  solo.setActivityOrientation(Solo.LANDSCAPE);

  // Change title

  solo.clickOnMenuItem("Edit title");

  //In first text field (0), add test

  solo.enterText(0, " test");

  solo.goBackToActivity("NotesList");

  boolean expected = true;

  // (Regexp) case insensitive

  boolean actual = solo.searchText("(?i).*?note 1 test");

  //Assert that Note 1 test is found

  assertEquals("Note 1 test is not found", expected, actual);

  }

  @Smoke

  public void testRemoveNote() throws Exception {

  //(Regexp) case insensitive/text that contains "test"

  solo.clickOnText("(?i).*?test.*");

  //Delete Note 1 test

  solo.clickOnMenuItem("Delete");

  //Note 1 test & Note 2 should not be found

  boolean expected = false;

  boolean actual = solo.searchText("Note 1 test");

  //Assert that Note 1 test is not found

  assertEquals("Note 1 Test is found", expected, actual);

  solo.clickLongOnText("Note 2");

原文转自:http://blog.csdn.net/xyz_lmn/article/details/6899905