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??
@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"
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With