How to use espresso to press a AlertDialog button

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

public class ApplicationTest {

public ActivityTestRule<LoadingActivity> mActivityRule =
        new ActivityTestRule<>(LoadingActivity.class);

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)
            .setPositiveButton(context.getString(R.string.go_to_settings), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
} No views in hierarchy found matching: with text: is "GA NAAR INSTELLINGEN"


  • 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());


    onView(withText("GA NAAR INSTELLINGEN"))

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


    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()) {;