Nital
Nital

Reputation: 6084

Not able to make selenium webdriver wait for specified period

I am using the following piece of code so that selenium driver waits 10 seconds before doing any further processing. But the page load is too fast and it seems like increasing or decreasing the period from 10 seconds to 20 seconds does not make any difference. I even change the TimeUnit.SECONDS to TimeUnit.MINUTES but no difference.

What am I missing ?

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

CheckoutTest.java

@Test
public void testCheckoutScenario() throws InterruptedException {
    driver.get(WEBSTORE_BASE_URL); //hit the Home Page
    assertEquals("Home Page", driver.getTitle().trim());

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

    //navigate to category page
    driver.findElement(By.linkText("Gear")).click();
    assertEquals("Gear", driver.getTitle().trim());
    //navigate to subcategory page
    driver.findElement(By.linkText("Watches")).click();
    assertEquals("Watches - Gear", driver.getTitle().trim());

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

    //some more code
}

I am using Selenium 3.0, Google Chrome Webdriver and Java 8 for running this test.

Upvotes: 0

Views: 1298

Answers (4)

yojna
yojna

Reputation: 513

Implicit wait gives max time to wait. But it will run next line before wait is over. So Try to use WebDriverWait class to wait properly until specified condition.

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By
                .className("session")));

And if you don't want to specify condition use Thread.sleep();

Upvotes: 0

Sai Ye Yan Naing Aye
Sai Ye Yan Naing Aye

Reputation: 6738

Use Thread.sleep() instead of implicitlyWait(). See sample

driver.get(WEBSTORE_BASE_URL);
assertEquals("Home Page", driver.getTitle().trim());
Thread.sleep(10000); //Thread default time unit is millisecond

Upvotes: 0

Shaun
Shaun

Reputation: 26

Your understanding of what the implicit wait feature of Selenium does is incorrect.
You state that you are using the implicit wait "so that selenium driver waits 10 seconds before doing any further processing".

The implicit wait does not put a delay in processing, but rather sets the amount of time selenium WebDriver will wait AS A MAXIMUM for any operation that implements the wait. The "click()" method does not implement the wait, so when you are executing your code, it will execute line:
assertEquals("Gear", driver.getTitle().trim());
but fails because it still sees the page "Home Page" as the assertion is executed before the page has moved on.

Consider rewriting your code using explicit waits in the following way:-
WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

Where myDynamicElement is a locator on the page you wish to navigate to (wait for).

To make your code even more readable, you should consider creating a class to implement the timers properly using a try-catch block.

To learn more about this you should consider signing up to the free Selenium boot camp at The Selenium Handbook. Waits are covered in day 4.

Upvotes: 1

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

There is a way to make Selenium wait until your DOM is loaded:

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

You can see this example in the docs, here.

In your case, you would wait until the DOM on your new page loaded.

Upvotes: 1

Related Questions