/* * This is an example test project created in Eclipse to test NotePad which is a sample * project located in AndroidSDK/samples/android-11/NotePad * Just click on File --> New --> Project --> Android Project --> Create Project from existing source and * select NotePad. * * Then you can run these test cases either on the emulator or on device. You right click * the test project and select Run As --> Run As Android JUnit Test * * @author Renas Reda, [email protected] * */ //测试用包 package com.jayway.test; //引入需要测试的工程 import com.example.android.notepad.NotesList; //引入测试用类 import com.jayway.android.robotium.solo.Solo; //引入测试工程要继承用来测试activity的父类 import android.test.ActivityInstrumentationTestCase2; import android.test.suitebuilder.annotation.Smoke;
//泛型写需要测试的工程的入口activity名NotesList。 public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList>{
//Solo类运用 //Solo类中提供了自动点击、取得、拖拽、搜索等各种方法。 //声明Solo类型的成员变量private Solo solo; private Solo solo; //修改构造方法,super要测试的用例包名和类名 public NotePadTest() { super("com.example.android.notepad", NotesList.class); } //在测试方法前覆写父类的setUp()方法:该方法用来初始化solo,绑定对应的Activity public void setUp() throws Exception { solo = new Solo(getInstrumentation(), getActivity()); } //1.可以根据不同目的编写多个测试方法。注意方法名称必须以test开头,程序运行会自动调用以test开头的方法。 //2.每次调用测试方法都会运行一次测试工程。 //创建需要的测试方法:主要目的是测试添加文本的功能 @Smoke public void testAddNote() throws Exception { solo.clickOnMenuItem("Add note"); //确信当前的 NoteEditor activity 是活动状态 solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); //在编辑区第一行输入: Note 1 solo.enterText(0, "Note 1"); //点击键盘的返回按钮 solo.goBack(); //点击菜单按钮 solo.clickOnMenuItem("Add note"); //在编辑区第一行输入:Note 2 solo.enterText(0, "Note 2"); //返回到活动 "NotesList"的activity页面 solo.goBackToActivity("NotesList"); //定义预期结果为真 boolean expected = true; //当前页面搜索"Note 1"和"Note 2"内容 boolean actual = solo.searchText("Note 1") && solo.searchText("Note 2"); //预期 " Note 1 & Note 2"能被找到,显示正确的运行结果 assertEquals("Note 1 and/or Note 2 are not found", expected, actual);
} //创建需要的测试方法:主要目的是测试编辑文本的功能 @Smoke public void testEditNote() throws Exception { // 点击列表第二行 solo.clickInList(2); // 改变当前activity页面的显示方式 //LANDSCAPE:横向显示 Portrait:竖向显示 solo.setActivityOrientation(Solo.LANDSCAPE); // 点击菜单按钮:编辑按钮 solo.clickOnMenuItem("Edit title"); //在第一个编辑区添加文字: test solo.enterText(0, " test"); solo.goBack(); boolean expected = true; // 正则表达式用例(大小写不敏感) boolean actual = solo.waitForText("(?i).*?note 1 test"); //Note 1 test 被找到,用例执行成功 assertEquals("Note 1 test is not found", expected, actual);
}
//创建需要的测试方法:主要目的是测试删除文本的功能 @Smoke public void testRemoveNote() throws Exception { //(正则表达式)不区分大小写/点击包含"test"的文本 solo.clickOnText("(?i).*?test.*"); //删除Note 1 test solo.clickOnMenuItem("Delete"); //Note 1 test & Note 2 should 没有被找到,定义预期为假 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"); //点击删除按钮 solo.clickOnText("(?i).*?Delete.*"); actual = solo.searchText("Note 2"); //Assert that Note 2 is not found assertEquals("Note 2 is found", expected, actual); } //在测试方法后重写父类的tearDown()方法:该方法用来清理资源垃圾,关闭activity。 @Override public void tearDown() throws Exception { //Robotium will finish all the activities that have been opened solo.finishOpenedActivities(); } }