Issue
I have the following toolbar in an Activity with two fragments:
When I navigate to the second fragment, there is an up button showing, and when coming back, it isn't there, which is correct.
I just want to assert that it is NOT there with Espresso
. To assert that it is there, I have used:
// The navigate up button should be displayed as a child of the toolbar
onView(allOf(instanceOf(AppCompatImageButton::class.java))).check(
matches(
ViewMatchers.withParent(
withId(R.id.toolbar)
)
)
)
However, I tried to negate it and do the following to assert that it isn't shown, but it doesn't work:
// The navigate up button should NOT be displayed as a child of the toolbar
onView(Matchers.anyOf(Matchers.instanceOf(AppCompatImageButton::class.java))).check(
matches(
Matchers.not(
withParent(withId(R.id.toolbar))
)
)
)
Any idea about how to assert it so that it works?
Thanks a lot in advance!
Solution
Assuming your original code returns a NoMatchingViewException
when the button doesn't exist, I'd wrap that in a try/catch. I'm simplifying my code below - you'd probably want to make this generic and parameterized for reuse.
private fun toolbarButtonExists(): Boolean {
try {
onView(allOf(instanceOf(AppCompatImageButton::class.java))).check(
matches(
ViewMatchers.withParent(
withId(R.id.toolbar)
)
)
)
return true
} catch (e: NoMatchingViewException) {
return false
}
}
I'd also suggest getting an id
assigned to that button because the way you're finding it is pretty brittle. Finding by id
would also let you use doesNotExist()
.
Answered By - Mike Collins
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.