下面列出了android.support.test.espresso.Espresso#onView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static ViewInteraction[] getButtonInteractions() {
ViewInteraction[] buttonsInteractions = new ViewInteraction[10];
// We cannot rely on the withDigit() matcher to retrieve these because,
// after performing a click on a button, the time display will update to
// take on that button's digit text, and so withDigit() will return a matcher
// that matches multiple views with that digit text: the button
// itself and the time display. This will prevent us from performing
// validation on the same ViewInteractions later.
buttonsInteractions[0] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text10));
buttonsInteractions[1] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text0));
buttonsInteractions[2] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text1));
buttonsInteractions[3] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text2));
buttonsInteractions[4] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text3));
buttonsInteractions[5] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text4));
buttonsInteractions[6] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text5));
buttonsInteractions[7] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text6));
buttonsInteractions[8] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text7));
buttonsInteractions[9] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text8));
return buttonsInteractions;
}
private static void verifyViewEnabledStates(TestCase test) {
ViewInteraction[] buttonsInteractions = getButtonInteractions();
ViewInteraction[] altButtonsInteractions = getAltButtonInteractions();
for (int digit : test.sequence) {
buttonsInteractions[digit]
.check(ViewAssertions.matches(ViewMatchers.isEnabled()))
.perform(ViewActions.click());
}
for (int i = 0; i < 10; i++) {
buttonsInteractions[i].check(matchesIsEnabled(
i >= test.numberKeysEnabledStart && i < test.numberKeysEnabledEnd));
altButtonsInteractions[0].check(matchesIsEnabled(test.leftAltKeyEnabled));
altButtonsInteractions[1].check(matchesIsEnabled(test.rightAltKeyEnabled));
}
Espresso.onView(ViewMatchers.withText(android.R.string.ok))
.check(matchesIsEnabled(test.okButtonEnabled));
ViewInteraction backspaceInteraction = Espresso.onView(
ViewMatchers.withId(R.id.nptp_backspace));
// Reset after each iteration by backspacing on the button just clicked.
backspaceInteraction.check(matchesIsEnabled(true))
.perform(ViewActions.longClick())
.check(matchesIsEnabled(false));
}
@Test
public void ViewMatchers() {
Espresso.onView(ViewMatchers.withId(json.chao.com.wanandroid.R.id.button));
//onView内部最好不要使用withText()断言处理
Espresso.onView(Matchers.allOf(ViewMatchers.withId(json.chao.com.wanandroid.R.id.button), ViewMatchers.withText("HaHa")));
Espresso.onView(Matchers.allOf(ViewMatchers.withId(json.chao.com.wanandroid.R.id.button), Matchers.not(ViewMatchers.withText("HaHa"))));
}
@Test
public void hasSibling() {
Espresso.onView(Matchers.allOf(ViewMatchers.withText("7"),
ViewMatchers.hasSibling(ViewMatchers.withText("item: 1"))));
}
private static ViewInteraction[] getAltButtonInteractions() {
ViewInteraction[] buttonsInteractions = new ViewInteraction[2];
buttonsInteractions[0] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text9));
buttonsInteractions[1] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text11));
return buttonsInteractions;
}
public ViewInteraction drawerLayout() {
return Espresso.onView(ViewMatchers.withId(R.id.drawer_layout));
}
public ViewInteraction root() {
return Espresso.onView(ViewMatchers.withId(R.id.root));
}
public ViewInteraction hiddenToolbar() {
return Espresso.onView(ViewMatchers.withId(R.id.hidden_toolbar));
}
public ViewInteraction toolbarText() {
return Espresso.onView(ViewMatchers.withId(R.id.toolbar_text));
}
public ViewInteraction toolbar() {
return Espresso.onView(ViewMatchers.withId(R.id.toolbar));
}
public ViewInteraction toolbarDrawerToggle() {
return Espresso.onView(ViewMatchers.withId(R.id.toolbar_drawer_toggle));
}
public ViewInteraction toolbarGoPrevious() {
return Espresso.onView(ViewMatchers.withId(R.id.toolbar_go_previous));
}
public ViewInteraction loginView() {
return Espresso.onView(ViewMatchers.isAssignableFrom(LoginView.class));
}
public ViewInteraction username() {
return Espresso.onView(ViewMatchers.withId(R.id.login_username));
}
public ViewInteraction password() {
return Espresso.onView(ViewMatchers.withId(R.id.login_password));
}
public ViewInteraction loginButton() {
return Espresso.onView(ViewMatchers.withId(R.id.login_login));
}
public ViewInteraction repositoriesView() {
return Espresso.onView(ViewMatchers.isAssignableFrom(RepositoriesView.class));
}