检查对话框是否可见 - Espresso

Check the dialog is visible - Espresso

我在 Whosebug 上发现了类似的关于检查对话框何时可见的内容:

 onView(withText("Yes"))
                    .inRoot(isDialog())
                    .check(matches(isDisplayed()))
                    .perform(click());

当然,如果带按钮 'yes' 的对话框可见,这当然有效,但在不同的情况下,如果对话框不可见,我会崩溃:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "Yes"

那么怎么写对话框存在就点yes,不存在就不点什么?

你可以试试这个:

  onView(withText("Yes")).inRoot(isDialog()).withFailureHandler(new FailureHandler() {
            @Override
            public void handle(Throwable error, Matcher<View> viewMatcher){

            }
        }).check(matches(isDisplayed())).perform(customClick());

//if dialog is visible, perform click, otherwise do nothing.