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
.
AFAIK there is no way to do that. From RootViewPicker.java:
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.
Well, I see also another solution than chiuki.
There's
@Before
annotation which you may add to method likeThis 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 calleduiautomater
. 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