appium java动态等待_appium封装显示等待Wait类和ExpectedCondition接口

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

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

使用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.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 {

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 extends Function{

}

③接下来就可以在框架封装的方法类中用appium的显示等待啦return  new AppiumDriverWait(driver,DriverBase.stepInterval).until(new  ExpectedConditionForAppium(){

public WebElement apply(AppiumDriver driver){

WebElement element =  DriverBase.Andriver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\""+locator+"\")");

return element.isDisplayed() ? element : null;

}

});

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值