通过实例来分析Espreeso的使用
<span style="font-size:10px;">import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
public class Tes extends ActivityInstrumentationTestCase2{
private static final String TARGET_PACKAGE_ID = "com.example.android.notepad" ;
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.example.android.notepad.NotesList" ;
private static Class<?> launcherActivityClass;
static {
try {
launcherActivityClass =
Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
//add one note
public void testCreateNote(){
//input sth in inputfiled
String text = "this is" ;
<span style="color:#3366FF;">//get object from </span></span><span style="font-size:10px;color:#3366FF;">withId</span><span style="color:#3366FF;"> </span>
<span style="font-size:10px;"> ViewInteraction create_button = onView(ViewMatchers.withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.example.android.notepad:id/menu_add", null, null)));
<span style="color:#3366FF;">//Open new note view</span>
create_button.perform(ViewActions.click()) ;
ViewInteraction inputFiled = onView(ViewMatchers.withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.example.android.notepad:id/note", null, null)));
inputFiled.perform(ViewActions.typeText(text)) ;
<span style="color:#3366FF;">//save this note and quite this note</span>
this.quitNote();
//verify this is new note
ViewInteraction new_note = onView(ViewMatchers.withText(text));
new_note.check(ViewAssertions.matches((ViewMatchers.isDisplayed()))) ;
}
//update note
public void testUpdateNote(){
//根据列表内note的名字,获取该对象,然后进行点击操作
String name = "D" ;
String updateName = "update" ;
<span style="color:#3366FF;">//get boject from text</span>
ViewInteraction note = onView(ViewMatchers.withText(name));
note.perform(ViewActions.longClick()) ;
ViewInteraction edit = onView(ViewMatchers.withText("Edit title"));
//update note
edit.perform(ViewActions.click()) ;
ViewInteraction editFiled = onView(ViewMatchers.withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.example.android.notepad:id/title", null, null)));
editFiled.perform(ViewActions.typeText(updateName)) ;
//save this note and quite this note
this.okButton();
<span style="color:#3366FF;">//verify this is new note</span>
ViewInteraction new_note = onView(ViewMatchers.withText(name + updateName));
new_note.check(ViewAssertions.matches((ViewMatchers.isDisplayed()))) ;
}
}</span>
然后补充以下内容:
ViewMatchers
下面介绍ViewMatchers的一些常用方法
1、