【问题标题】:Appium scripts do not work after switch to WebViewAppium 脚本在切换到 WebView 后不起作用
【发布时间】:2014-07-11 14:12:06
【问题描述】:

我尝试在混合应用程序中运行测试用例。 我可以使用 driver.context("WEBVIEW") 切换到 WebView。
但是在我切换到 webview 之后,driver.findElement() 方法在 webview 下不执行,它在 Appium 服务器中返回错误,如下所示。你能帮我解决这个问题吗?

以下是我的测试脚本:

    DesiredCapabilities capabilities = new DesiredCapabilities();

    capabilities.setCapability(CapabilityType.BROWSER_NAME, "");

    capabilities.setCapability("deviceName","066e28s70b115a3b");

    capabilities.setCapability("platformVersion", "4.4.2");

    capabilities.setCapability("platformName","Android");

    capabilities.setCapability("app", "/Users/lin/codes/tester.apk");

    capabilities.setCapability("app-package", "com.test.tester");

    capabilities.setCapability("app-activity", "com.test.tester.myActivity");

    AppiumDriver driver = new AppiumDriver(new URL("http://my.local.address:4723/wd/hub"), capabilities);


     Set<String> contextNames = driver.getContextHandles();

     System.out.println(contextNames.size());

     for (String contextName : contextNames) {

       System.out.println(contextName);

       if (contextName.contains("WEBVIEW")){

         driver.context(contextName);

       }

     }

     Thread.sleep(5000);

     driver.findElement(By.xpath("//div[@class='item item-entrance']/h2[@class='grey-title mb20']")).click();

以下代码不起作用:

 driver.findElement(By.xpath("//div[@class='item item-entrance']/h2[@class='grey-title mb20']")).click();

这是来自 Appium 服务器日志的错误消息:

info: Available contexts: NATIVE_APP,WEBVIEW_1

info: Responding to client with success: {"status":0,"value":["NATIVE_APP","WEBVIEW_1"],"sessionId":"7dd9ea41-f47c-4466-a108-21ce90284ad1"}

GET /wd/hub/session/7dd9ea41-f47c-4466-a108-21ce90284ad1/contexts 200 50ms - 124b

debug: Appium request initiated at /wd/hub/session/7dd9ea41-f47c-4466-a108-21ce90284ad1/context

debug: Request received with params: {"name":"WEBVIEW_1"}

info: Getting a list of available webviews

debug: executing: "/Users/lin/tools/android-sdk-macosx/platform-tools/adb" -s 066e28s70b115a3b shell "cat /proc/net/unix"

info: ["webview_devtools_remote_20051"]

info: Available contexts: NATIVE_APP,WEBVIEW_1

info: Connecting to chrome-backed webview

info: Creating Chrome session

info: Ensuring Chromedriver exists

info: Killing any old chromedrivers, running: ps -e | grep /Users/lin/codes/appiumGit/appium/build/chromedriver/mac/chromedriver | grep -v grep |grep -e '--port=9515$' | awk '{ print $1 }' | xargs kill -15

info: Successfully cleaned up old chromedrivers

info: Spawning chromedriver with: /Users/lin/codes/appiumGit/appium/build/chromedriver/mac/chromedriver

info: [CHROMEDRIVER] Starting ChromeDriver (v2.10.267517) on port 9515

Only local connections are allowed.

info: Making http request with opts: {"url":"http://my.local.address:9515/wd/hub/session","method":"POST","json":{"sessionId":null,"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.test.tester","androidUseRunningApp":true,"androidDeviceSerial":"066e28s70b115a3b"}}}}

info: Could not connect yet; retrying

info: Making http request with opts: {"url":"http://my.local.address:9515/wd/hub/session","method":"POST","json":{"sessionId":null,"desiredCapabilities":{"chromeOptions":{"androidPackage": "com.test.tester","androidUseRunningApp":true,"androidDeviceSerial":"066e28s70b115a3b"}}}}

info: Didn't get a new command in 60 secs, shutting down...

info: Shutting down appium session...

info: [ADB] Pressing the HOME button

debug: executing: "/Users/lingzhixiang/tools/android-sdk-macosx/platform-tools/adb" -s 066e28s70b115a3b shell "input keyevent 3"

info: Stopping logcat capture

debug: Logcat terminated with code null, signal SIGTERM

info: Cleaning up Chromedriver

info: Killing chromedriver

info: Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command. (Original error: socket hang up)","code":"ECONNRESET","origValue":"socket hang up"},"sessionId":"7dd9ea41-f47c-4466-a108-21ce90284ad1"}

POST /wd/hub/session/7dd9ea41-f47c-4466-a108-21ce90284ad1/context 500 60649ms - 270b

info: Chromedriver exited with code null

info: (killed by signal SIGTERM)

info: [BOOTSTRAP] [info] Got data from client: {"cmd":"shutdown"}

info: [BOOTSTRAP] [info] Got command of type SHUTDOWN

info: [BOOTSTRAP] [info] Returning result: {"value":"OK, shutting down","status":0}

info: [BOOTSTRAP] [info] Closed client connection

顺便说一句,我从源 (>node appium.js --no-reset) 启动了 Appium 1.0.0 服务器。

【问题讨论】:

  • 请重启设备尝试,否则使用GUI版appium。
  • 我可以通过在测试执行期间一直监控 chromedriver 来修复,当从 natiove_app 切换回 webview 时 chromedriver 变得无响应。我在github.com/mi2pankaj/chromeDriverHandler 开发了一小段代码,它解决了我所有的问题,只需将此jar chromedriverHandler.jar 添加到您的项目中并使用ChromedriverHandler.chromeDriverHandlerThread().start();在你的 beforeclass / beforetest 和 ChromedriverHandler.chromeDriverHandlerThread().stop();在你的课后/课后

标签: webview appium


【解决方案1】:

@xlzdew 请把 driver.context("WEBVIEW") 改成 driver.context("WEBVIEW_1")

【讨论】:

    【解决方案2】:

    您的日志说它无法在您的设备上启动 chrome 驱动程序。所以请检查 chromedriver

    /Users/lin/codes/appiumGit/appium/build/chromedriver/mac/chromedriver

    是否工作,或者它需要执行权限。您可以使用一些 chrome 驱动程序命令对其进行测试。

    【讨论】:

      【解决方案3】:

      我相信当您在 WebView 中时,通过 xpath 查找元素不起作用。找到我成功使用的元素的唯一方法是By.CSS_SELECTOR 和元素类(.menu-item)

      http://appium.io/slate/en/v1.2.0/?java#automating-hybrid-android-apps

      【讨论】:

      • 这非常具有误导性,绝对不真实。这应该被否决
      • 链接无效
      猜你喜欢
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 2021-01-10
      • 2016-02-26
      • 2016-11-14
      • 2016-06-13
      • 2018-10-16
      • 2020-07-14
      相关资源
      最近更新 更多