经过Espresso测试的视图位于对话框下方,因此Espresso无法找到它 [英] Espresso tested view is below dialog and therefore Espresso can't find it
问题描述
我想验证用户界面上是否有一些任意文本的视图.因此,我正在使用:
I want to verify that there's a view on the UI that mathces some arbitary text. Therefore I'm using:
onView(withId(R.id.my_view_id)).check(matches(withText("my text")));
问题在于,根据设备的电池电量,可能会出现一个对话框,提示电池电量低.
The problem is though that depending on the battery level of the device there might be a dialog saying that the battery level is low.
将显示的对话框是普通的AlertDialog
.
The dialog that will be shown is a normal AlertDialog
.
如果我不显示对话框,则上面的espresso语句显示的那一刻我的测试运行正常.
My test runs fine if I don't show the dialog, the moment I'll show it the above espresso statement fails.
如何告诉espresso我不在乎对话框,而只想查找视图?当Espresso尝试在AlertDialog
上找到视图R.id.my_view_id
时.
How can I tell espresso that I don't care about the dialog and just want to find the view? As Espresso is trying to find the view R.id.my_view_id
on the AlertDialog
.
推荐答案
好吧,我也看到了chiuki以外的另一种解决方案.
Well, I see also another solution than chiuki.
有@Before
个注释,您可以将其添加到类似方法中
There's @Before
annotation which you may add to method like
@Before
public void checkIfAlertDialogIsShown() {
//your code
}
此方法将在每次Espresso
测试之前运行.因此,您可以检查这种电量不足的情况是否发生.
This method would run before every Espresso
test. So you can check if this battery-low situation happens.
很遗憾,Espresso无法捕获AlertDialog
或系统通知.为此,您可以使用另一个名为uiautomater
的出色Google工具.它与Espresso搭配使用效果很好,因此您可以同时使用它们.
Unfortunately, Espresso can't catch AlertDialog
or system notifications. For this purpose you can use another great Google's tool called uiautomater
. It works good with Espresso, so you may use both of them.
因此在@Before带注释的方法中,您将使用uiatomater
框架检查是否发生了这种情况.如果为true,则您将关闭此对话框.
So in @Before annotated method you would check using uiatomater
framework if it happens. If true, than you would close this Dialog.
检查这些链接以了解有关uiatomator
的更多信息:
Check these links to learn more about uiatomator
:
- http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html
- https://developer.android.com/tools/testing-support-library/index.html#UIAutomator
- https://google.github.io/android-testing-support-library/docs/uiautomator/index.html
- http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html
- https://developer.android.com/tools/testing-support-library/index.html#UIAutomator
- https://google.github.io/android-testing-support-library/docs/uiautomator/index.html
希望有帮助
这篇关于经过Espresso测试的视图位于对话框下方,因此Espresso无法找到它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!