霍格沃兹测试学院 打造 Shell 版 Android 自动化测试工具实战

思寒_seveniruby for 霍格沃兹测试学院外部交流圈 · July 12, 2019 · Last by 思寒_seveniruby replied at July 12, 2019 · 603 hits

Android SDK

export ANDROID_HOME=/Users/seveniruby/Library/Android/sdk/
export PATH=$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin

Adb

  • 客户端
  • 服务端
  • android后台服务

服务器server的参数

adb -L tcp:5037 fork-server server --reply-fd 4

adb client

adb devices
adb kill-server

daemon

localhost:~ seveniruby$ adb shell ps -ef | grep adbd
shell 13883 1 2 20:27:48 ? 00:01:10 adbd --root_seclabel=u:r:su:s0

adb client -> adb server -> adbd

性能测试

adb shell dumpsys procstats --hours 3
adb shell dumpsys meminfo package_name|pid [-d]
adb shell dumpsys batterystats --charged package-name
adb shell dumpsys netstats detail
adb shell dumpsys gfxinfo package-name

自动化测试

常用命令

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

HWKNT:/ $ input text abc
HWKNT:/ $ input text 123
HWKNT:/ $ input tap 1000 600
HWKNT:/ $ input text 123
HWKNT:/ $ input tap 1000 600
HWKNT:/ $ input swipe 100 1500 800 500
HWKNT:/ $ input swipe 100 1500 800 500
HWKNT:/ $ input swipe 100 1500 800 500
HWKNT:/ $ input swipe 100 1500 800 500
localhost:~ seveniruby$ adb shell "uiautomator dump --compressed && cat /sdcard/window_dump.xml"
UI hierchary dumped to: /sdcard/window_dump.xml
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><hierarchy rotation="0"><node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.android.incallui" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1812]"><node index="0" text="" resource-id="com.android.settings:id/drawer_layout" class="android.support.v4.widget.DrawerLayout" package="com.android.incallui" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1812]"><node index="0" text="" resource-id="com.android.settings:id/content_parent" class="android.widget.LinearLayout" package="com.android.incallui" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1812]"><node index="0" text=""
。。。
package="
com.android.incallui" content-desc="状态消息:" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,72][1080,216]"><node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.android.incallui" content-desc="向上导航" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,72][156,216]" /><node index="1" text="状态消息" resource-id="android:id/action_bar_title" class="android.widget.TextView" package="com.android.incallui" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[168,107][384,180]" /></node></node></node></hierarchy>

封装

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

完整的自动化框架

  • 自动化能力
  • 用例组织管理能力
  • 断言

维护公司的回归测试集合,推荐使用的是标准的编程框架。比如appium

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 1 条回复 时间 点赞
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up