Espresso - Click by text in List view

后端 未结 3 1651
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 12:54

I am trying to click on a text in a list view using Espresso. I know they have this guide, but I can\'t see how to make this work by looking for text. This is what I have t

相关标签:
3条回答
  • 2020-12-13 13:34

    If adapter have custom model class for example Item:

    public static Matcher<Object> withItemValue(final String value) {
            return new BoundedMatcher<Object, Item>(Item.class) {
                @Override
                public void describeTo(Description description) {
                    description.appendText("has value " + value);
                }
    
                @Override
                public boolean matchesSafely(Item item) {
                    return item.getName().toUpperCase().equals(String.valueOf(value));
                }
            };
        }
    

    Then call following:

    onData(withItemValue("DRINK1")).inAdapterView(withId(R.id.menu_item_grid)).perform(click());
    
    0 讨论(0)
  • 2020-12-13 13:42

    The problem is, that you try to match the list view itself with the instanceOf(ListView.class) as argument for onData(). onData() requires a data matcher that matches the adapted data of the ListView, not the ListView itself, and also not the View that Adapter.getView() returns, but the actual data.

    If you have something like this in your production code:

    ListView listView = (ListView)findViewById(R.id.myListView);
    ArrayAdapter<MyDataClass> adapter = getAdapterFromSomewhere();
    listView.setAdapter(adapter);
    

    Then the Matcher argument of Espresso.onData() should match the desired instance of MyDataClass. So, something like this should work:

    onData(hasToString(startsWith("ASDF"))).perform(click());
    

    (You can use another Matcher using a method of org.hamcrest.Matchers)

    In case you have multiple adapter views in your activity, you can call ViewMatchers.inAdapterView() with a view matcher that specifies the AdapterView like this:

    onData(hasToString(startsWith("ASDF")))
        .inAdapterView(withId(R.id.myListView))
        .perform(click());
    
    0 讨论(0)
  • 2020-12-13 13:46
    onData(hasEntry(equalTo(ListViewActivity.ROW_TEXT),is("List item: 25")))
            .onChildView(withId(R.id.rowTextView)).perform(click());
    

    This works best for me with row text data..

    0 讨论(0)
提交回复
热议问题