如何使用ADB使用sendevent命令将触摸事件发送到设备?
我正在尝试使用 AndroidDebugBridge 将触摸事件发送到设备,以便我可以为 UI 测试进行一些基本的自动化。我已关注LINK。我可以使用 sendevent 模拟模拟器上的触摸,但无法在设备上执行相同的操作。
就像上面的链接一样,模拟器似乎为每次触摸发送 6 个事件(xcoord,ycoord,2 个按下,2 个释放),并且很容易使用此信息来发送事件,但是设备触摸屏的 getevent 似乎产生太多事件。
有人设法将触摸从 ADB 发送到设备吗?您能分享一下解决方案吗?
I am trying to send touch events to a device using AndroidDebugBridge, so that I can do some basic automation for UI tests. I have followed the discussion in LINK. I am able to use sendevent to simulate touch on emulators, but unable to do the same on a device.
Like in above link the emulator seems to send out 6 events for each touch (xcoord, ycoord, 2 for press, 2 for release) and it was easy to use this information to sendevents, but a getevent for the touchscreen for a device seems to generate far too many events.
Has somebody managed to send touch from ADB to a device? Could you please share the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Android 附带了一个
input
命令行工具,可以模拟各种输入事件。要模拟点击,它是:您可以使用 adb shell (> 2.3.5) 远程运行命令:
Android comes with an
input
command-line tool that can simulate miscellaneous input events. To simulate tapping, it's:You can use the adb shell (> 2.3.5) to run the command remotely:
为了执行特定操作(例如打开网络浏览器),您需要首先弄清楚要点击的位置。为此,您可以首先运行:
一旦您在设备上按下,在您想要的位置,您将看到以下输出:
adb 告诉您在位置 2f5, 69e(十六进制)处按下了一个键(按钮按下),其中十进制为 757 和 1694。
如果您现在想要生成相同的事件,您可以在同一位置使用输入点击命令:
更多信息可以在以下位置找到:
https://source.android.com/devices/input/touch-devices.html
http://source.android.com/devices/input/getevent.html
In order to do a particular action (for example to open the web browser), you need to first figure out where to tap. To do that, you can first run:
Once you press on the device, at the location that you want, you will see this output:
adb is telling you that a key was pressed (button down) at position 2f5, 69e in hex which is 757 and 1694 in decimal.
If you now want to generate the same event, you can use the input tap command at the same position:
More info can be found at:
https://source.android.com/devices/input/touch-devices.html
http://source.android.com/devices/input/getevent.html
2.3.5没有
input tap
,只有input keyevent
和input text
您可以使用 Monkeyrunner 来实现:(这是答案的副本 https://stackoverflow.com/a/18959385/1587329 ):
2.3.5 did not have
input tap
, justinput keyevent
andinput text
You can use the monkeyrunner for it: (this is a copy of the answer at https://stackoverflow.com/a/18959385/1587329):
建立在 Tomas 的答案之上,这是我发现的以整数形式查找位置点击位置的最佳方法:
使用
adb shell getevent -l
获取事件列表,使用 grep 获取ABS_MT_POSITION
(获取十六进制触摸事件行),最后使用 awk 获取相关的十六进制值,strip它们为零并将十六进制转换为整数。仅当您按下设备时,才会在终端中连续打印 x 和 y 坐标。然后您可以使用此 adb shell 命令发送命令:
Building on top of Tomas's answer, this is the best approach of finding the location tap position as an integer I found:
Use
adb shell getevent -l
to get a list of events, the using grep forABS_MT_POSITION
(gets the line with touch events in hex) and finally use awk to get the relevant hex values, strip them of zeros and convert hex to integer. This continuously prints the x and y coordinates in the terminal only when you press on the device.You can then use this adb shell command to send the command:
你不需要使用
命令,您只需在设备上的开发者选项中启用[显示触摸数据]即可获取 X 和 Y。
更多信息可以在我的文章中找到:https://mobileqablog.wordpress.com/2016/08/ 20/android-automatic-touchscreen-taps-adb-shell-input-touchscreen-tap/
You don't need to use
command, you just need to enable in Developer Options on the device [Show Touch data] to get X and Y.
Some more information can be found in my article here: https://mobileqablog.wordpress.com/2016/08/20/android-automatic-touchscreen-taps-adb-shell-input-touchscreen-tap/
考虑使用 Android 的 uiautomator 以及 adb shell uiautomator [...] 或直接使用SDK自带的.jar即可。
Consider using Android's uiautomator, with adb shell uiautomator [...] or directly using the .jar that comes with the SDK.