首页 > 解决方案 > TestNG Java 硒。Thread.sleep() 的替代方法是什么

问题描述

根据我看到的一些文章,使用 Thread.sleep() 似乎是不受欢迎的。我在我的测试类中经常使用它,需要等待加载。我曾尝试使用此方法告诉我加载何时完成,但这没有帮助。这不是一个可靠的方法。:

try {
        return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("loaded")
                || ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
    } catch (Exception e) {
        System.err.println(thisClass + " Exception caught: " + e.getMessage());
    }

我可以使用哪些替代方法?

该解决方案有效:

  1. 调用函数:

    String className = "gwt-Button form-control btn back-button ripple-container";
    String textToFind = "back-button-icon";
    String htmlElement = "button";
    boolean isReady = Common.FluentWait(driver, 60, className, textToFind, htmlElement);
    
  2. 那个功能:

    public static boolean FluentWait(WebDriver driver, int timeOut, String className, String textToFind,
        String htmlElement) {
    // Waiting timeOut seconds for an element to be present on the page, checking
    // for its presence once every 5 seconds.
    Common.myPrint(thisClass + " FluentWait. ");
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeOut, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
    
    WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            Common.myPrint(thisClass + " run returnWebElement. ");
            return returnWebElement(className, textToFind, htmlElement, driver);
        }
    });
    
    if (foo != null) {
        return true;
    }
    return false;
    

    }

以及我在其中调用的函数:

public static WebElement returnWebElement(String className, String textToFind, String htmlElement,
        WebDriver driver) {

    List<WebElement> elements = Common.findElementsUsingHtmlXpathClass(driver, htmlElement, className);

    Common.myPrint(thisClass + " elements count: " + elements.size());
    String text = "";
    for (WebElement element : elements) {
        // select an element
        if (element.isDisplayed()) {
            text = element.getAttribute("innerHTML");
            if (text != "") {
                text = text.trim();
                if (text.contains(textToFind)) {
                    Common.myPrint(thisClass + " innerHTML: " + text);
                    Common.myPrint(thisClass + " returning element found. ");
                    return element;
                }
            }
        }
    }
    Common.myPrint(thisClass + " element not found. ");
    return null;
}

标签: javaselenium-webdriver

解决方案


您可以使用 FluentWait

每个 FluentWait 实例定义等待条件的最长时间,以及检查条件的频率。此外,用户可以将等待配置为在等待时忽略特定类型的异常,例如在页面上搜索元素时的 NoSuchElementExceptions。

/* 
code snippet will Wait 30 seconds for 
an element to be present on the page and check for its 
presence once every 5 seconds.
*/
Wait wait = new FluentWait(driver)
    .withTimeout(30, SECONDS)
    .pollingEvery(5, SECONDS)
    .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function() {
    public WebElement apply(WebDriver driver) {
        return driver.findElement(By.id("foo"));
    }
});

另一个等待也可用


推荐阅读