Espresso tested view is below dialog and therefore

2019-09-11 09:54发布

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.

2条回答
小情绪 Triste *
2楼-- · 2019-09-11 10:03

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.

查看更多
3楼-- · 2019-09-11 10:17

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:

Hope it help

查看更多
登录 后发表回答