yarikxny
yarikxny

Reputation: 3

How to find button in dialog espresso

I have this popup. How can I press on QUIT button ?

I use string like this:

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

enter image description here

Upvotes: 0

Views: 1543

Answers (2)

hyung jun yoo
hyung jun yoo

Reputation: 613

If you use the UI-Automator with AndroidX, you can find the dialog and buttons.

It is a gradle dependency code.

dependencies {
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}

You can reach the QUIT button with this code.

It is Kotlin code.

val button = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
                .findObject(
                    UiSelector().text(
                        activityTestRule
                            .activity
                            .getString(R.string.quit_button)
                            .toUpperCase()
                    )
                )

if (button.exists() && button.isEnabled) {
    button.click()
}

Upvotes: 0

jeprubio
jeprubio

Reputation: 18032

Try:

onView(withText("QUIT"))
    .inRoot(isDialog())
    .check(matches(isDisplayed()))
    .perform(click());

Upvotes: 6

Related Questions