本节课适合中高级测试人才
npm install -g cnpm
cnpm install -g appium
新手入门工具: https://github.com/appium/appium-desktop/releases
启动雪球 app 的设置
{
"platformName": "android",
"deviceName": "hogwarts",
"appPackage": "com.xueqiu.android",
"appActivity": ".view.WelcomeActivityAlias",
"autoGrantPermissions": "true"
}
log=/tmp/adb.log
echo "# $$ "$(date "+%Y/%m/%d %H:%M:%S") >> $log
echo "# ppid: $(ps -o command $(ps -o ppid $$ | tail -1) | tail -1)" >> $log
echo "adb $@" >> $log
if echo "$@" | grep -E "logcat |exec-out |uiautomator runtest" &>/dev/null; then
echo "exec" >> $log
exec /Users/seveniruby/Library/Android/sdk//platform-tools/adb.bak "$@"
else
result=$(/Users/seveniruby/Library/Android/sdk//platform-tools/adb.bak "$@")
echo "origin" >> $log
echo "$result" | tee -a $log
fi
echo "" >> $log
# 89030 2019/07/05 20:51:23
# ppid: node /usr/local/bin/appium -p 5723 -g /tmp/appium.log
adb -P 5037 devices
origin
List of devices attached
emulator-5554 device
# 89043 2019/07/05 20:51:23
# ppid: node /usr/local/bin/appium -p 5723 -g /tmp/appium.log
adb -P 5037 -s emulator-5554 shell getprop ro.build.version.sdk
origin
26
# 89056 2019/07/05 20:51:23
# ppid: node /usr/local/bin/appium -p 5723 -g /tmp/appium.log
adb -P 5037 -s emulator-5554 shell pm list packages com.xueqiu.android
origin
package:com.xueqiu.android
curl -H "Content-Type: application/json; charset=utf-8" \
-H "Cache-Control: no-cache" \
--data '{"desiredCapabilities":{"appActivity":".view.WelcomeActivityAlias","appPackage":"com.xueqiu.android","autoGrantPermissions":"true","deviceName":"hogwarts","platformName":"android","newCommandTimeout":0,"connectHardwareKeyboard":true}}' \
-XPOST http://127.0.0.1:5723/wd/hub/session
抓包
sudo tcpdump -i any port 5723 or port 4724 -vv -w /tmp/tcpdump.1
appium 的交互
POST /wd/hub/session HTTP/1.1
Connection: keep-alive
User-Agent: admc/wd/1.11.1
Accept: application/json
Content-Type: application/json; charset=UTF-8
Content-Length: 234
host: 127.0.0.1:5723
{"desiredCapabilities":{"appActivity":".view.WelcomeActivityAlias","appPackage":"com.xueqiu.android","autoGrantPermissions":"true","deviceName":"hogwarts","platformName":"android","newCommandTimeout":0,"connectHardwareKeyboard":true}}HTTP/1.1 200 OK
X-Powered-By: Express
Vary: X-HTTP-Method-Override
Content-Type: application/json; charset=utf-8
Content-Length: 856
ETag: W/"358-h2tUi1B25lJ0BAIZ1c7Eh+Nw5yY"
Date: Fri, 05 Jul 2019 13:19:54 GMT
Connection: keep-alive
{"status":0,"value":{"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"appActivity":".view.WelcomeActivityAlias","appPackage":"com.xueqiu.android","autoGrantPermissions":true,"deviceName":"hogwarts","platformName":"android","newCommandTimeout":0,"connectHardwareKeyboard":true},"appActivity":".view.WelcomeActivityAlias","appPackage":"com.xueqiu.android","autoGrantPermissions":true,"deviceName":"emulator-5554","platformName":"android","newCommandTimeout":0,"connectHardwareKeyboard":true,"deviceUDID":"emulator-5554","platformVersion":"8.0.0","deviceScreenSize":"1080x1920","deviceModel":"Android SDK built for x86","deviceManufacturer":"Google"},"sessionId":"2f9abce4-a49f-49f2-9c58-440a2dd300f6"}
改造 appium
git clone https://github.com/appium/appium.git
cd appium
cnpm install
npm run build
node .
改造 uiautomator
appium依赖appium-uiautomator2-driver
appium-uiautomator2-driver依赖appium-uiautomator2-server
升级uiautomator2-server后,把编译的apk copy到 appium/node_modules/appium-uiautomator2-driver/node_modules/appium-uiautomator2-server/apks/
修改 uiautomator2-server
git clone https://github.com/appium/appium-uiautomator2-server.git
cd appium-uiautomator2-server
gradle clean assembleServerDebug assembleServerDebugAndroidTest
adb shell am instrument -w io.appium.uiautomator2.server.test/android.support.test.runner.AndroidJUnitRunner
log=/tmp/adb.log
echo "# $$ "$(date "+%Y/%m/%d %H:%M:%S") >> $log
echo "# ppid: $(ps -o command $(ps -o ppid $$ | tail -1) | tail -1)" >> $log
echo "adb $@" >> $log
if echo "$@" | grep -E "logcat |exec-out |uiautomator runtest" &>/dev/null; then
echo "exec" >> $log
exec /Users/seveniruby/Library/Android/sdk//platform-tools/adb.bak "$@"
elif echo "$@" | grep "dumpsys package io.appium.settings" &>/dev/null; then
echo "mock" >> $log
cat /Users/seveniruby/temp/appium/package.mock | tee -a $log
elif echo "$@" | grep "io\.appium\.settings" &>/dev/null;then
echo "mock" >> $log
echo "11111" | tee -a $log
else
result=$(/Users/seveniruby/Library/Android/sdk//platform-tools/adb.bak "$@")
echo "origin" >> $log
echo "$result" | tee -a $log
fi
echo "" >> $log