Check if a dialog with espresso is displayed

I am trying to write some tests using the new android test suite (Espresso) . But I can not find information on how to check if a dialog box is displayed and perform some actions on it (for example, clicking the positive and negative buttons, etc). Note that the dialog may also be displayed using the WebView , and not using the application.

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

  • Check if a dialog box is displayed
  • Click on dialog buttons
  • Interaction with the internal representation of the dialog (if this is a custom view)
  • A preliminary delay goes beyond the dialog box and checks whether it is displayed or not (for example, if setCancelable(false) was called in the dialog builder, and we want to check this)

Thank you for the consultation!

+46
android android-espresso
Jan 10 '14 at 13:25
source share
6 answers
  • To check if a dialog box is displayed, you can simply check if the view with the text that is present in the dialog box is displayed:

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

    or, based on text with id

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

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

    UPDATE

  • I think this is possible, since Espresso has multi-window support .
  • Not sure if you need to click outside of the user dialog, but to check if it is displayed or not, you need to create your own custom connector and check it inside.
+77
Jan 10 '14 at 14:28
source share

I am currently using this and it seems to be working fine.

 onView(withText(R.string.my_title)) .inRoot(isDialog()) // <--- .check(matches(isDisplayed())); 
+43
Apr 23 '15 at 9:56
source share

If you have an AlertDialog:

enter image description here

You can check if the components are displayed:

 int titleId = mActivityTestRule.getActivity().getResources() .getIdentifier( "alertTitle", "id", "android" ); onView(withId(titleId)) .inRoot(isDialog()) .check(matches(withText(R.string.my_title))) .check(matches(isDisplayed())); onView(withId(android.R.id.text1)) .inRoot(isDialog()) .check(matches(withText(R.string.my_message))) .check(matches(isDisplayed())); onView(withId(android.R.id.button2)) .inRoot(isDialog()) .check(matches(withText(android.R.string.no))) .check(matches(isDisplayed())); onView(withId(android.R.id.button3)) .inRoot(isDialog()) .check(matches(withText(android.R.string.yes))) .check(matches(isDisplayed())); 

and perform the action:

 onView(withId(android.R.id.button3)).perform(click()); 
+12
Jul 04 '16 at 18:08
source share

To answer question 4, to which the accepted answer did not answer, I changed the following code, which I found here in the "Stack Overflow" section ( link ) to verify that.

 @NonNull public static ViewInteraction getRootView(@NonNull Activity activity, @IdRes int id) { return onView(withId(id)).inRoot(withDecorView(not(is(activity.getWindow().getDecorView())))); } 

The passed id is the View identifier displayed in your dialog box. You can also write a method as follows:

 @NonNull public static ViewInteraction getRootView(@NonNull Activity activity, @NonNull String text) { return onView(withText(text)).inRoot(withDecorView(not(is(activity.getWindow().getDecorView())))); } 

And now it is looking for a View containing a specific text string.

Use it like this:

 getRootView(getActivity(), R.id.text_id).perform(click()); 
+2
Dec 08 '15 at 23:07
source share

Just in case, someone will stumble upon this question, like me. All answers will work only for dialogs with dialog buttons. Do not try to use this for progress dialogs without user interaction. Espresso continues to wait until the application enters an idle state. While the execution dialog is displayed, the application is not idle.

+2
Sep 26 '17 at 11:33
source share

The identifiers of the R.id.button1 and R.id.button2 buttons will not be the same for all devices. Identifiers may vary with OS versions.

The correct way to achieve this is to use UIAutomator. Include the UIAutomator dependency in the build.gradle file

 // Set this dependency to build and run UI Automator tests androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' 

and use

 // Initialize UiDevice instance UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); // Search for correct button in the dialog. UiObject button = uiDevice.findObject(new UiSelector().text("ButtonText")); if (button.exists() && button.isEnabled()) { button.click(); } 
+1
Dec 25 '15 at 18:04
source share



All Articles