测试人社区

【appium】求助,appium切换webview时报错,DevTools request: http://localhost:12541/json/version [Chromedriver] [STDERR] [1.840][DEBUG]: DevTools request failed

desiredCapabilities相关配置信息:

desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "android");
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "test");
desiredCapabilities.setCapability(MobileCapabilityType.UDID, deviceId);
desiredCapabilities.setCapability(MobileCapabilityType.APP,  app);
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, appPackage);
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, getActivity(deviceId, appPackage));
desiredCapabilities.setCapability(AndroidMobileCapabilityType.NO_SIGN, true);
desiredCapabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, true); 
// Appium在没有收到下一个命令时,默认超时时间是60s,超时后应用将会自动关闭
desiredCapabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 3600);
desiredCapabilities.setCapability(AndroidMobileCapabilityType.RECREATE_CHROME_DRIVER_SESSIONS, true);
desiredCapabilities.setCapability(AndroidMobileCapabilityType.DONT_STOP_APP_ON_RESET, true);
desiredCapabilities.setCapability(MobileCapabilityType.NO_RESET, true); 
desiredCapabilities.setCapability("chromedriverExecutableDir", CHROMEDRIVER_EXECUTABLE_DIT_PATH);
desiredCapabilities.setCapability("showChromedriverLog", true);
desiredCapabilities.setCapability("relaxedSecurityEnabled", true);

本地已开启chromedriver:

➜  ~ ps -ef | grep chromedriver
  501 13324 13170   0  9:19上午 ttys001    0:00.04 /Users/apple/Data/appium/Touchtv/chromeDriver/chromedriver_2.29 --url-base=wd/hub --port=8000 --adb-port=5037 --verbose

端口也重定向了:

➜  ~ adb forward --list
605dcd5b tcp:8200 tcp:6790
605dcd5b tcp:8201 tcp:6790
605dcd5b tcp:8202 tcp:6790
605dcd5b tcp:12541 localabstract:webview_devtools_remote_12178

➜  ~ adb -P 5037 -s 605dcd5b shell cat /proc/net/unix | grep webview
00000000: 00000002 00000000 00010000 0001 01 107458 @webview_devtools_remote_m5x_12178
➜  ~ adb -P 5037 -s 605dcd5b shell ps 12178
USER      PID   PPID  VSIZE  RSS   WCHAN            PC  NAME
u0_a126   12178 315   1636520 289692 sys_epoll_ 00000000 S com.cgbchina.xpt

chrome://inspect/#devices已经加装出了webview的画面和数据

错误日志

