I am currently in the process of writing Android Espresso tests for my application.
The issue is, I have an item with the same name in two layouts (both are being used by my main activity, so when i launch main activity using @get:Rule annotation I get both layouts). I am trying to write a test for it, but, as expected, it shows AmbigiousViewMatcherExpression.
How can i specify to which item i am referring to, without changing the name of the item in one of the layouts?
Thanks in advance!
2
Answers
The solution was in giving an id to the parent layout closest to the problematic element, and then using withParent in a test.
You can use the
inRoot
method provided by Espresso to specify the root view from which to search for a view.In here, the
withDecorView
method is used to specify the root view of the second layout, andnot(is(activityTestRule.activity.window.decorView))
is used to exclude the first layout as the root view. This way, Espresso will search for the view only in the second layout.You can adapt this approach to your specific case by using the appropriate view matcher to identify the views you want to interact with, and by specifying the root view using the
inRoot
method.