Search code examples
inputtouchadb

Is it possible to create an Automated Touch/hold script using ADB shell?


I'm trying to set up an automated touch sequence using ADB for testing purposes, I've been searching for a few weeks now about information on how to create pauses, long touches, etc with no luck. I know about using the following for taps and swipes:

input [touchscreen|touchpad] tap <x> <y>
input [touchscreen|touchpad] swipe <x1> <y1> <x2> <y2>

But I'm not sure if they can be modified to create the things I mentioned earlier (pauses, long touches, holds).

What would a general script look like to create a sequence such as:

tap, tap, tap, tap, pause, long touch, pause, long touch, pause, tap, tap, tap, tap, pause, repeat

For example purposes assume all the commands are happening at the same <x> <y> locations.

Any help is much appreciated.


Solution

  • You can do this using monkeyrunner, a tool that comes with the Android SDK.

    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyView
    device = MonkeyRunner.waitForConnection(timeout = 60, deviceId = "DEVICE_ID")
    # DEVICE_ID is the device's serial number obtained by running adb devices
    
    # to tap 4 times
    for i in range(4):
        device.touch(x, y,MonkeyDevice.DOWN_AND_UP)
    
    # to pause
    MonkeyRunner.sleep(no_of_seconds)
    
    # to long touch
    device.touch(x, y,MonkeyDevice.DOWN)
    MonkeyRunner.sleep(no_of_seconds)
    # to release the hold
    device.touch(x, y,MonkeyDevice.UP)
    

    Now, to include all these in a repeat cicle, you can use python while or for statements.

    monkeyrunner is at Android-sdk/tools/monkeyrunner

    Next run it ./monkeyrunner you will enter interactive console of Jython 2.5.3

    Or run saved script by monkeyrunner ../Desktop/level.py