无法将上下文设置为 WEBVIEW

Posted

技术标签:

【中文标题】无法将上下文设置为 WEBVIEW【英文标题】:cannot set context to WEBVIEW 【发布时间】:2018-06-26 22:01:17 【问题描述】:

我正在使用 Appium C# 绑定测试使用 Ionic(Cordova、AngularJS)构建的混合应用程序。与具有 id 属性的元素交互取得适度成功。能够将 css 选择器与 FindElementByCssSelector 一起使用将使我的生活变得更轻松,但是您不能将其与 NATIVE_APP 上下文一起使用。

[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["css selector", "button[ng-click='something']","0ab74e91-afc4-4f90-8702-f2be13f235c5"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator

所以我需要将上下文切换到 WEBVIEW 才能使用 css 选择器。该应用程序是使用WebView.setWebContentsDebuggingEnabled(true) 构建的。我已经验证了可用的上下文如下:

ReadOnlyCollection<string> contexts = device.Contexts;

上下文是NATIVE_APPWEBVIEW_com.blah.myapp。我因此切换到 webview:

device.Context = "WEBVIEW_com.blah.myapp";

它设置得很好,但 Chromedriver 最终以chrome not reachable 失败。这是日志记录信息。 chromdriver 错误我不知道下一步该去哪里。

[debug] [MJSONWP] Responding to client with driver.getContexts() result: ["NATIVE_APP","WEBVIEW_com.blah.myapp"]
[HTTP] <-- GET /wd/hub/session/ad3e9aee-d794-43d9-9896-497846dec4f4/contexts 200 230 ms - 119
[HTTP] --> POST /wd/hub/session/ad3e9aee-d794-43d9-9896-497846dec4f4/context "name":"WEBVIEW_com.blah.myapp"
[debug] [MJSONWP] Calling AppiumDriver.setContext() with args: ["WEBVIEW_com.blah.myapp","ad3e9aee-d794-43d9-9896-497846dec4f4"]
[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'C:\Users\xxxxx\AppData\Local\Android\Sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","0123456789ABCDEF","shell","cat","/proc/net/unix"]
[debug] [AndroidDriver] Found webviews: ["WEBVIEW_com.blah.myapp"]
[debug] [AndroidDriver] Available contexts: ["NATIVE_APP","WEBVIEW_com.blah.myapp"]
[debug] [AndroidDriver] Connecting to chrome-backed webview context 'WEBVIEW_com.blah.myapp'
[debug] [AndroidDriver] A port was not given, using random port: 8000
[debug] [Chromedriver] Changed state to 'starting'
[Chromedriver] Set chromedriver binary as: C:\Users\xxxxx\AppData\Roaming\npm\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe
[debug] [Chromedriver] Killing any old chromedrivers, running: FOR /F "usebackqtokens=5" %a in (`netstat -nao ^| findstr /R /C:"8000 "`) do (FOR /F "usebackq"%b in (`TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe`) do (IF NOT %b=="" TASKKILL /F /PID %a))
[Chromedriver] No old chromedrivers seemed to exist
[debug] [Chromedriver] Cleaning any old adb forwarded port socket connections
[debug] [ADB] List forwarding ports
[debug] [ADB] Running 'C:\Users\xxxxx\AppData\Local\Android\Sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","0123456789ABCDEF","forward","--list"]
[debug] [ADB] Removing forwarded port socket connection: 12980
[debug] [ADB] Running 'C:\Users\xxxxx\AppData\Local\Android\Sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","0123456789ABCDEF","forward","--remove","tcp:12980"]
[Chromedriver] Spawning chromedriver with: C:\Users\xxxxx\AppData\Roaming\npm\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe --url-base=wd/hub --port=8000 --adb-port=5037 --verbose
[debug] [Chromedriver] Chromedriver version: '2.34.522940'
[debug] [JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8000/wd/hub/status] with no body
[debug] [JSONWP Proxy] Got response with status 200: "\"sessionId\":\"\",\"status\":0,\"value\":\"build\":\"version\":\"alpha\",\"os\":\"arch\":\"x86_64\",\"name\":\"Windows NT\",\"version\":\"6.1.7601 SP1\""
[debug] [JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: "desiredCapabilities":"chromeOptions":"androidPackage":"com.blah.myapp","androidUseRunningApp":true,"androidDeviceSerial":"0123456789ABCDEF"
[HTTP] <-- POST /wd/hub/session/ad3e9aee-d794-43d9-9896-497846dec4f4/context - - ms - -
[debug] [JSONWP Proxy] Got response with status 200: "sessionId":"bbe4bf5c4a6bdd020f27e07624258589","status":100,"value":"message":"chrome not reachable\n  (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86_64)"
[Chromedriver] Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable  (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86_64))

编辑: 使用 Appium 1.7.2-beta5(在之前的版本中也有同样的失败) Android 7.0 和 6.0,还有真机和模拟器

【问题讨论】:

【参考方案1】:

这是Appium & Chromedriver 的一个持续问题,没有针对每种情况的确切解决方案,因此您必须尝试不同的解决方案:

    试用最新的 beta - 1.8.0-beta 可用:
npm uninstall -g appium && npm install -g appium@beta

    使用这些instructions 尝试不同版本的 chromedriver:

    下载并解压 chromedriver 使用下载的 chromedriver 的路径启动 appium 服务器
    appium --chromedriver-executable /path/to/my/chromedriver
    
    运行您的测试,看看它是否有效

您还可以跟踪特定于 1.7.2-beta 和 Android 6 的 this issue 的进度。

【讨论】:

非常感谢您提供此信息。很棒的建议。它没有解决问题,但至少现在我知道我已经尽力了。 我正在调查更多的事情,比如删除 crosswalk webview 插件。如果有什么工作会更新。

以上是关于无法将上下文设置为 WEBVIEW的主要内容,如果未能解决你的问题,请参考以下文章

当上下文设置为 WebView 时,VideoView 不会播放

无法为 WebView 创建层,尺寸 640x7040 最大尺寸 8192 颜色类型 4 具有上下文 1

无法在 iOS Appium 测试(AWS Device Farm)上切换到 WebView 上下文

在 mac 上使用真正的 android 设备运行自动化测试时,是不是可以将上下文切换到 WebView?

无法在 android webview 上执行操作

Android Webview 应用程序上下文