浓咖啡-如何检查是否显示其中一个视图 [英] Espresso - How to check if one of the view is displayed

查看:54
本文介绍了浓咖啡-如何检查是否显示其中一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的测试中,执行一项操作后,可能会出现两种可能的视图,并且两种视图都是正确的.如何检查是否显示了其中一个视图.对于单个视图,我可以使用Displayed()进行检查.但是,如果其他视图可见,那将失败.如果要显示这两个视图中的任何一个,我想通过测试.

In my test, after one action, there are two possible views which can appear and both of them are correct. How can I check if one of the view is displayed. For a single view I can check with is Displayed(). But that would fail if other view is visible instead. I want to pass the test if any one of those two views are displayed.

onMyButton.perform(click());

onMyPageOne.check(matches(isDisplayed())); //view 1
or
onMyPageTwo.check(matches(isDisplayed())); //view 2

之后,在MyButton上单击,预期将出现任何一个视图(1或2),但不会同时出现.不固定显示哪一个. 如何检查其中是否显示任何一个?

After, perform click on MyButton, any one of the view (1 or 2) is expected to appear but not both. It is not fixed that which one would be displayed. How can I check if any one of them is displayed?

推荐答案

可以像这样捕获Espresso引发的异常:

It's possible to catch the exceptions raised by Espresso like this:

如果要测试视图是否在层次结构中:

If you want to test if a view is in hierarchy:

try {
    onView(withText("Button")).perform(click());
    // View is in hierarchy

} catch (NoMatchingViewException e) {
    // View is not in hierarchy
}

如果视图在层次结构中不是,则会引发此异常.

This exception will be thrown if the view is not in the hierarchy.

有时视图可以在层次结构中,但是我们需要测试它是否显示,因此断言还有另一个例外,例如:

Sometimes the view can be in the hierarchy, but we need to test if it is displayed, so there is another exception for assertions, like this:

try {
    onView(withText("Button")).check(matches(isDisplayed()));
    // View is displayed
} catch (AssertionFailedError e) {
    // View not displayed
}

这篇关于浓咖啡-如何检查是否显示其中一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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