Espresso test, click X/Y coordinates

2019-07-09 12:29发布

问题:

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:

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);
}