如何在QAF框架中实现explicit wait和fluent wait功能?
QAF框架显式等待与流畅等待实现方案
QAF本身完全兼容Selenium原生等待API,同时也做了更便捷的封装,以下是两种场景的具体实现方式:
1 类Selenium原生写法的显式等待
你可以直接复用Selenium的ExpectedConditions工具类,搭配QAFWebDriverWait实现和Selenium完全一致的显式等待逻辑:
import org.openqa.selenium.support.ui.ExpectedConditions; import com.qmetry.qaf.automation.ui.webdriver.QAFWebDriverWait; // 示例:等待指定元素可点击,最长等待10秒 QAFWebDriverWait wait = new QAFWebDriverWait(driver, 10); wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn")));
你提到的QAFWebElementExpectedCondition类也封装了大量QAF专属的预期条件,比如元素属性校验、列表状态校验等,可直接静态调用:
import static com.qmetry.qaf.automation.ui.webdriver.QAFWebElementExpectedCondition.attributeToBe; // 示例:等待元素的disabled属性变为false wait.until(attributeToBe(submitBtn, "disabled", "false"));
2 QAF内置元素等待(更简洁)
如果你的页面对象使用QAFWebElement定义,不需要单独实例化等待类,直接调用元素自带的等待方法即可:
import com.qmetry.qaf.automation.ui.webdriver.QAFWebElement; QAFWebElement submitBtn = new QAFWebElementImpl(By.id("submitBtn")); // 等待元素可见,默认超时时长可通过配置文件修改 submitBtn.waitForVisible(); // 自定义等待5秒直到元素可点击 submitBtn.waitForClickable(5); // 等待元素文本包含指定内容 submitBtn.waitForText("立即提交");
常用的内置等待方法还有waitForEnabled()、waitForNotPresent()、waitForAttribute()等,可直接按需调用。
3 流畅等待(Fluent Wait)实现
QAF完全兼容Selenium的FluentWait配置逻辑,可自定义超时时间、轮询间隔、忽略的异常类型:
import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.NoSuchElementException; import java.time.Duration; // 示例:最长等待20秒,每2秒轮询一次,忽略元素不存在异常 FluentWait<QAFWebDriver> fluentWait = new FluentWait<>(driver) .withTimeout(Duration.ofSeconds(20)) .pollingEvery(Duration.ofSeconds(2)) .ignoring(NoSuchElementException.class); // 自定义等待条件 fluentWait.until(driver -> { QAFWebElement statusTip = driver.findElement(By.id("op_status")); return statusTip.getText().contains("操作成功"); });
内容的提问来源于stack exchange,提问作者Vinee-the-Pooh
相关产品推荐
相关产品推荐

