使用 Appium 测试 Android Webview 的各种问题 [英] Variety of issues testing Android Webview using Appium

查看:20
本文介绍了使用 Appium 测试 Android Webview 的各种问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个混合应用程序,主要只是一个 web 视图.这是基本测试的代码:

 @BeforeClasspublic static void setupAppium() 抛出 MalformedURLException {DesiredCapabilities 能力 = 新的 DesiredCapabilities();capabilities.setCapability("appium-version", "1.5.3");capabilities.setCapability("platformName", "Android");capabilities.setCapability("platformVersion", "6.0");Capability.setCapability(MobileCapabilityType.DEVICE_NAME, "AppiumTesterDevice");capabilities.setCapability("app", "/work/builds/unify/app/build/outputs/apk/app-debug.apk");capabilities.setCapability("appPackage", "com.company.unify");Capability.setCapability("appActivity", "AdministerActivity");driver = new AndroidDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);}@测试public void CompanyUrlTest() {driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]")).clear();driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]")).sendKeys(companyserverUrl);driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[3]/android.widget.Button[2]")).click();}@测试公共无效简单测试(){driver.context("WEBVIEW_com.company.unify");WebDriverWait wait = new WebDriverWait(driver,20);wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".localPickerList")));driver.findElementByCssSelector(".localePickerList").click();driver.findElementByCssSelector(".SPANISH_PR").click();}

第一个测试工作正常,因为它只是在普通的 android 应用程序上运行,没有 webview.因此,它只是更改文本字段中的 URL 并按下按钮将我们带到 web 视图中的该 URL.

第二次测试是哪里有问题,问题根据android版本和chromedriver版本而变化.我已经进行了大量的谷歌搜索,只能得出结论,appium/selendroid/chromedriver 存在无穷无尽的错误.

我从 https://sites.google.com 下载了最新的 Chromedriver/a/chromium.org/chromedriver/downloads.在我的 appium 设置中,我已将 chromedriver 路径设置为指向此.在我的 Android 6.0 中,此版本的 ChromeDriver 出现此错误:

org.openqa.selenium.WebDriverException:处理命令时发生未知的服务器端错误.原始错误:无法停止进程;它当前没有运行(警告:服务器没有提供任何堆栈跟踪信息)命令持续时间或超时:224 毫秒构建信息:版本:'未知',修订:'1969d75',时间:'2016-10-18 09:43:45 -0700'系统信息:主机:'company-me-m',ip:'192.168.1.154',os.name:'Mac OS X',os.arch:'x86_64',os.version:'10.11.4',java.version: '1.8.0_45'驱动信息:io.appium.java_client.android.AndroidDriver功能 [{app=/work/builds/unify/app/build/outputs/apk/app-debug.apk, appPackage=com.company.unify, networkConnectionEnabled=true, warnings={}, databaseEnabled=false, deviceName=emulator-5554,平台=LINUX,设备UDID=emulator-5554,appActivity=AdministerActivity,期望={app=/work/builds/unify/app/build/outputs/apk/app-debug.apk,appPackage=com.comany.unify,appActivity=AdministerActivity,appium-version=1.5.3,platformVersion=6.0,automationName=Appium,platformName=Android,deviceName=AppiumTesterDevice},appium-version=1.5.3,platformVersion=6.0,webStorageEnabled=false,locationContextEnabled=false,自动化名称=Appium,takesScreenshot=true,javascriptEnabled=true,platformName=Android}]会话 ID:bc7100bd-2498-47d2-b0f2-3b29602c8e0d在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)在 java.lang.reflect.Constructor.newInstance(Constructor.java:422)在 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)在 io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:40)在 io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)在 io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)在 io.appium.java_client.AppiumDriver.context(AppiumDriver.java:557)在 com.comapny.UnifyAppiumJunit.BasicTest.SimpleTest(BasicTest.java:57)

如果没有在 appium 选项中设置这个版本的 Chromedriver,我会得到一个不同的错误(但是,我不知道当我没有指定它时使用的是什么 chromedriver 可执行文件).这是错误:

2017 年 1 月 27 日下午 3:02:39 org.openqa.selenium.support.ui.ExpectedConditions findElement警告:由 findElement(By.cssSelector: .localPickerList) 抛出的 WebDriverExceptionorg.openqa.selenium.NoSuchSessionException: 没有这样的会话

在这两种情况下,它似乎甚至都不让我的 webview 加载.当应用程序加载 webview 时,代码仍然运行,这看起来很糟糕.为什么 Wait 似乎没有做任何事情?这还重要吗?

最后,如果我使用我的 Android 5.1.1 模拟器,我会收到这个错误:

