Как имитировать прикосновение за пределами окна AlertDialog на Android с помощью эспрессо

Мои зависимости:

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'

Я не могу найти способ имитировать щелчок за пределами окна AlertDialog, чтобы проверить что-то, когда оно закрывается...

Как мне это сделать?


person Daniel Gomez Rico    schedule 16.09.2015    source источник
comment
Как вы решили эту проблему?   -  person Rakesh    schedule 24.02.2016
comment
@Rakesh не решен :(   -  person Daniel Gomez Rico    schedule 24.02.2016


Ответы (2)


Проверьте мой ответ.

Эспрессо не может этого сделать.

Вам нужно использовать uiautomator внутри вашего теста Espresso, добавьте это в свой проект:

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

После того, как появится диалоговое окно, вы можете щелкнуть на экране по любой координате:

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

Это закроет ваш диалог

person Rubén López García    schedule 25.04.2018

Во многих случаях вы можете сделать это, создав собственное действие 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);
}   

Затем вы можете найти известное представление в своем диалоговом окне (скажем, кнопку «ОК») и вызвать его следующим образом:

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

Очевидно, что значения x/y, которые вы используете, будут зависеть от вашей конкретной ситуации.

person jdonmoyer    schedule 02.05.2016