有没有办法使用Espresso测试小吃店出现的正确文字?
我有一个简单的电话来创建一个小吃吧
Snackbar.make(mView, "My text", Snackbar.LENGTH_LONG).show();
我没有运气试过这个
onView(withText("My text")).inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));
答案 0 :(得分:59)
这对我有用,请尝试。
onView(allOf(withId(android.support.design.R.id.snackbar_text), withText("My text")))
.check(matches(isDisplayed()));
如果您使用AndroidX,请使用以下内容:
onView(withId(com.google.android.material.R.id.snackbar_text))
.check(matches(withText(R.string.whatever_is_your_text)))
答案 1 :(得分:12)
替代
private void checkSnackBarDisplayedByMessage(
@StringRes int message) {
onView(withText(message))
.check(matches(withEffectiveVisibility(
ViewMatchers.Visibility.VISIBLE
)));
}
答案 2 :(得分:0)
I saw the previous answers but I thought this would be better.
@Test
public void onFabClick_shouldDisplaySnackbar() throws Exception {
onView(withId(R.id.fab)).perform(click());
// Compare with the text message of snackbar
onView(withText(R.string.snackbar_message))
.check(matches(isDisplayed()));
}