Espresso – how can you perform an OR ( || ) check?

Issue

I need only one of these two espresso checks to pass. How can I achieve this? If either of these ViewAssertions pass, I will be happy.

    onView(withId(R.id.mainview))
            .check(

                    matches(not(isDisplayed()))
            )

or

    onView(withId(R.id.mainview))
            .check(

                    doesNotExist()
            )

Solution

This isn’t the best method but it’ll work. Ideally you should use a custom view matcher… but we’re all lazy.

try {
  // See if view is displayed
  onView(withId(R.id.mainview))
          .check(
                  matches(not(isDisplayed()))
          );
}
catch (NoMatchingViewException e){
  // Otherwise check it doesn't exist
  onView(withId(R.id.mainview))
          .check(
                  doesNotExist()
          );
}

Answered By – stealthcopter

Leave a Comment