Check if a dialog is displayed with Espresso

后端 未结 6 598
孤街浪徒
孤街浪徒 2020-12-04 14:07

I\'m trying to write some tests with the new android-test-kit (Espresso). But I can\'t find any information on how to check if a dialog is displayed and per

6条回答
  •  感情败类
    2020-12-04 14:39

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

提交回复
热议问题