Chad Bingham Chad Bingham - 3 months ago 271
Android Question

Espresso: return boolean if view exists

I am trying to check to see if a view is displayed with Espresso. Here is some pseudo code to show what I am trying:

if (!Espresso.onView(withId(R.id.someID)).check(doesNotExist()){
// then do something
} else {
// do nothing, or what have you
}


But my problem is
.check(doesNotExist())
does not return boolean. It is just an assertion. With UiAutomator I was able to just do something like so:

if (UiAutomator.getbyId(SomeId).exists){
.....
}


I found this; check line 46. Still can't find out how to use it.

Answer

We need that functionality and I ended up implementing it below:

https://github.com/marcosdiez/espresso_clone

if(onView(withText("click OK to Continue")).exists()){ 
    doSomething(); 
} else { 
   doSomethingElse(); 
}

I hope it is useful for you.

Comments