Android shell get and simulate click event

1. Get the coordinate position of the tap screen

by

adb shell getevent

Command to get the location coordinates of the click screen:


Step 1: Calculate the ratio

First pass the command

adb shell getevent -p | grep -e "0035" -e "0036"

Obtain the width (0035) and height (0036) in the event system

Taking the mobile phone I currently use as an example, the command outputs the following information:

0035  : value 0, min 0, max 1602, fuzz 0, flat 0, resolution 0
0036  : value 0, min 0, max 2503, fuzz 0, flat 0, resolution 0


Note: If it is a windows environment, there is no "|" pipe and grep command, you can use it directly

adb shell getevent -p

Then filter the 0035 and 0036 in the printed information to find the corresponding two lines of information


All we need is the max

0035 (width) max 1602

0036 (high) max 2503


The resolution of the mobile phone screen is known, and the current mobile phone I use as an example

The screen resolution of the mobile phone is 1080p, namely: 1080 (wide) * 1920 (high)


Calculate the ratio:

rateW = 1080 (the width of the phone screen) / 1602 (max of 0035 in the event) = 0.674

rateH = 1920 (the height of the phone screen) / 2503 (max of 0036 in the event) = 0.767



Step 2: Click the screen to calculate the coordinates of the click location

Excuting an order

adb shell getevent | grep -e "0035" -e "0036"

Taking the mobile phone I currently use as an example, the command outputs the following information:

/dev/input/event0: 0003 0035 00000341
/dev/input/event0: 0003 0036 000008ec


把0035和0036后面的位置数据从16进制转化为10进制

width = 0x341 = 3*16*16 + 4*16 + 1 = 833

height = 0x8ec = 8*16*16 + 14*16 + 12 = 2284

这是在event体系里的位置,将其转化为屏幕位置


screenW = width*rateW = 833*0.674 = 561

screenH = height*rateH = 2284*0.767 = 1751


终于算出来了

刚刚点击的屏幕位置坐标就是(561, 1751)


==============================================================================

当然还有其他很多方法获得点击屏幕位置坐标。

如果有点击页面的源码,嗯嗯,你可以打印log。TouchEvent里面的位置直接就是你在屏幕上的点击位置

或者

用自动化测试工具,直接输出点击位置坐标,

当然也是OK滴


adb shell getevent 只是其中之一方法,

它的使用就是没有源码,也木有自动化测试工具时。

一旦算出比例后,

每次计算坐标位置的计算量也不算大。可以忍啦^_^

2. 模拟滑动、点击屏幕事件

模拟事件全部是通过input命令来实现的,首先看一下input命令的使用: 

usage: input ...

       input text <string>
       input keyevent <key code number or name>
       input tap <x> <y>
       input swipe <x1> <y1> <x2> <y2>


1. keyevent指的是Android对应的keycode,比如home键的keycode=3,back键的keycode=4.

具体请查阅 <android keycode详解> http://blog.csdn.net/huiguixian/article/details/8550170

然后使用的话比较简单,比如想模拟home按键:

adb shell input keyevent 3

请查阅上述文章,根据具体keycode编辑即可。


2. 关于tap的话,他模拟的是touch屏幕的事件,只需给出x、y坐标即可。

此x、y坐标对应的是真实的屏幕分辨率,所以要根据具体手机具体看,比如你想点击屏幕(x, y) = (250, 250)位置:

adb shell input tap 250 250


3. 关于swipe同tap是一样的,只是他是模拟滑动的事件,给出起点和终点的坐标即可。例如从屏幕(250, 250), 到屏幕(300, 300)即

adb shell input swipe 250 250 300 300


发布了60 篇原创文章 · 获赞 44 · 访问量 34万+

Guess you like

Origin blog.csdn.net/beyond702/article/details/69258932