5

如果我使用 webdriver 代码thread.sleep(20000)。它正在等待 20 秒,我的代码也可以正常工作。如果我使用隐式等待,则归档相同

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 

它不会强制等待 20 秒,只需 3 到 4 秒即可进入下一步。页面仍在加载。

这是有线情况,因为我正在使用流利的等待来查找一些元素。如果元素仍在页面上加载,则不会显示错误并使测试通过。

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
  .withTimeout(50, TimeUnit.SECONDS)
  .pollingEvery(5, TimeUnit.SECONDS)
  .ignoring(NoSuchElementException.class);

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

但是,如果我说错误的 ID,它会等待 50 秒,但其他测试没有点击就通过了.. 它没有显示任何错误。

我的问题是我应该如何避免Thread.sleep(),因为其他硒方法对我没有帮助..

4

2 回答 2

3

使用以下方法等待元素:

public boolean waitForElementToBePresent(By by, int waitInMilliSeconds) throws Exception
{

    int wait = waitInMilliSeconds;
    int iterations  = (wait/250);
    long startmilliSec = System.currentTimeMillis();
    for (int i = 0; i < iterations; i++)
    {
        if((System.currentTimeMillis()-startmilliSec)>wait)
            return false;
        List<WebElement> elements = driver.findElements(by);
        if (elements != null && elements.size() > 0)
            return true;
        Thread.sleep(250);
    }
    return false;
}

下面的方法是等待页面加载:

public void waitForPageLoadingToComplete() throws Exception {
    ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            return ((JavascriptExecutor) driver).executeScript(
                    "return      document.readyState").equals("complete");
        }
    };
    Wait<WebDriver> wait = new WebDriverWait(driver, 30);
    wait.until(expectation);
}

假设您正在等待页面加载。true然后调用第一个方法,等待时间和页面加载后出现的任何元素,否则它将返回false。像这样使用它,

waitForElementToBePresent(By.id("Something"), 20000)

上面调用的函数一直等待,直到它在给定的持续时间内找到给定的元素。

在上述方法之后尝试以下任何代码

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

或者

wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));

更新:

public boolean waitForTextFiled(By by, int waitInMilliSeconds, WebDriver wdriver) throws Exception
    {
        WebDriver driver = wdriver;
        int wait = waitInMilliSeconds;
        int iterations  = (wait/250);
        long startmilliSec = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++)
        {
            if((System.currentTimeMillis()-startmilliSec)>wait)
                return false;
            driver.findElement(By.id("txt")).sendKeys("Something");
            String name =  driver.findElement(by).getAttribute("value");

            if (name != null && !name.equals("")){
                return true;
            }
            Thread.sleep(250);
        }
        return false;
    }

这将尝试在文本字段中输入文本,直到给定时间(以毫秒为单位)。如果getAttribute()不适合您的情况使用getText()。如果输入了文本,则返回 true。把你可以等到的最长时间。

于 2013-04-15T16:47:03.223 回答
0

您可能想尝试这样做以使元素在屏幕上可见。

new WebDriverWait(10, driver).until(ExpectedConditions.visibilityOfElementLocated(By.id("jxxx")).

在这种情况下,等待时间最长为 10 秒。

于 2013-04-16T02:12:17.440 回答