Serj Lotutovici Serj Lotutovici - 7 months ago 718
Android Question

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

, 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
    was called on the dialog builder and we want to check that)

Thank you in advice!

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


    or, based on text with id

    onView(withId(, isDisplayed()));
  2. To click on dialogs button do this (button1 - OK, button2 - Cancel):

  3. I think it is possible now with latest Espresso release 1.1 - they announced 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.