skip to Main Content

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


  1. Chosen as BEST ANSWER

    The solution was in giving an id to the parent layout closest to the problematic element, and then using withParent in a test.


  2. You can use the inRoot method provided by Espresso to specify the root view from which to search for a view.

    onView(withText("My Text"))
        .inRoot(withDecorView(not(is(activityTestRule.activity.window.decorView))))
        .perform(click());
    

    In here, the withDecorView method is used to specify the root view of the second layout, and not(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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search