zoukankan      html  css  js  c++  java
  • appium封装显示等待Wait类和ExpectedCondition接口

    此文已由作者夏鹏授权网易云社区发布。

    欢迎访问网易云社区,了解更多网易技术产品运营经验。


    使用WebDriver做Web自动化的时候,org.openqa.selenium.support.ui中提供了非常便捷好用的WebDriverWait类继承FluentWait<WebDriver>,所以可以使用FluentWait类中until方法和ExpectedCondition<T>接口进行显示等待的定义,比如某个元素的可见或者可点击等条件,在规定的时间之内等不到指定条件那么就跳出Exception。最近使用appium做客户端的自动化时发现AppiumDriver无法使用WebDriverWait类,是由于WebDriverWait继承于FluentWait<WebDriver>,而WebDriver接口是没有定义findElementByAccessibilityId()、findElementByIosUIAutomation()、findElementByAndroidUIAutomator()的,所以appium想使用像WebDriverWait的显示等待功能,就必须自己封装造轮子了。


    ①首先依葫芦(WebDriverWait)画瓢写AppiumDriverWait.java

    package com.netease.media.qa.base.util;
    import org.openqa.selenium.NotFoundException;
    import org.openqa.selenium.TimeoutException;
    import org.openqa.selenium.WebDriverException;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.openqa.selenium.support.ui.Clock;
    import org.openqa.selenium.support.ui.FluentWait;
    import org.openqa.selenium.support.ui.Sleeper;
    import org.openqa.selenium.support.ui.SystemClock;
    import io.appium.java_client.AppiumDriver;
    import java.util.concurrent.TimeUnit;
    public class AppiumDriverWait extends FluentWait<AppiumDriver> {
      public final static long DEFAULT_SLEEP_TIMEOUT = 500;
      private final AppiumDriver driver;
      public AppiumDriverWait(AppiumDriver driver, long timeOutInSeconds) {
        this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, DEFAULT_SLEEP_TIMEOUT);
      }
      public AppiumDriverWait(AppiumDriver driver, long timeOutInSeconds, long sleepInMillis) {
        this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis);
      }
      public AppiumDriverWait(AppiumDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds,
          long sleepTimeOut) {
        super(driver, clock, sleeper);
        withTimeout(timeOutInSeconds, TimeUnit.SECONDS);
        pollingEvery(sleepTimeOut, TimeUnit.MILLISECONDS);
        ignoring(NotFoundException.class);
        this.driver = driver;
      }


    @Override
      protected RuntimeException timeoutException(String message, Throwable lastException) {
        TimeoutException ex = new TimeoutException(message, lastException);
        ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());
        if (driver instanceof RemoteWebDriver) {
          RemoteWebDriver remote = (RemoteWebDriver) driver;
          if (remote.getSessionId() != null) {
            ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());
          }
          if (remote.getCapabilities() != null) {
            ex.addInfo("Capabilities", remote.getCapabilities().toString());
          }
        }
        throw ex;
      }
    }


    ②然后还要需要修改ExpectedCondition接口,将其WebDriver的类型替换为AppiumDriver


    package com.netease.media.qa.base.util;
     import com.google.common.base.Function;
     import io.appium.java_client.AppiumDriver;
     public interface ExpectedConditionForAppium<T> extends Function<AppiumDriver, T>{
     }


    ③接下来就可以在框架封装的方法类中用appium的显示等待啦

     return  new AppiumDriverWait(driver,DriverBase.stepInterval).until(new  ExpectedConditionForAppium<WebElement>(){
      public WebElement apply(AppiumDriver driver){
       WebElement element =  DriverBase.Andriver.findElementByAndroidUIAutomator("new UiSelector().resourceId(""+locator+"")");
       return element.isDisplayed() ? element : null;
      }
     });



    网易云免费体验馆,0成本体验20+款云产品! 

    更多网易技术、产品、运营经验分享请点击


    相关文章:
    【推荐】 用scrapy数据抓取实践
    【推荐】 Hive中文注释乱码解决方案(2)
    【推荐】 知物由学 | 未来安全隐患:AI的软肋——故意欺骗神经网络

  • 相关阅读:
    【郑轻邀请赛 G】密室逃脱
    【郑轻邀请赛 C】DOBRI
    【郑轻邀请赛 F】 Tmk吃汤饭
    【郑轻邀请赛 I】这里是天堂!
    【郑轻邀请赛 B】base64解密
    【郑轻邀请赛 A】tmk射气球
    【郑轻邀请赛 H】 维克兹的进制转换
    解决adb command not found以及sdk环境配置
    adb shell 命令详解,android, adb logcat
    Unexpected exception 'Cannot run program ... error=2, No such file or directory' ... adb'
  • 原文地址:https://www.cnblogs.com/zyfd/p/9841807.html
Copyright © 2011-2022 走看看