Simulating Touch Events on Android Devices via ADB

Simulating Touch Events on Android Devices via ADB

22 April 2024 Stephan Petzl Leave a comment Tech-Help

Automating user interface tests on Android can be challenging, especially when it comes to simulating touch events. Developers and testers often resort to the Android Debug Bridge (ADB) to achieve this task. In this guide, we will cover how to use ADB to send touch events to your Android device for UI testing purposes.

Using the Input Command

Android provides a handy input command-line tool to simulate various input events. For simulating touch events such as tapping, you can use the following command:

adb shell input tap x y

Here, x and y represent the coordinates on the screen where you want to simulate the touch event.

Finding Coordinates for Tap Events

To perform an action, such as opening a web browser, you’ll need to determine the exact coordinates to tap. This can be done by running:

adb shell getevent -l

When you press on the desired location on your device, you will see an output that includes the coordinates of the touch event in hexadecimal format. For example:

/dev/input/event3: EV_KEY       BTN_TOUCH            DOWN
/dev/input/event3: EV_ABS       ABS_MT_POSITION_X    000002f5
/dev/input/event3: EV_ABS       ABS_MT_POSITION_Y    0000069e

The hexadecimal values 2f5 and 69e correspond to the decimal values 757 and 1694 respectively. To simulate the same touch event, use:

adb shell input tap 757 1694

Advanced Coordinate Extraction

For a more refined approach to finding tap positions, you can filter and convert the getevent output to decimal integers:

adb shell getevent -l | grep ABS_MT_POSITION --line-buffered | awk '{a = substr($0,54,8); sub(/^0+/, "", a); b = sprintf("0x%s",a); printf("%d\n",strtonum(b))}'

This will output the x and y coordinates in real-time as you press on the device.

Alternative Methods

While the input tap command is straightforward, there are alternative methods to simulate touch events:

  • MonkeyRunner: Useful for older Android versions that do not support the input tap command. It uses a Python API to control an Android device or emulator.
  • UI Automator: Provides a more sophisticated way to interact with the user interface through the UI Automator API.

Integrating with Repeato

While the methods described above are useful, they can be cumbersome when dealing with complex UI tests or multiple devices. This is where Repeato can significantly streamline your testing process. Repeato is a no-code test automation tool that simplifies creating, running, and maintaining automated tests for iOS and Android apps.

With Repeato, you can avoid the manual work of scripting ADB commands and instead focus on what matters: ensuring your app’s quality. Repeato’s computer vision and AI capabilities allow you to easily test apps across different frameworks such as React Native, Flutter, Unity, and more. Plus, it comes with built-in ADB functionality, enabling you to execute ADB commands directly within your tests.

For more information on how Repeato can enhance your UI testing workflow, check out our comparison with Espresso and see why it might be the right tool for your testing needs.

Like this article? there’s more where that came from!