saba
saba

Reputation: 539

Infinite Wait in Selenium WebDriver

I actually have 3 questions:

  1. How Selenium WebDriver implements this driver.manage().timeouts().pageLoadTimeout(-1, TimeUnit.SECONDS)

because we cannot give a negative value to a thread for infinite sleep.

  1. I make a code for infinite sleep like this
    WebDriver driver=new FirefoxDriver(fb, fp);
    driver.get("http://www.google.com");
    String str="'Feeling Lucky'";
    driver.manage().timeouts().pageLoadTimeout(-1, TimeUnit.SECONDS)
    //driver.findElement(By.xpath(".//span[contains(text(),"+str+")]")).click();
    while(true)
    {
        try
        {
            driver.findElement(By.xpath(".//span[contains(text(),"+str+")]")).click();
            break;
        }
        catch(Exception e)
        {
            Thread.sleep(500);
        }
    }
    driver.quit();

Is there any better way to implement a infinite wait?

  1. I do a test for infinite wait on webdriver implementation using driver.manage().timeouts().pageLoadTimeout(-1, TimeUnit.SECONDS). What I do I just disconnect the network connection when page try to load but I got a unexpected result that after some time driver quite but it should not quite the browser it should wait infinite.

We see that in selenium webdriver documentation

    /**
     * Sets the amount of time to wait for a page load to complete before throwing an error.
     * If the timeout is negative, page loads can be indefinite.
     *
     * @param time The timeout value.
     * @param unit The unit of time.
     * @return A Timeouts interface.
     */
    Timeouts pageLoadTimeout(long time, TimeUnit unit);

Upvotes: 1

Views: 6597

Answers (2)

Justin Lambert
Justin Lambert

Reputation: 978

Hi If you create wait method and keep inside your project you can use that wait method any time and anywhere , This is customize one

 private static WebElement waitForElement(By locator,int timeout)
{
    WebElement element=new WebDriverWait(driver,timeout).until(ExpectedConditions.presenceOfElementLocated(locator));
    return element;
}

for example if you want wait for element id you can line code as below

**waitForElement(By.id(""),20);**

Here 20 is milliseconds ,you can use any time here and webelement

Upvotes: 0

ddavison
ddavison

Reputation: 29062

I'd recommend adding your own findElement() method, instead of trying to cheat Selenium.

DISCLAIMER: I wholeheartedly recommend you do NOT do this. The waits are in there for a reason. If you have an infinite wait, then your test will run forever if you don't manually intervene if something DOES go wrong. Instead, I recommend setting it to a very high number, but NOT infinite.

You could do something like:

WebElement findElement(By by) {
  int attempts = 0;
  while (attempts > 300)
    try {
      return driver.findElement(by);
    } catch (Exception e) {
      Thread.sleep(500);
      attempts++;
    }
  }
}

It's unnecessary to have the infinite loop there.

Again, I DONT recommend doing above! I am only providing you that solution since it is what you are explicitly asking for.

Another thing you could do is, if you are really wanting to set a big wait:

driver.manage().timeouts().pageLoadTimeout(300, TimeUnit.SECONDS)

That is 300 seconds... Even for me, this is extremely janky and shouldn't be put in. Web Pages should NOT take 300 seconds to load. If it does, then there is something else you need to fix ;)

Upvotes: 3

Related Questions