The following statement does not work because doesNotExist()
returns a ViewAssertion
instead of a matcher. Any way to make it work without a try-catch
?
.check(either(matches(doesNotExist())).or(matches(not(isDisplayed()))));
The following statement does not work because doesNotExist()
returns a ViewAssertion
instead of a matcher. Any way to make it work without a try-catch
?
.check(either(matches(doesNotExist())).or(matches(not(isDisplayed()))));
not(isDisplayed)
is not perfect because i.e. the view might be displayed but below the screen in a ScrollView.
Simple checking view.getVisibility() != View.GONE
also is not a 100% solution. If view parent is hidden, the view is effectively hidden so the test should pass the scenario.
I suggest checking if view and its parents are visible:
fun isNotPresented(): ViewAssertion = object : ViewAssertion {
override fun check(view: View?, noViewFoundException: NoMatchingViewException?) {
if (view != null) {
if (view.visibility != View.VISIBLE) {
return
}
var searchView: View = view
while (searchView.parent != null && searchView.parent is View) {
searchView = searchView.parent as View
if (searchView.visibility != View.VISIBLE) {
return
}
}
assertThat<Boolean>(
"View is present in the hierarchy and it is visible" + HumanReadables.describe(view),
true,
`is`(false)
)
}
}
}
I had the same issue, one of my views will not have a certain view initially but could add it and hide it later. Which state the UI was in depended on wether background activities were destroyed.
I ended up just writing a variation on the implementation of doesNotExist:
public class ViewAssertions {
public static ViewAssertion doesNotExistOrGone() {
return new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noView) {
if (view != null && view.getVisibility() != View.GONE) {
assertThat("View is present in the hierarchy and not GONE: "
+ HumanReadables.describe(view), true, is(false));
}
}
};
}
}
If you want to check if a view doesn't not exist in the hierarchy, please use below assertion.
If you want to check if a view exist in the hierarchy but not displayed to the user, please use below assertion.
Hope this helps.