Espresso test, click X/Y coordinates

2019-07-09 11:56发布

Any idea how to do this for android? I can't seem to create a method that actually clicks.

I know you can do it with onview, but I just want an x/y position.

1条回答
成全新的幸福
2楼-- · 2019-07-09 12:12

The answer is already given here.

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);
}
查看更多
登录 后发表回答