WebDriverException 由 findElement org.openqa.selenium.WebDriverException 抛出:未知错误:超出最大调用堆栈大小

有什么想法吗?我一整天都在为这件事与谷歌争吵.

解决方案

使用新的 UIautomator,您无需将驱动程序切换到 webview.它具有识别 webView 中元素的内置功能.<​​/p>

请更新一次您的 Android SDK.

WebView 中的元素将在适用于 Android 6.0+ 版本的 UIAutomator 中可见.但是,一旦您为 Android 6.0+ 设备自动化脚本,您就可以为低于 6.0 的 Android 版本使用相同的脚本.

您还可以使用 UIAutomator 在 Android 6.0+ 设备上查看 webView 中的元素.

有关更多信息,请参阅此链接

I have a hybrid app that is primarily just a webview. Here is the code for the basic test:

  @BeforeClass
  public static void setupAppium() throws MalformedURLException {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("appium-version", "1.5.3");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("platformVersion", "6.0");
    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "AppiumTesterDevice");
    capabilities.setCapability("app", "/work/builds/unify/app/build/outputs/apk/app-debug.apk");
    capabilities.setCapability("appPackage", "com.company.unify");
    capabilities.setCapability("appActivity", "AdministerActivity");
    driver = new AndroidDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
  }

  @Test
  public void CompanyUrlTest() {
    driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]")).clear();
    driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]")).sendKeys(companyserverUrl);
    driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[3]/android.widget.Button[2]")).click();
  }

  @Test
  public void SimpleTest() {
    driver.context("WEBVIEW_com.company.unify");
    WebDriverWait wait = new WebDriverWait(driver,20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".localPickerList")));
    driver.findElementByCssSelector(".localePickerList").click();
    driver.findElementByCssSelector(".SPANISH_PR").click();
  }

The first test works fine because it's just operating on normal android app, no webview. So its just changing the URL in a text field and pressing a button to take us to that URL in the webview.

The second test is where there are problems, and the problems change depending on android version and chromedriver version. I've done tons of googling and can only conclude there is an endless series of bugs with appium/selendroid/chromedriver.

I downloaded the latest Chromedriver from https://sites.google.com/a/chromium.org/chromedriver/downloads. And in my appium settings, I have set the chromedriver path to point at this. In my Android 6.0 I get this error with this version of ChromeDriver:

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Can't stop process; it's not currently running (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 224 milliseconds
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'company-me-m', ip: '192.168.1.154', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11.4', java.version: '1.8.0_45'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{app=/work/builds/unify/app/build/outputs/apk/app-debug.apk, appPackage=com.company.unify, networkConnectionEnabled=true, warnings={}, databaseEnabled=false, deviceName=emulator-5554, platform=LINUX, deviceUDID=emulator-5554, appActivity=AdministerActivity, desired={app=/work/builds/unify/app/build/outputs/apk/app-debug.apk, appPackage=com.comany.unify, appActivity=AdministerActivity, appium-version=1.5.3, platformVersion=6.0, automationName=Appium, platformName=Android, deviceName=AppiumTesterDevice}, appium-version=1.5.3, platformVersion=6.0, webStorageEnabled=false, locationContextEnabled=false, automationName=Appium, takesScreenshot=true, javascriptEnabled=true, platformName=Android}]
Session ID: bc7100bd-2498-47d2-b0f2-3b29602c8e0d

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:40)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at io.appium.java_client.AppiumDriver.context(AppiumDriver.java:557)
    at com.comapny.UnifyAppiumJunit.BasicTest.SimpleTest(BasicTest.java:57)

Without this version of Chromedriver set in appium options, I get a different error (I don't however, know what chromedriver executable its using when I'm not specifying it). Here is that error:

Jan 27, 2017 3:02:39 PM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.cssSelector: .localPickerList)
org.openqa.selenium.NoSuchSessionException: no such session

It seems like it's not even letting my webview load in both cases. While the app is loading the webview, the code runs anyway, which seems bad. Why isn't the Wait seemingly doing anything? Does this even matter?

Finally, if I use my Android 5.1.1 emulator, I get this error:

WebDriverException thrown by findElement org.openqa.selenium.WebDriverException: unknown error: Maximum call stack size exceeded

Any ideas? I've been banging my head against google all day over this.

解决方案

With new UIautomator you don't need to switch your driver to webview. It has built in capabilities to identify the elements inside webView.

Please update your Android SDK once.

Elements inside WebView will be visible in UIAutomator for Android Version 6.0+ versions. However, once you automate the script for Android 6.0+ device, you can use the same script for Android version below 6.0.

And also you can see the elements inside webView on Android 6.0+ devices using UIAutomator.

For more information refer this link

这篇关于使用 Appium 测试 Android Webview 的各种问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