Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use espresso to press a AlertDialog button

Tags:

I want to press below button using Espresso, but I'm not sure how. Should I get the resource-id? Or how to set an ID to the AlertDialog??

enter image description here

@RunWith(AndroidJUnit4.class) public class ApplicationTest {  @Rule public ActivityTestRule<LoadingActivity> mActivityRule =         new ActivityTestRule<>(LoadingActivity.class);  @Test public void loginClickMarker() { //Doesn't work:     onView(withText("GA NAAR INSTELLINGEN")).perform(click()); } }  public class PopupDialog {  public static void showGPSIsDisabled(Context context, String msg, final PopupDialogCallback popupDialogCallback) {     new AlertDialog.Builder(context)             .setTitle(context.getString(R.string.location_turned_off))             .setMessage(msg)             .setPositiveButton(context.getString(R.string.go_to_settings), new DialogInterface.OnClickListener() {                 @Override                 public void onClick(DialogInterface dialog, int which) {                     dialog.dismiss();                     popupDialogCallback.hasClicked();                 }             }).show(); } } 

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "GA NAAR INSTELLINGEN"

like image 264
Jim Clermonts Avatar asked Sep 07 '16 18:09

Jim Clermonts


People also ask

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

How do you inflate dialog?

To inflate the layout in your DialogFragment , get a LayoutInflater with getLayoutInflater() and call inflate() , where the first parameter is the layout resource ID and the second parameter is a parent view for the layout. You can then call setView() to place the layout in the dialog.


1 Answers

According to StackOverflow similar issue: Check if a dialog is displayed with Espresso

You should change your code from:

onView(withText("GA NAAR INSTELLINGEN")).perform(click()); 

to

onView(withText("GA NAAR INSTELLINGEN")))     .inRoot(isDialog()) // <---     .check(matches(isDisplayed()))     .perform(click()); 

If it won't work, don't bother to use long with Espresso another great Google's instrumentation test called uiatomator.

Check: http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

Example code:

// Initialize UiDevice instance UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());  // Search for correct button in the dialog. UiObject button = uiDevice.findObject(new UiSelector().text("GA NAAR INSTELLINGEN")); if (button.exists() && button.isEnabled()) {     button.click(); } 

Hope it will help

like image 193
piotrek1543 Avatar answered Sep 22 '22 20:09

piotrek1543