In Espresso, specify that you want to work with the currently visible AdapterView

In the exercise, I have ViewPagertwo tabs: ALL and Filtered.
Both pages use the same one Fragmentto display data with the difference that the "Filtered" page filters data according to some criteria.

I want to click an element on the ALL page (which may also exist on the Filtered page), and if I do this:

onData(transactionWithId(960L)).perform(click());

In response, I get:

AmbiguousViewMatcherException: 'can be assigned from a class: class android.widget.AdapterView' corresponds to several views in the hierarchy

Then I tried to clarify my description, indicating an additional restriction that I was looking for a visible element:

onData(allOf(transactionWithId(960L), isDisplayed())).perform(click());

and I got the same error.

- , "" ( , ):

onData(allOf(
      transactionWithId(960L), 
      withParent(withText("ALL")))
   ).perform(click());

.

, AdapterView, :

onData(allOf(
       is(instanceOf(Transaction.class)), 
       transactionWithId(960L))
   ).inAdapterView(allOf(
            isAssignableFrom(AdapterView.class),
            isDisplayed())
          ).perform(click());

:

PerformException: 'load adapter data' on view '(is : class android.widget.AdapterView ).

, , Activity ListView, , , - ViewPager , Fragment .
.

+4
1

, .

, . . DataInteration inAdapterView() Matcher . - , :

:

boolean isFiltered = ...
AdapterView av = ...
av.setTag(isFiltered);

:

@Test
public void testClickOnSecondItemInAllTab() {
    onData(instanceOf(String.class)).inAdapterView(withTag(false)) //
                                    .atPosition(1)                 //
                                    .perform(click());
}

:

static Matcher<View> withTag(final Object tag) {
    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(final Description description) {
            description.appendText("has tag equals to: " + tag);
        }

        @Override
        protected boolean matchesSafely(final View view) {
            Object viewTag = view.getTag();
            if (viewTag == null) {
                return tag == null;
            }

            return viewTag.equals(tag);
        }
    };
}
+3

All Articles