这种方法特别适用于自动化测试和远程控制场景,可以准确获取屏幕文本信息并实现文本输入提交。

完整操作流程
1、连接设备并执行 adb shell uiautomator dump /sdcard/screen.xml 获取UI结构
2、拉取XML文件并解析,获取所有UI元素的class、id、text和bounds信息
3、使用 adb shell input text 在目标输入框输入文本
4、通过坐标点击或按键事件完成提交操作
 

例如:远程真机时,需要手动连接wifi,在输入wifi密码时,远程界面会黑屏,导致无法操作(如下图)。以下操作以此为例子

1、将当前屏幕的UI层次结构导出为XML文件

adb shell uiautomator dump

2、获取UI结构

adb shell uiautomator dump /sdcard/screen.xml

3、 将文件拉取到本地进行分析

adb pull /sdcard/screen.xml 

可以在本地查看xml文件:

 某个输入框的控件信息

XML文件中的 <node> 节点包含以下关键属性:

text:控件显示的文本内容

class:控件类型(如android.widget.TextView)

resource-id:控件的资源ID

bounds:控件在屏幕上的坐标范围

4、向当前获得焦点的输入框输入文本

注:仅支持输入无空格文本,若存在空格,则只会输入空格截断前内容

①、单个输入框:直接输入文本

adb shell input text 命令可以向当前获得焦点的输入框输入文本(例如执行 adb shell input text "Hello World" 即可在输入框中输入相应内容)

adb shell input "text"

②、多个输入框:先定位后(adb shell input tap),输入文本(adb shell input text)

查看xml文件,找到对应的输入框,得到bounds坐标值.

以上就完成了一个输入完成一个文本,剩下的文本框内容则重复上诉操作,修改坐标值和文本内容即可。


5、提交
方法一:使用 adb shell input keyevent KEYCODE_ENTER 模拟回车键提交

 adb shell input keyevent KEYCODE_ENTER


方法二:通过解析的bounds坐标,使用 adb shell input tap x y 点击提交按钮

adb shell input tap x y

例如下方坐标,那么x取值范围 102≤x≤258 ,y的取值范围 492≤x≤630,主要是为了将光标定位至目标按钮区域。eg:adb shell input tap 150 600


 

更多推荐