Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a dialog is displayed with Espresso

I'm trying to write some tests with the new android-test-kit (Espresso). But I can't find any information on how to check if a dialog is displayed and perform some actions on it (like clicking the positive and negative buttons, e.t.c.). Note that a dialog may be also displayed by a WebView, not by the application it self.

Any help would be appreciated. I just need a link, or some example code for the basics:

  1. Check if a dialog appears
  2. Perform clicks on dialog buttons
  3. Interact with the dialog's inner view (if it's a custom view)
  4. Preform clicks outside the dialog, and check if it's displaying or not (for example if setCancelable(false) was called on the dialog builder and we want to check that)

Thank you in advice!

like image 627
Serj Lotutovici Avatar asked Jan 10 '14 13:01

Serj Lotutovici


People also ask

How do you check espresso visibility?

One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.

How do you dismiss an espresso dialog?

Use onView(withText("alert_dialog_text")). perform(pressBack()); this must dismiss your dialog.

How do I know if my android has dialog?

Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog().

Can we show dialog with application context?

Afaik there is no way to display a Dialog w/o being the active foreground app.


1 Answers

  1. To verify if dialog appears you can simply check if View with a text that present inside the dialog is shown:

    onView(withText("dialogText")).check(matches(isDisplayed())); 

    or, based on text with id

    onView(withId(R.id.myDialogTextId)).check(matches(allOf(withText(myDialogText), isDisplayed())); 
  2. To click on dialogs button do this (button1 - OK, button2 - Cancel):

    onView(withId(android.R.id.button1)).perform(click()); 

    UPDATE

  3. I think is possible since Espresso has multi window support.
  4. Not sure about clicking outside the custom dialog view but for checking if it is displaying or not you have to create your custom matcher and check inside it.
like image 199
denys Avatar answered Sep 18 '22 17:09

denys