此文已由作者夏鹏授权网易云社区发布。
欢迎访问网易云社区,了解更多网易技术产品运营经验。
使用WebDriver做Web自动化的时候,org.openqa.selenium.support.ui中提供了非常便捷好用的WebDriverWait类继承FluentWait,所以可以使用FluentWait类中until方法和ExpectedCondition接口进行显示等待的定义,比如某个元素的可见或者可点击等条件,在规定的时间之内等不到指定条件那么就跳出Exception。最近使用appium做客户端的自动化时发现AppiumDriver无法使用WebDriverWait类,是由于WebDriverWait继承于FluentWait,而WebDriver接口是没有定义findElementByAccessibilityId()、findElementByIosUIAutomation()、findElementByAndroidUIAutomator()的,所以appium想使用像WebDriverWait的显示等待功能,就必须自己封装造轮子了。
①首先依葫芦(WebDriverWait)画瓢写AppiumDriverWait.javapackage 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.su