Espresso tested view is below dialog and therefore Espresso can't find it

て烟熏妆下的殇ゞ 提交于 2019-12-13 07:27:20

问题


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.

The dialog that will be shown is a normal AlertDialog.

My test runs fine if I don't show the dialog, the moment I'll show it the above espresso statement fails.

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.


回答1:


AFAIK there is no way to do that. From RootViewPicker.java:

// when an android.app.dialog is shown
// - again, this is getting all the users attention, so it gets the test attention
// too.

I'd recommend mocking your battery dialog logic so you can control if it is shown or not during test. See http://blog.sqisland.com/2015/04/dagger-2-espresso-2-mockito.html and https://gum.co/AndroidTestSharedPref for more info.




回答2:


Well, I see also another solution than chiuki.

There's @Before annotation which you may add to method like

@Before
public void checkIfAlertDialogIsShown() {
   //your code
}

This method would run before every Espresso test. So you can check if this battery-low situation happens.

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.

So in @Before annotated method you would check using uiatomater framework if it happens. If true, than you would close this Dialog.

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

Hope it help



来源:https://stackoverflow.com/questions/34653576/espresso-tested-view-is-below-dialog-and-therefore-espresso-cant-find-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!