0

I need to implement in Espresso something like this:

if (!onView(withId(R.id.someID)).check(Exist()){
// push button
} else {
 // select check box
}

I have checked this post: Espresso: return boolean if view exists and would like example how to implement second answer form ValeraZakharov, because I've tried to implement myself but with little success.

Laurel
  • 5,965
  • 14
  • 31
  • 57
user3333277
  • 1
  • 1
  • 2

1 Answers1

1

example implementation could be

public void testSomthing() {
  if (!doesViewExist(R.id.someID)) {
    // push button
  } else {
    // select check box
  }
}

public boolean doesViewExist(int id) {
  try {
    onView(withId(id)).check(matches(isDisplayed()));
    return true;
  } catch (NoMatchingViewException e) {
    return false;
  }
}
nenick
  • 7,340
  • 3
  • 31
  • 23
  • 1
    This doesn't work for me. The framework catches the exception & finishes the activity before it can be caught by this code. – Gary99 Jul 25 '17 at 21:32
  • @Gary99 - Notice the NoMatching is an exception but ViewAssertion is an error. Use the catch(Throwable t) and try it again. – MG Developer Jan 02 '18 at 23:48