树洞 Appium 框架原理与二次开发

思寒_seveniruby · 2019年07月05日 · 最后由 cmlanche 回复于 2019年07月05日 · 4113 次阅读

本节课适合中高级测试人才

Appium

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"
}

分析 Appium log

  • 查看 appium 的 log
  • adb fake
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

WebDriver

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

UIAutomator

Shell Mock

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
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 8 条回复 时间 点赞
2楼 已删除

capability 写错了吧

[AndroidDriver] Error: Unable to find an active device or emulator with OS 7.0. The following are available: ZY223P6BLD (6.0.1)
4楼 已删除

已二次改造完成,相关技术要点可以看我专栏:

cmlanche 回复

会有广告的嫌疑,尽量把文章都保留在 testerhome 吧

cmlanche 回复

你啥时候进的 testin 呀,咱们也算是前同事了,哈哈

那就看 testerhome 的专栏: https://testerhome.com/columns/custom-appium 哈哈,是一样的

我 16 年 11 月进的,比较晚,没想到和思寒大神是同事,很酷

仅楼主可见
仅楼主可见
11楼 已删除
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册