[debug] [ADB] Getting package info for 'com.google.android.webview'
[debug] [ADB] Running '/Users/apple/Library/Android/sdk/platform-tools/adb -P 5037 -s 605dcd5b shell dumpsys package com.google.android.webview'
[debug] [Chromedriver] Found Chrome bundle 'com.google.android.webview' version '57.0.2987'
[debug] [Chromedriver] Found 1 Chromedriver executable capable of automating Chrome '57.0.2987'.
[debug] [Chromedriver] Choosing the most recent, '/Users/apple/Data/appium/Touchtv/chromeDriver/chromedriver_2.29'.
[debug] [Chromedriver] If a specific version is required, specify it with the `chromedriverExecutable`desired capability.
[Chromedriver] Set chromedriver binary as: /Users/apple/Data/appium/Touchtv/chromeDriver/chromedriver_2.29
[debug] [Chromedriver] Killing any old chromedrivers, running: pkill -15 -f "/Users/apple/Data/appium/Touchtv/chromeDriver/chromedriver_2.29.*--port=8000"
[Chromedriver] No old chromedrivers seem to exist
[debug] [Chromedriver] Cleaning any old adb forwarded port socket connections
[debug] [ADB] List forwarding ports
[debug] [ADB] Running '/Users/apple/Library/Android/sdk/platform-tools/adb -P 5037 -s 605dcd5b forward --list'
[debug] [ADB] Removing forwarded port socket connection: 12292
[debug] [ADB] Running '/Users/apple/Library/Android/sdk/platform-tools/adb -P 5037 -s 605dcd5b forward --remove tcp\:12292'
[Chromedriver] Spawning chromedriver with: /Users/apple/Data/appium/Touchtv/chromeDriver/chromedriver_2.29 --url-base=wd/hub --port=8000 --adb-port=5037 --verbose
[debug] [Chromedriver] Chromedriver version: '2.29.461585'
[debug] [Chromedriver] Chromedriver v. 2.29.461585 does not fully support W3C protocol. Defaulting to MJSONWP
[debug] [Chromedriver] [STDOUT] Starting ChromeDriver 2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b) on port 8000
[debug] [Chromedriver] [STDOUT] Only local connections are allowed.
[debug] [WD Proxy] Matched '/status' to command name 'getStatus'
[debug] [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8000/wd/hub/status] with no body
[debug] [WD Proxy] Got response with status 200: {"sessionId":"","status":0,"value":{"build":{"version":"alpha"},"os":{"arch":"x86_64","name":"Mac OS X","version":"10.14.6"}}}
[Chromedriver] Starting MJSONWP Chromedriver session with capabilities: {
[Chromedriver]   "desiredCapabilities": {
[Chromedriver]     "chromeOptions": {
[Chromedriver]       "androidPackage": "com.cgbchina.xpt",
[Chromedriver]       "androidUseRunningApp": true,
[Chromedriver]       "androidDeviceSerial": "605dcd5b"
[Chromedriver]     },
[Chromedriver]     "loggingPrefs": {
[Chromedriver]       "browser": "ALL"
[Chromedriver]     }
[Chromedriver]   }
[Chromedriver] }
[debug] [WD Proxy] Matched '/session' to command name 'createSession'
[debug] [WD Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.cgbchina.xpt","androidUseRunningApp":true,"androidDeviceSerial":"605dcd5b"},"loggingPrefs":{"browser":"ALL"}}}
[Chromedriver] [STDERR] [0.017][INFO]: COMMAND InitSession {
[Chromedriver] [STDERR]    "desiredCapabilities": {
[Chromedriver] [STDERR]       "chromeOptions": {
[Chromedriver] [STDERR]          "androidDeviceSerial": "605dcd5b",
[Chromedriver] [STDERR]          "androidPackage": "com.cgbchina.xpt",
[Chromedriver] [STDERR]          "androidUseRunningApp": true
[Chromedriver] [STDERR]       },
[Chromedriver] [STDERR]       "loggingPrefs": {
[Chromedriver] [STDERR]          "browser": "ALL"
[Chromedriver] [STDERR]       }
[Chromedriver] [STDERR]    }
[Chromedriver] [STDERR] }
[Chromedriver] [STDERR] [0.017][DEBUG]: Sending adb command: host:devices
[Chromedriver] [STDERR] [0.018][DEBUG]: Received adb response: 605dcd5b	device
[Chromedriver] [STDERR] [0.018][DEBUG]: Sending adb command: host:transport:605dcd5b|shell:pm path com.cgbchina.xpt
[Chromedriver] [STDERR] [1.323][DEBUG]: Received adb response: package:/data/app/com.cgbchina.xpt-1/base.apk
[Chromedriver] [STDERR] [1.323][DEBUG]: Sending adb command: host:transport:605dcd5b|shell:ps
[Chromedriver] [STDERR] [1.445][DEBUG]: Received adb response: USER      PID   PPID  VSIZE  RSS   WCHAN            PC  NAME
[Chromedriver] [STDERR] root      1     0     5560   1256  sys_epoll_ 00000000 S /init
[Chromedriver] [STDERR] root      2     0     0      0       kthreadd 00000000 S kthreadd
[Chromedriver] [STDERR] root      3     2     0      0     run_ksofti 00000000 S ksoftirqd/0
[Chromedriver] [STDERR] root      5     2     0      0     worker_thr 00000000 S kworker/0:0H
[Chromedriver] [STDERR] root      6     2     0      0     msm_mpm_wo 00000000 D kworker/u:0
[Chromedriver] [STDERR] root      7     2     0      0     msm_rpm_sm 00000000 D kworker/u:0H
[Chromedriver] [STDERR] root      8     2     0      0     cpu_stoppe 00000000 S migration/0
....太长了,只打印部分

[Chromedriver] [STDERR] [1.470][DEBUG]: Sending adb command: host-serial:605dcd5b:forward:tcp:12541;localabstract:webview_devtools_remote_12178
[Chromedriver] [STDERR] [1.471][DEBUG]: DevTools request: http://localhost:12541/json/version
[Chromedriver] [STDERR] [1.472][DEBUG]: DevTools request failed
[Chromedriver] [STDERR] [1.523][DEBUG]: DevTools request: http://localhost:12541/json/version
[Chromedriver] [STDERR] [1.525][DEBUG]: DevTools request failed
[Chromedriver] [STDERR] [1.576][DEBUG]: DevTools request: http://localhost:12541/json/version
[Chromedriver] [STDERR] [1.578][DEBUG]: DevTools request failed

推测可能浏览器和驱动版本不一致,查一下,首先要保证版本一致,不行可以直接指定driver路径。

应该是一致的,我试过同一台手机,三个不同的app,有两个是正常切换的,有个是现公司的一个银行项目app,一直切换报这个错误。他们都打开了webview的调式。

下次 标签要选择一下“open”哈,要不都看不到

首先确定环境没有问题:

  1. 打开webview开关
  2. chromedriver的版本与app里webview的版本一致, 不同的app所用的webview版本有可能是不同的。

如果环境没有问题,再确定一下端口有没有被占用,或者被公司限制。
如果这些都没有解决,考虑换一台设备试试。

问题定位到了:

问题描述:

  • 环境:Appium v1.16.0
  • 移动端型号:小米4S
  • 移动端系统:android6.0.1
  • chromedriver版本:v2.29
  • 问题复述:appium 切换 webview 失败。

相关log:

app 打开 webview 的进程,期望切换的 webview_devtools_remote_m5x_9629 进程

➜  ~ adb -P 5037 -s 605dcd5b shell cat /proc/net/unix | grep webview
00000000: 00000002 00000000 00010000 0001 01 76169 @webview_devtools_remote_8506
00000000: 00000002 00000000 00010000 0001 01 88495 @webview_devtools_remote_m5x_9629
00000000: 00000003 00000000 00000000 0001 03 73673 @webview_devtools_remote_8506
00000000: 00000003 00000000 00000000 0001 03 76304 @webview_devtools_remote_8506

➜  ~ adb -P 5037 -s 605dcd5b shell ps 9629
USER      PID   PPID  VSIZE  RSS   WCHAN            PC  NAME
u0_a126   9629  315   1695512 339232 sys_epoll_ 00000000 S com.cgbchina.xpt
➜  ~ adb -s 605dcd5b forward tcp:17778 localabstract:webview_devtools_remote_m5x_9629

appium log,从第一行的日志看,是把m5x给去掉了:

[Chromedriver] [STDERR] [1.330][DEBUG]: Sending adb command: host-serial:605dcd5b:forward:tcp:12574;localabstract:webview_devtools_remote_9629
[Chromedriver] [STDERR] [1.331][DEBUG]: DevTools request: http://localhost:12574/json/version
[Chromedriver] [STDERR] [1.333][DEBUG]: DevTools request failed
[Chromedriver] [STDERR] [1.383][DEBUG]: DevTools request: http://localhost:12574/json/version
[Chromedriver] [STDERR] [1.384][DEBUG]: DevTools request failed

报错信息:

[Chromedriver] [STDERR] [1.383][DEBUG]: DevTools request: http://localhost:12574/json/version
[Chromedriver] [STDERR] [1.384][DEBUG]: DevTools request failed

原因:

重定向的结果,发现appium重定向时,m5x没有拼接进去:

➜  ~ adb forward --list
# 运行 appium 切换 webview 时,appium自己重新向的端口
605dcd5b tcp:12574 localabstract:webview_devtools_remote_9629

# 自己手动重定向端口
605dcd5b tcp:17778 localabstract:webview_devtools_remote_m5x_9629

访问结果:

➜  ~ curl http://127.0.0.1:12574/json/version
curl: (56) Recv failure: Connection reset by peer


➜  ~ curl http://127.0.0.1:17778/json/version
{
   "Android-Package": "com.cgbchina.xpt",
   "Browser": "Chrome/57.0.2987.108",
   "Protocol-Version": "1.2",
   "User-Agent": "Mozilla/5.0 (Linux; Android 6.0.1; MI 4LTE Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 Mobile Safari/537.36",
   "V8-Version": "6.3.298",
   "WebKit-Version": "537.36 (@a000f5daeaac3f79102a0c8f6eaab57aa0e00ae9)"
}

解决方案:

那这种问题怎么解决呢,是要改下appium源码吗?

  1. 尝试添加 caps 。

http://appium.io/docs/en/writing-running-appium/caps/

  1. 不行了问一下贵司Android大佬,webview 名字能改不,如果可以就美滋滋。webview_devtools_remote_m5x,chromedriver 默认查找webview的字符串 webview_devtools_remote。之前一段时间 微信小程序webview改成xweb开头的就用不了。

3. 最后只能修改chromedriver源码来。编译很麻烦,代价太大。https://chromium.googlesource.com/chromium/src/+/master/chrome/test/chromedriver/chrome/device_manager.cc

问题已解决,开发去掉了m5x。 :rofl: :rofl: :rofl:
注:添加caps方法行不通,解决不了问题

1 Like