编写shell脚本完成ui自动化测试

常用命令

adb shell "uiautomator dump --compressed && cat /sdcard/window_dump.xml

input text abc
input text 123
input tap 1000 600
input text 123
input tap 1000 600
input swipe 100 1500 800 500                        
input swipe 100 1500 800 500
input swipe 100 1500 800 500
input swipe 100 1500 800 500

注释
#以后的语句,shell不会解析
多行注释可以考虑采用:<<
传参

$0:表示执行的程序,是相对于执行目录的路径
$1、$2、$3:便是第几个参数,默认shell只支持9个参数,使用shift可以传递更多的参数
@ @、 :表示所有的参数,不含$0
KaTeX parse error: Expected '}', got '#' at position 2: {#̲*}和{#@}表示位置参数的个数
通过 : 1 : 3 , {*:1:3}, {
.$#}来表示多个参数

函数

[function]name(){
...
}

Function 可以省略。除了可以在脚本文件中使用函数外,还可以在shell中定义。这些定义会在本次shell结束后消失。
如果没有return,返回值是最后一句指令的返回值。

安装Android SDK,找到adb服务

启动一个模拟器
adb devices
adb shell
top | 筛选条件
top [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [ -t ] [ -h ]
    -m num  Maximum number of processes to display.
    -n num  Updates to show before exiting.
    -d num  Seconds to wait between updates.
    -s col  Column to sort by (cpu,vss,rss,thr).
    -t      Show threads instead of processes.
    -h      Display this help screen.

执行
调试

实际案例
封装函数

click(){
adb shell input tap $( adb shell "uiautomator dump --compressed && cat /sdcard/window_dump.xml"  | sed 's#<node #^<node #g' | tr ^ '\n' | grep "$@"  | awk -F '\\[|\\]|,' '{print ($(NF-2)+$(NF-5))/2, ($(NF-1)+$(NF-4))/2 }')
}
send_keys(){
adb shell input text "$@"
}
swipe(){
size=$(adb shell wm size)
start=$(echo "$size" | awk -v x=$1 -v y=$2 -F ' |x' '{print $(NF-1)*x, $NF*y}')
end=$(echo "$size" | awk -v x=$3 -v y=$4 -F ' |x' '{print $(NF-1)*x, $NF*y}')
adb shell input swipe $start $end
}

执行脚本

#摩拜的登录自动化
localhost:~ seveniruby$ click 摩拜单车
localhost:~ seveniruby$ click 请输入手机号
localhost:~ seveniruby$ send
send_keys  sendmail
localhost:~ seveniruby$ send_keys 11111111
localhost:~ seveniruby$ click 请输入验证码
localhost:~ seveniruby$ send_keys 1234
localhost:~ seveniruby$ click '"确定"

#滑动摩拜的地图
localhost:~ seveniruby$ swipe 0.1 0.8 0.9 0.3
localhost:~ seveniruby$ swipe 0.1 0.8 0.9 0.3
localhost:~ seveniruby$ swipe 0.9 0.8 0.1 0.3
localhost:~ seveniruby$ swipe 0.9 0.8 0.1 0.3
发布了16 篇原创文章 · 获赞 0 · 访问量 580

猜你喜欢

转载自blog.csdn.net/LittleGirl_orBoy/article/details/105022527