How to use espresso to press a AlertDialog button
Solution 1
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
Solution 2
Since inRoot(isDialog())
does not seem to work for DialogFragment
I use this workaround so far:
enum class AlertDialogButton(@IdRes val resId: Int) {
POSITIVE(android.R.id.button1),
NEGATIVE(android.R.id.button2),
NEUTRAL(android.R.id.button3)
}
fun clickOnButtonInAlertDialog(button: AlertDialogButton) {
onView(withId(button.resId)).perform(click())
}
Solution 3
You might need to close the softkeyboard like this:
onView(withId(R.id.username))
.perform(typeText("username"))
.perform(closeSoftKeyboard())
onView(withId(android.R.id.button1)).perform((click()))
As detailed more thoroughly by this answer.
Solution 4
For an AlertDialog
, the id assigned for each button is:
- POSITIVE:
android.R.id.button1
- NEGATIVE:
android.R.id.button2
- NEUTRAL:
android.R.id.button3
You can check yourself in the AlertController
class, setupButtons()
method.
So you can perform a click as follows:
onView(withId(android.R.id.button1)).perform(click());
Solution 5
The good thing about the last update is it allows you to set permissions before each test, so that you don't have to click the dialog (this speeds up testing by a lot!)
Use the following rule (e.g. for location/storage.. substitute these with the permissions you need obviously)
@Rule
public GrantPermissionRule runtimePermissionRule = GrantPermissionRule.grant(
ACCESS_FINE_LOCATION, READ_EXTERNAL_STORAGE);
Related videos on Youtube
Jim Clermonts
Multiple award-winning Android Developer. Has launched his own products and has Android and iOS apps live in the App stores. Has developed several apps (Android/iOS) as freelance programmer. Is highly specialized in Android SDK. Jim has worked for 8 months for a tech Startup in Silicon Valley as part of a traineeship. Jim also has experience in embedded software development in C/C++ and is a certified (ISTQB) software tester. Jim has won several prizes with attending and winning hackathons & entrepreneurship competitions. More info: www.JimClermonts.nl
Updated on September 27, 2022Comments
-
Jim Clermonts over 1 year
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"
-
jeprubio over 7 yearsI think you could use the withText() method instead of matching by id
-
Jim Clermonts over 7 yearsonView(withText("GA NAAR INSTELLINGEN")).perform(click()); doesn't work
-
-
Zeek Aran about 6 yearsThank you! isDialog() wasn't working for me, but I was able to find button1 and button2. Thank you for explicitly writing out how the different buttons line up with their ID numbers.
-
nasch over 4 yearsI don't know if it's a new thing or not, but
androidx.test.espresso.action.ViewActions
has a method `closeSoftKeyboard()' -
Safety about 4 yearsFor me this the best solution.
-
Taslim Oseni about 3 yearsThis worked for me without the
check()
method.