31

I know it is possible for Espresso to click by bounds the way UiAutomator does. (x and y coordinates) I have read through the documentation but I can't seem to find it. Any help is appreciated. Thanks

Edit
I found this link, but no examples how to use it, My main concern with this is the UiController is or how to use it.

Community
  • 1
  • 1
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115

3 Answers3

61

Espresso has the GeneralClickAction, this is the underlying implementation of ViewActions click(), doubleClick(), and longClick().

The GeneralClickAction's constructor takes a CoordinatesProvider as second argument. So the basic idea is to create a static ViewAction getter which provides a custom CoordinatesProvider. Something like this:

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

A general advice with Espresso: instead of looking for documentation (there's virtually none), look at the source code. Espresso is open source and the source code itself is of really good quality.

haffax
  • 5,898
  • 1
  • 32
  • 30
  • Thanks for your answer! When I get a chance to test it out, I will accept it. – Chad Bingham Apr 02 '14 at 15:50
  • 1
    Sorry for the late request for help, but this is not clicking. I am not getting any errors, but it is also not clicking anywhere. Is there anything I don't understand about it? – Chad Bingham Apr 24 '14 at 15:43
  • One thing to remember is, that x, y are coordinates relative to view left/top, not screen left/top. But else I can't think of more right now. We use this kind of custom CoordinatesProviders a lot and so far didn't run into any issues. You can also try to log screenX/screenY to the logcat and see if the values make any sense. – haffax Apr 24 '14 at 18:36
  • So what view is it referring to? – Chad Bingham Apr 24 '14 at 18:39
  • 3
    The view you are performing it on. onView(..).perform(clickXY(10, 20)); The view that is matched by the matcher given to onView. – haffax Apr 24 '14 at 18:47
  • Ah. Gotcha. Thats what I missunderstood – Chad Bingham Apr 24 '14 at 19:18
  • 1
    @haffax I'd like to use screen absolute coordinates (say, 0,0 of screen). What should I put to `onView(...)` ? I tried `onView(isRoot()). and it failed with no match. Note : I am currently showing dialog in front – kiruwka Apr 23 '15 at 10:23
14

The valid answer helped me although that method is deprecated. Now you have to specify the inputDevice (for example InputDevice.SOURCE_MOUSE) and buttonState (for example MotionEvent.BUTTON_PRIMARY

Example in Kotlin:

companion object {
    fun clickIn(x: Int, y: Int): ViewAction {
        return GeneralClickAction(
                Tap.SINGLE,
                CoordinatesProvider { view ->
                    val screenPos = IntArray(2)
                    view?.getLocationOnScreen(screenPos)

                    val screenX = (screenPos[0] + x).toFloat()
                    val screenY = (screenPos[1] + y).toFloat()

                    floatArrayOf(screenX, screenY)
                },
                Press.FINGER,
                InputDevice.SOURCE_MOUSE,
                MotionEvent.BUTTON_PRIMARY)
    }
}
Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
11

@haffax's answer is great and works well.

However, if you want to click in a certain part of a View that may change from screen to screen, it might be useful to click based on percents (or ratios) as even the dp numbers may not be stable across all screens. So, I made an easy modification to it:

    public static ViewAction clickPercent(final float pctX, final float pctY){
    return new GeneralClickAction(
            Tap.SINGLE,
            new CoordinatesProvider() {
                @Override
                public float[] calculateCoordinates(View view) {

                    final int[] screenPos = new int[2];
                    view.getLocationOnScreen(screenPos);
                    int w = view.getWidth();
                    int h = view.getHeight();

                    float x = w * pctX;
                    float y = h * pctY;

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

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

I thought I would share it here so others can benefit.

lilbyrdie
  • 1,887
  • 2
  • 20
  • 24
  • 3
    Thanks. If anyone uses this code, value for pctX and pctY, ranges from 0 to 1. – sat Jun 30 '16 at 08:45