Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate a touch outside of AlertDialog window on android with espresso

My Dependencies:

androidTestCompile 'com.android.support.test:runner:0.3' 
androidTestCompile 'com.android.support.test:rules:0.3' 
androidTestCompile 'com.android.support:support-annotations:23.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2' 
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') { 
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude module: 'recyclerview-v7' 
} 
androidTestCompile 'junit:junit:4.12'

I can´t find a way to simulate a click outside of an AlertDialog window to check something when it closes...

How can I do it?

like image 666
Daniel Gomez Rico Avatar asked Sep 16 '15 05:09

Daniel Gomez Rico


People also ask

What is Espresso testing in Android?

The Espresso Test Recorder tool lets you create UI tests for your app without writing any test code. By recording a test scenario, you can record your interactions with a device and add assertions to verify UI elements in particular snapshots of your app.

What is a dialog in Android Studio?

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. Dialog Design.

How do you dismiss an espresso dialog?

Use onView(withText("alert_dialog_text")). perform(pressBack()); this must dismiss your dialog.


2 Answers

Check my answer.

Espresso can't do this.

You need to use uiautomator inside your Espresso test, add this to your project's gradle:

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

After your dialog appears, you can click on the screen at any coordinate:

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

That will close your dialog

like image 192
Rubén López García Avatar answered Sep 25 '22 19:09

Rubén López García


You can do this in many cases by creating a custom ClickAction:

    public static ViewAction clickXY(final int x, final int y){
    return new GeneralClickAction(
            Tap.SINGLE,
            new CoordinatesProvider() {
                @Override
                public float[] calculateCoordinates(View view) {

                    final int[] screenPos = new int[2];
                    view.getLocationOnScreen(screenPos);

                    final float screenX = screenPos[0] + x;
                    final float screenY = screenPos[1] + y;
                    float[] coordinates = {screenX, screenY};

                    return coordinates;
                }
            },
            Press.FINGER);
}   

Then you can locate a known view in your dialog (say the 'OK' button) and invoke it as follows:

onView(withText("OK")).perform(clickXY(-200, 200));

Obviously, the x/y values that you use will be dependent upon your particular situation.

like image 37
jdonmoyer Avatar answered Sep 25 '22 19:09

jdonmoyer