• 14

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Espresso - Click by text in List view

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 tried

Espresso.onData(Matchers.allOf(Matchers.is(Matchers.instanceOf(ListView.class)), Matchers.hasToString(Matchers.startsWith("ASDF")))).perform(ViewActions.click());

As expected, this didn't work. The error said no view in hierarchy. Does anyone know how to select a String? ("ASDF" in this case) Thanks in advance.

Update due to @haffax

I received error:

com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException: 'is assignable from class: class android.widget.AdapterView' matches multiple views in the hierarchy.

Second error

With this code


I get this error

com.google.android.apps.common.testing.ui.espresso.PerformException: Error performing 'load adapter data' on view 'with content description: is "MapList"'.

Caused by: java.lang.RuntimeException: No data found matching: asString(a string starting with "ASDF")



      • 2
    • If you don't have access to the backing data type, then you need to go over the position of that item in the ListView, using onData(anything()).inAdapterView(...).atPosition(x).perform(click()) to click on the x'th item in the list.
      • 2
    • The interesting bit of this exception is further below in the stack trace. There should be a "Caused by:" part in the whole stack trace. That describes what really went wrong. Please share the complete stack trace for analysis.

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();

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


(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:

  • 43
Reply Report
      • 1
    • Well, you'll have to sort the matching out yourself. Seems weird to have so many different list views with the same id. Can't you give your list views different IDs? Or is this because the are used in ListFragments? Other ways to further distinguish: Assign a tag to the view and use the hasTag() matcher or use isDescendantOfA() to distinguish via hierarchy.
    • Yeah I think it is odd too. i didn't write the app, I just test it. But yes, you're right, I can't give them different id's cause they are in ListFragments. I will look in to adding a tag.

If adapter have custom model class for example Item:

public static Matcher<Object> withItemValue(final String value) {
        return new BoundedMatcher<Object, Item>(Item.class) {
            public void describeTo(Description description) {
                description.appendText("has value " + value);

            public boolean matchesSafely(Item item) {
                return item.getName().toUpperCase().equals(String.valueOf(value));

Then call following:

  • 2
Reply Report
      • 1
    • this works for me on one screen, but on another screen, Espresso seems to scroll to that item, but the item is not clicked even if perform(click()) is called (tested this with breakpoints)....this is very weird
onData(hasEntry(equalTo(ListViewActivity.ROW_TEXT),is("List item: 25")))

This works best for me with row text data..

  • 0
Reply Report