TestNG Java Selenium。 Thread.sleep()有哪些替代方案

时间:2018-05-08 14:42:28

标签: java selenium-webdriver

根据我看过的一些文章,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;
    

    }

  3. 和我在里面调用的函数:

    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;
    }
    

3 个答案:

答案 0 :(得分:4)

您可以使用FluentWait

  

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

refs/heads/1954-branch-a ---------------------------------------------------------------------------------------------------------------------------------------- 2.9.27
refs/heads/1955-branch-b ---------------------------------------------------------------------------------------------------------------------------------------- 2.9.43
refs/heads/1965-branch-c ---------------------------------------------------------------------------------------------------------------------------------------- 2.9.32
refs/heads/1968-branch-d ---------------------------------------------------------------------------------------------------------------------------------------- 2.9.101
refs/heads/1969-branch-e ---------------------------------------------------------------------------------------------------------------------------------------- 2.9.114
refs/heads/master ----------------------------------------------------------------------------------------------------------------------------------------------- 2.9.115

另一个等待available

答案 1 :(得分:1)

您的代码只会尝试等待页面加载,而不是等待特定元素等加载。这适用于静态HTML页面,但是一旦你开始向页面添加动态部分(使用AJAX等),它将无法实现你想要的效果。

您可以使用WebDriverWait。有关详细信息,请参阅the docs

简单的例子,

// create an instance that can be reused
WebDriverWait wait = new WebDriverWait(driver, 10);
// wait for an element to be clickable and store the return
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("someId"));
// click the returned button
button.click();

ExpectedConditions提供了许多标准条件。有关详细信息,请参阅the docs

答案 2 :(得分:1)

我发现了另一种方法,尽管它与Thread.sleep()非常相似。我正在寻找另一个等待,因为对Selenium的隐式等待和显式等待对我来说等待时间不够长。

我使用的替代方法:

TimeUnit.SECONDS.sleep(int timeUnit)

这提供了与Thread.sleep()相同的功能。希望对您有所帮助。