Cancel Alert Dialog in Android Espresso Test

I have been looking for a solution for this, but cannot find it. I am creating an Espresso test and should reject the Alert dialog box that appears in the middle of the screen the first time a specific Activity screen is displayed. There are no buttons in the dialog box, so to reject it, the user needs to click anywhere outside the window. Does anyone know how I can do this with Espresso. I tried to click on the layout on the main screen, but Espresso did not say that this view was not found in the hierarchy.

+5
source share
5 answers

Use onView(withText("alert_dialog_text")).perform(pressBack());, this should reject your dialogue.

+17
source

, , , pressBack.

onView(withText("OK")).inRoot(isDialog()).check(matches(isDisplayed())).perform(pressBack());

,

+2

.

uiautomator Espresso, gradle:

androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

:

UiDevice device = UiDevice.getInstance(getInstrumentation());
        device.click(x,y);

+1

, pressBack(). , . , , . !

0

, , UIAutomator. Espresso , UIAutomator .

  • build.gradle

    dependencies {
        androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0'
    }
    
  • , , .

    UiDevice mDevice = UiDevice.getInstance(getInstrumentation());
    UiObject uiObject = mDevice.findObject(new UiSelector().text("TEXT TO CHECK"));
    if (uiObject.exists())
    {
        mDevice.pressBack();
    }
    

    . Android 4.3 ( API 18) .

0

All Articles