Click by bounds / coordinates

前端 未结 3 652
清酒与你
清酒与你 2020-12-06 04:11

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

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 04:49

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

提交回复
热议问题