环境:Android 6.0.1+appium 1.4.16.1+ruby
在上面环境中执行微信公众号中的 H5 测试,发现 driver.available_contexts 有时候返回:webview_undefined,有时候返回:com.tencent.mm:tools
set_context()切换引擎时也就时而报错,appium 日志:

info: [debug] Getting a list of available webviews
info: [debug] executing cmd: D:\adt-bundle-windows-x86_64-20140702\sdk\platform-tools\adb.exe -s 02157df27164db19 shell "cat /proc/net/unix"
info: [debug] WEBVIEW_29715 mapped to pid 29715
info: [debug] Getting process name for webview
info: [debug] executing cmd: D:\adt-bundle-windows-x86_64-20140702\sdk\platform-tools\adb.exe -s 02157df27164db19 shell "ps"
info: [debug] Parsed pid: 29715 pkg: undefined
info: [debug] from: u0_a189,29715,3125,2129912,92912,0000000000,R,com.tencent.mm:tools
info: [debug] returning process name: undefined
info: [debug] Available contexts:
info: [debug] ["WEBVIEW_undefined"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_undefined
info: [debug] Responding to client with success: {"status":0,"value":["NATIVE_APP","WEBVIEW_undefined"],"sessionId":"e5ffe50d-0381-45b0-824e-65c5c406b392"}
info: <-- GET /wd/hub/session/e5ffe50d-0381-45b0-824e-65c5c406b392/contexts 200 245.268 ms - 106 {"status":0,"value":["NATIVE_APP","WEBVIEW_undefined"],"sessionId":"e5ffe50d-0381-45b0-824e-65c5c406b392"}
info: --> GET /wd/hub/session/e5ffe50d-0381-45b0-824e-65c5c406b392/context {}
info: [debug] Responding to client with success: {"status":0,"value":"NATIVE_APP","sessionId":"e5ffe50d-0381-45b0-824e-65c5c406b392"}
info: <-- GET /wd/hub/session/e5ffe50d-0381-45b0-824e-65c5c406b392/context 200 0.689 ms - 84 {"status":0,"value":"NATIVE_APP","sessionId":"e5ffe50d-0381-45b0-824e-65c5c406b392"}
info: --> POST /wd/hub/session/e5ffe50d-0381-45b0-824e-65c5c406b392/context {"name":"WEBVIEW_undefined"}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: D:\adt-bundle-windows-x86_64-20140702\sdk\platform-tools\adb.exe -s 02157df27164db19 shell "cat /proc/net/unix"
info: [debug] WEBVIEW_29715 mapped to pid 29715
info: [debug] Getting process name for webview
info: [debug] executing cmd: D:\adt-bundle-windows-x86_64-20140702\sdk\platform-tools\adb.exe -s 02157df27164db19 shell "ps"
info: [debug] Parsed pid: 29715 pkg: undefined
info: [debug] from: u0_a189,29715,3125,2131296,96856,0000000000,R,com.tencent.mm:tools
info: [debug] returning process name: undefined
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_undefined
info: [debug] ["WEBVIEW_undefined"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_undefined
info: [debug] Connecting to chrome-backed webview
info: Chromedriver: Changed state to 'starting'
info: Chromedriver: Set chromedriver binary as: C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe
info: Chromedriver: Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (netstat -nao ^| findstr /R /C:"9515 ") do (FOR /F "usebackq" %b in (TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe) do (IF NOT %b=="" TASKKILL /F /PID %a))
info: Chromedriver: No old chromedrivers seemed to exist
info: Chromedriver: Spawning chromedriver with: C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe --url-base=wd/hub --port=9515
info: Chromedriver: [STDOUT] Starting ChromeDriver 2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a) on port 9515
Only local connections are allowed.
info: JSONWP Proxy: Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
info: JSONWP Proxy: Got response with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Windows NT\",\"version\":\"10.0\"}}}"
info: JSONWP Proxy: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidProcess":"com.tencent.mm:tools","androidDeviceSerial":"02157df27164db19"}}}

所以直接切换到"androidProcess":"com.tencent.mm:tools"会报错,因为找不到这个引擎

只针对 Android 6.0+ 的解决办法:
修改 android-hybrid.js 文件,在 pkg = line [pkgColumn]; 前面加上 pkgColumn = line.length - 1; 可以解决
路径:C:、Program Files (x86)\Appium\node_modules\appium\lib\devices\android\android-hybrid.js

产生问题的原因:
在 Android 5.0 中有 9 个字段在引导时,但在 Android 6.0 中有 8 字段在引导时:
Android 5.0
USER PID PPID VSIZE RSS WCHAN PC NAME
u0_a611 23802 188 1169748 133272 ffffffff 00000000 S com.XXX.XXX.XXX

Android 6.0
User PID PPID VSIZE RSS WCHAN PC NAME
u0_a178 6346 211 1724936 123260 00000000 R com.XXX.XXX.XXX

但几秒钟后有 9 个字段
USER PID PPID VSIZE RSS WCHAN PC NAME
u0_a182 10156 211 1721024 111224 sys_epoll_ 00000000 S com.XXX.XXX.XXX

sys_epoll_字段在几秒钟后才可见

所以,当我们尝试获取 webview 中 9 个字段但是只有 8 个时,我们就得到一个 “undefined”


↙↙↙阅读原文可查看相关链接,并与作者交流