检查对话框是否可见 - Espresso [英] Check the dialog is visible - Espresso

查看:47
本文介绍了检查对话框是否可见 - Espresso的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I found on Stackoverflow something like that about checking when dialog is visible:

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

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

Of course this works if Dialog with button 'yes' is visible, but in different scenario, if dialog will be invisible I got crash:

android.support.test.espresso.NoMatchingViewException:没有视图找到匹配的层次结构:文本:是是"

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

那么怎么写,如果对话框存在,点击yes,如果不存在,那么什么都不点击?

So how to write that if the dialog exists, click yes, and if it does not exist, then nothing will be clicked?

推荐答案

你可以试试这个:

  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.

这篇关于检查对话框是否可见 - Espresso的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