Webdriver implicitWait not working as expected

In webdriver code, if I use thread.sleep(20000). It waits 20 seconds and my code also works fine. To archive the same if I use implicit wait like

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

He does not wait for strong power for 20 seconds and proceeds to the next steps in just 3 to 4 seconds. and the page is still loading.

This is a wired situation, as I use my free time to find some items. if the elements are still loading on the page, it does not show an error and passes the test.

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

But if I say the wrong identifier, it waits 50 seconds, but the other test passed without a click .. it does not show any errors.

My question is how should I avoid Thread.sleep(), as other selenium methods do not help me.

+5
2

:

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

, . 1- , , 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>));

Update:

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. , u .

+3

, , .

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

10 .

0

All Articles