Espresso - withEffectiveVisibility vs isDisplayed

Daniel Gomez Rico picture Daniel Gomez Rico · Jul 6, 2015 · Viewed 7.5k times · Source

Whats´s the difference between isDisplayed and withEffectiveVisibility?

onView(withText("Much Dagger")).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));


onView(withText("Much Dagger")).check(matches(ViewMatchers.isDisplayed());

Answer

UDI picture UDI · Jul 20, 2015

According to the Documentation

Returns a matcher that matches {@link View}s that have "effective" visibility set to the given value. Effective visibility takes into account not only the view's visibility value, but also that of its ancestors. In case of View.VISIBLE, this means that the view and all of its ancestors have visibility=VISIBLE. In case of GONE and INVISIBLE, it's the opposite - any GONE or INVISIBLE parent will make all of its children have their effective visibility.

Note:

Contrary to what the name may imply, view visibility does not directly translate into whether the view is displayed on screen (use isDisplayed() for that). For example, the view and all of its ancestors can have visibility=VISIBLE, but the view may need to be scrolled to in order to be actually visible to the user. Unless you're specifically targeting the visibility value with your test, use isDisplayed.

So if using for verifying if a view is visible use isDisplayed() but for other verification incase if invisible and gone use withEffectiveVisibilty()