Espresso 测试,点击 X/Y 坐标

Pul*_*kit 5 android click android-espresso

知道如何为android做到这一点吗?我似乎无法创建一个实际点击的方法。

我知道你可以用 onview 做到这一点,但我只想要一个 x/y 位置。

小智 7

答案已经在这里给出。

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);
}
Run Code Online (Sandbox Code Playgroud)