java - 您能否为 Java 和 Selenium 中的显式等待创建通用或可重用的方法?

标签 java selenium-webdriver

我们能否为 Java 和 Selenium 中的显式等待创建通用或可重用的方法?

使用预期条件,如 visibilityOfElementLocated、presenceOfElementLocated、elementToBeClickable

感谢任何帮助。

谢谢

最佳答案

你完全可以。我从我在这里写的另一个答案中得到这个:Selenium java SafariDriver wait for page to load after click

public ExpectedCondition<Boolean> myCustomCondition(/* some arguments from the outside */) {
return new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            // Check something on the page and return either true or false,
            // this method will be run repeatedly until either the time
            // limit is exceeded, or true is returned
        }

        public String toString() {
            return "a description of what this is waiting for";
        }
    };
}

根据我的经验,将 this 包装在一个函数中是最常见的方式。但这里要知道的一点是你想获得一个 ExpectedCondition 的实例。它提供了 apply() 和 toString() 方法的实现。

apply()只有当条件满足时才返回真。

这是一个简单的真实例子:

public ExpectedCondition<Boolean> waitForElementToHaveText(final WebElement element, final String expectedText) {
    return new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            try {
                return element.getText().equals(expectedText);
            } catch (Exception e) {
                return false; // catchall fail case
            }
        }

        public String toString() {
            return "an element to have specific text";
        }
    };
}

这可以像这样使用:

WebDriverWait wait = new WebDriverWait(driver, maxSecondsToWait);
wait.until(waitForElementToHaveText(anElement, "my visible text"));

编辑:

一些额外的注意事项,如果您注意到 ExpectedCondition 声明的泛型类型uses 是 boolean 型,也就是说你要返回true或false来判断条件是否满足。但是 null 也可以作为 false 的替代品。所以你可以这样做的另一种方法是声明你的 ExpectedCondition具有 WebElement 的通用类型不满足条件时返回 null。

public static ExpectedCondition<WebElement> waitForElementToContainText(final String cssSelector, final String visibleText) {
    return new ExpectedCondition<WebElement>() {
        @Override
        public WebElement apply(WebDriver webDriver) {
            WebElement anElement = webDriver.findElement(By.cssSelector(cssSelector);
            if (anElement.getText().contains(visibleText)) {
                return anElement; // Condition passed
            }
            return null; // Condition failed
        }

        public String toString() {
            return "first occurance of text '" + visibleText + "' in element " + cssSelector;
        }
    };
}

当然,这些示例中的任何一个都可以使用 <Boolean><WebElement>并且只想为它选择一个合适的返回值。

关于java - 您能否为 Java 和 Selenium 中的显式等待创建通用或可重用的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51196215/

相关文章:

Java Lotus Notes API/VBA nsf 文件路径

java - 当预期等于输出时测试用例失败

java - boolean 检查后单击单选按钮

java - Selenium 网络驱动程序 : sometimes sends keys to wrong field

python - 无法通过 Python 中的 Selenium 单击复选框

java - 我可以在正则表达式中组合 unicode 类别吗?

java - 如何在 Android Studio 中使用原生 C 库

java - instanceof Class<?> 参数

selenium-webdriver - Chrome 版本 71.x 未知错误 : call function result missing 'value'

java - 以编程方式启用或禁用 TestNG 中的断言