Search code examples
androidui-automationandroid-espressohamcrest

Difference between onView(allOf(withId(R.id.login_card_view), isDisplayed())) and check(matches(isDisplayed()))


What is the difference between

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

and

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

Whats the use of isDisplayed() in 1 if I am doing the same thing in 2?


Solution

  • isDisplayed has different semantics in those two contexts.

    Imagine that your activity has no views. Look at this unit test test1, it will pass successfully, because you're asking espresso to find a view that has a specific text and is display. Well espresso didn't find that view, but there is no further checking so there is no exception and unit test function is fine

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

    But look at the following unit test test2, it will fail, because you're telling espresso to find a view that has a specific text then check if that view is displayed and that checking didn't pass

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

    I hope it is more clear now