android - Espresso - Click by text in List view -
i trying click on text in list view using espresso. know have this guide, can't see how make work looking text. have tried
espresso.ondata(matchers.allof(matchers.is(matchers.instanceof(listview.class)), matchers.hastostring(matchers.startswith("asdf")))).perform(viewactions.click());
as expected, didn't work. error said no view in hierarchy. know how select string? ("asdf"
in case) in advance.
update due @haffax
i received error:
com.google.android.apps.common.testing.ui.espresso.ambiguousviewmatcherexception: 'is assignable class: class android.widget.adapterview' matches multiple views in hierarchy.
second error
with code
ondata(hastostring(startswith("asdf"))).inadapterview(withcontentdescription("maplist")).perform(click());
i error
com.google.android.apps.common.testing.ui.espresso.performexception: error performing 'load adapter data' on view 'with content description: "maplist"'.
caused by: java.lang.runtimeexception: no data found matching: asstring(a string starting "asdf")
solution
ondata(anything()).inadapterview(withcontentdescription("desc")).atposition(x).perform(click())
the problem is, try match list view instanceof(listview.class)
argument ondata()
. ondata()
requires data matcher matches adapted data of listview
, not listview
itself, , not view
adapter.getview()
returns, actual data.
if have in production code:
listview listview = (listview)findviewbyid(r.id.mylistview); arrayadapter<mydataclass> adapter = getadapterfromsomewhere(); listview.setadapter(adapter);
then matcher argument of espresso.ondata()
should match desired instance of mydataclass
. so, should work:
ondata(hastostring(startswith("asdf"))).perform(click());
(you can use matcher using method of org.hamcrest.matchers
)
in case have multiple adapter views in activity, can call viewmatchers.inadapterview()
view matcher specifies adapterview this:
ondata(hastostring(startswith("asdf"))) .inadapterview(withid(r.id.mylistview)) .perform(click());
Comments
Post a Comment