onView(allOf(withId(R.id.login_card_view), isDisplayed())) 和 check(matches(isDisplayed())) 之间的区别

Sha*_*aja 3 android hamcrest ui-automation android-espresso

有什么区别

1.ViewInteraction v = onView(allOf(withId(R.id.login_card_view), isDisplayed()))

2.v.check(matches(isDisplayed()))

isDisplayed()如果我在 2 中做同样的事情,那么 1有什么用呢?

Wil*_*aan 6

isDisplayed在这两种上下文中具有不同的语义。

想象一下您的活动没有视图。看一下这个单元测试test1,它将成功通过,因为您要求 espresso 查找具有特定文本且为 display 的视图。嗯,espresso 没有找到那个视图,但是没有进一步检查,所以没有异常,单元测试功能也很好

@Test
public void test1() {
    Espresso.onView(Matchers.allOf(ViewMatchers.withText("bla bla lba") ,ViewMatchers.isDisplayed()));
}
Run Code Online (Sandbox Code Playgroud)

但是看看下面的单元测试test2,它会失败,因为你告诉 espresso 找到一个具有特定文本的视图,然后检查该视图是否显示并且检查没有通过

@Test
public void test2() {
    Espresso.onView(Matchers.allOf(ViewMatchers.withText("bla bla lba"))).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}
Run Code Online (Sandbox Code Playgroud)

我希望现在更清楚了