|
这几天一直在学习robotium,但是在网上找了很久除了一个测试noteslist的例子外,其他基本上没有多少资料,
但是网上关于noteslist的例子在我这不知道为什么老是行不同,于是就觉得自己弄,不按照网上的提供的方法
了,经过2天的摸索和研究终于在今天搞定,下面就把我的成果分享给大家:
1.启动Eclipse执行 New --> Project --> Android Project --> Create Project from existing sample --> NotePad将
自带的例子导入进来.
2.将robotium导入到刚新建的工程中如图
3、在该工程中新建一个类并将从robotium官网下载的例子粘贴到刚建的类中,如图
4、在AndroidManifest.xml中添加如下内容:
<uses-library android:name="android.test.runner" />
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10"/>
<instrumentation android:targetPackage="com.example.android.notepad" //要测试的包
android:name="android.test.InstrumentationTestRunner" />
5、现在就可以运行了,下面是我的测试结果:
6、为了熟悉这些测试代码自己尝试了修改源代码,修改够的内容如下:
- package com.jayway.test;
- import com.example.android.notepad.NotesList;
- import com.jayway.android.robotium.solo.Solo;
- import android.test.ActivityInstrumentationTestCase2;
- import android.test.suitebuilder.annotation.Smoke;
- public class NotePadtest extends ActivityInstrumentationTestCase2<NotesList>{
- 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, "老郭的第一个测试 ");
- solo.goBack();
- //Clicks on menu item
- solo.clickOnMenuItem("Add note");
- //In text field 0, add Note 2
- solo.enterText(0, "老郭的第二个测试 ");
- //Go back to first activity named "NotesList"
- solo.goBackToActivity("NotesList");
- //solo.goBackToActivity("NotesList");
- boolean expected = true;
- boolean actual = solo.searchText("老郭的第一个测试 ") && solo.searchText("老郭的第二个测试 ");
- //Assert that Note 1 & Note 2 are found
- assertEquals("Note 1 and/or Note 2 are not found", expected, actual);
- System.out.println("添加功能测试的实际结果是:"+actual+" "+"预期结果是:"+expected);
- }
-
- @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).*? test");
- //Assert that Note 1 test is found
- assertEquals("Note 1 test is not found", expected, actual);
- System.out.println("编辑功能测试的实际结果是:"+actual+" "+"预期结果是:"+expected);
- }
- @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("(?i).*?test.*");
- //Assert that Note 1 test is not found
- assertEquals("Note 1 Test is found", expected, actual);
- solo.clickLongOnText("老郭的第二个测试");
- //Clicks on Delete in the context menu
- solo.clickOnText("(?i).*?Delete.*");
- actual = solo.searchText("老郭的第二个测试 ");
- //Assert that Note 2 is not found
- assertEquals("Note 2 is found", expected, actual);
- System.out.println("删除功能测试的实际结果是:"+actual+" "+"预期结果是:"+expected);
- }
- @Override
- public void tearDown() throws Exception {
- try {
- //Robotium will finish all the activities that have been opened
- solo.finalize();
- } catch (Throwable e) {
- e.printStackTrace();
- }
- getActivity().finish();
- super.tearDown();
- }
- }
- 需要注意的地方:
-
- 要做一点修改。 因为noteslist是在androidV21开发的,而我的测试代码是V23的。我们最好要改成一致的。
- 修改 noteslisttest 下的AndroidManifest.xml
- <uses-sdk android:minSdkVersion="9" />
- 改成<uses-sdk android:minSdkVersion="7" />
-
- 这两个数字表示什么意思呢?
- 7--androidV21,9--androidV23,最低版本是3--AndroidV15.
- 大家按顺序排就知道哪个数字对应的版本了
-
- 然后在 noteslisttest 右击选中Properties--Android,选中AndroidV21
- 这样noteslisttest 里带的android jar 由android2.3 变为android2.1
-
- 再说一个配置,我觉得也很重要
- 还是在AndroidManifest.xml 里
- <instrumentation android:targetPackage="com.example.android.notepad" android:name="android.test.InstrumentationTestRunner" />
- 红色加粗的字符串表示我们要测试代码的package
-
- 测试代码讲解:
- 下面我们看看 noteslisttest 里的具体代码,看看它是怎么测试的
-
- // 告知系统我要测试的app是什么
-
- public NotePadTest() {
- super("com.example.android.notepad", NotesList.class);
- }
- //打开noteslist
- public void setUp() throws Exception {
- solo = new Solo(getInstrumentation(), getActivity());
- }
-
- 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");
- 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);
- }
- 这是我们第一个case,主要目的是测试添加文本的功能
- clickOnMenuItem(String)
- 功能是点击Menu按钮,选择文本描述为String的菜单,如我们的例子是"Add note"
- assertCurrentActivity(String message,String name)
- 这个是判断当前的activity是否和我预期的一致
- message是描述性的文字
- name是指activity的名字
- 关于如何知道activity 名字,我找了半天的文档,目前的方法是得看源码中的 AndroidManifest.xml--Application label--Application Nodes,在那里我们可以看到所有的activity的name
-
- enterText(int index,string text)
- index用来标识写到哪个EditText中。如果当前只打开一个EditText,那index=0
- text:就是我们要写入的内容
-
- goBack()
- 相当于手机上的 返回键(back key)
-
- goBackToActivity(String name)
- 返回到指定的activity
-
- searchText(String text)
- 在当前的activity中搜索是否含有text的内容
-
- 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 found", expected, actual);
- }
- 第二个case,主要是测试编辑功能的
- clickInList(int index)
- 点击list表的第index行,进入该文本界面
- solo.setActivityOrientation(Solo.LANDSCAPE);
- setActivityOrientation,设置手机屏幕显示方式
- LANDSCAPE:横向显示
- Portrait:竖向显示
-
-
- 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 not found", expected, actual);
- solo.clickLongOnText("Note 2");
- //Clicks on Delete in the context menu
- solo.clickOnText("(?i).*?Delete.*");
- actual = solo.searchText("Note 2");
- //Assert that Note 2 is not found
- assertEquals("Note 2 is not found", expected, actual);
- }
复制代码
第三个case,是用来测试删除功能的
clickOnText(String text)
点击包含该文字的地方
其中text可以用正则表达式表示
(?i)----忽略大小写。默认情况是大小写敏感的。
正则表达式与java保持一致
clickLongOnText(String text)
长时间按住所选的文字
例子看上去还比较简单。学了几个函数方法。
|
|