Reputation: 85
Currently I have an activity that has an auto complete text view. The data in the text view is populated from an array adapter and I'm having issues getting my test case to click on one of the List View elements by name. Here's screenshots and the code:
// setup ingredient list widget
mFridgeList = (ListView) findViewById(R.id.fridge_list);
mFridgeList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, FridgesterDB.ingredientsList));
When a user types in text (ex: tom), a list of options will appear which the user can then select to confirm their selection.
I feel like I've tried a thousand different solutions but the most consistent error I'm getting is:
Error performing 'load adapter data' on view 'is assignable from class: class android.widget.AdapterView
I've tried using the solutions suggested here:
onData(hasToString(startsWith("tomato"))).perform(click());
and different variations of it to no avail.
This is the code that was generated from the Espresso Recorder and it causes a NoMatchingViewException:
ViewInteraction appCompatTextView = onView(
allOf(withId(android.R.id.text1), withText("tomato"), isDisplayed()));
appCompatTextView2.perform(click());
NoMatchingViewException: No views in hierarchy found matching: (with id: android:id/text1 and with text: is "tomato" and is displayed on the screen to the user)
I've also seen where a few people got this to work by using sleeps but that makes no difference in my case. Any ideas? Thanks
Upvotes: 0
Views: 1041
Reputation: 85
I was able to use the answer suggested here: Espresso AutoCompleteTextView click
onData(allOf(instanceOf(BaseIngredient.class), withContent(ingredient))).inRoot(RootMatchers.withDecorView(not(is(mActivityTestRule.getActivity().getWindow().getDecorView())))).perform(ViewActions.click());
I had to create a custom Matcher because each element in the adapter was a BaseIngredient object .
public static Matcher<Object> withContent(final String title) {
return new BoundedMatcher<Object, BaseIngredient>(BaseIngredient.class) {
@Override
public boolean matchesSafely(BaseIngredient myObj) {
return myObj.mName.equals(title);
}
@Override
public void describeTo(Description description) {
description.appendText("with title '" + title + "'");
}
};
}
Upvotes: 1