Reputation: 2549
I have a dialog with a list with elements where each one is supplied with an ArrayAdapter:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
ArrayList<String> names = getArguments().getStringArrayList(INTENT_OPTIONS);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, names);
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
if (mListener != null){
mListener.onSelectedElement(which);
}
}
};
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.loginActivity_selectVACenter)
.setCancelable(true)
.setAdapter(adapter, listener)
.create();
}
Before implementing the appcompat v7 library to incorporate material features, I selected each element in Espresso with the following sentence:
onView(withText("text of the entry"))
.perform(click());
But after adding this library, this sentence does not work anymore, Espresso cannot find the view and throws a NoMatchingViewException.
Is there any other way to access the dialog options?
Upvotes: 1
Views: 1592
Reputation: 19351
I've already found this topic which might be helpful: Check if a dialog is displayed with Espresso
In one of possible answers there is a getRootView()
matcher:
To answer question 4, which the accepted answer does not, I modified the following code, which I found here on Stack Overflow (link) for testing whether a Toast was displayed.
@NonNull public static ViewInteraction getRootView(@NonNull Activity activity, @IdRes int id) { return onView(withId(id)).inRoot(withDecorView(not(is(activity.getWindow().getDecorView())))); }
The
id
passed in is the id of aView
currently displayed in your dialog. You could also write the method like so:@NonNull public static ViewInteraction getRootView(@NonNull Activity activity, @NonNull String text) { return onView(withText(text)).inRoot(withDecorView(not(is(activity.getWindow().getDecorView())))); }
And now it's looking for a
View
containing a particular text string.Use it like so:
getRootView(getActivity(), R.id.text_id).perform(click());
Try this at first. Also in the same topic you would find this method:
onView(withText(R.string.my_title)).inRoot(isDialog()).check(matches(isDisplayed()));
If it still doesn't work it would might to use onData
matcher.
Whenever you have a ListView, GridView, Spinner, and other Adapter based views, you’ll have to use onData() in order to interact with an item from that list. onData() is targeting directly the data provided by your adapter. What does this mean, we will see in a moment.[1]
You can read about it here:
Get started with a simple test using onData
[codepath] UI Testing with Espresso
Android Espresso onData with doesNotExist
Upvotes: 1