此文已由作者夏鹏授权网易云社区发布。
欢迎访问网易云社区,了解更多网易技术产品运营经验。
使用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+款云产品!
更多网易技术、产品、运营经验分享请点击。
相关文章:
【推荐】 MongoDB复制集成员及状态转换
【推荐】 初识Continuation