Explicit Wait with waitUntilPresent

2,290 views
Skip to first unread message

Harry King

unread,
May 27, 2015, 7:31:11 AM5/27/15
to thucydid...@googlegroups.com
I'm trying to use explicit wait to wait for an element to be present in the DOM.  In this example, I have a login form which when the submit button is pressed, displays a Loading div, then once login is validated, the user is redirected to the logged in area.  This process takes up to 10 seconds.  To verify I've reached the logged in area, I'm using waitUntilPresent on an element:
navigation.internalTab().waitUntilPresent();

In my serenity.properties file I have the following settings:
webdriver.wait.for.timeout=10000
webdriver.timeouts.implicitlywait=0

This should disable implicit wait and only use explicit wait, as one of the Selenium devs has suggested avoiding mixing both implicit and explicit waits.

However, it looks like waitUntilPresent relies on the implicit wait parameter.  With the above settings, my test fails after the first poll (typically at around 100 milliseconds). Is there to force it to use the explicit wait parameter instead?  I know I could increase the implicit wait parameter and reset it later, but my concern is that if this test failed before resetting the parameter, the rest of the tests would be subject to the implicit wait, which at ten seconds massively slows down my test suite.

If I was writing the same operation in plain Selenium, I'd use the following:
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.get(url);
driver.findElement(By.cssSelector("#email")).sendKeys(email);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("input.submit[value='Log in']")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".mjHome")));

While I could build a WebDriverWait using getDriver(), I'd need to either repeat the selector (something I'd rather avoid), or refer to the selector from the Page Object, as WebDriverWait doesn't appear to work with WebElementFacade, and I am not aware of an equivalent in Serentiy.

Any thoughts?

John Smart

unread,
May 27, 2015, 9:06:51 AM5/27/15
to thucydid...@googlegroups.com, hazme...@gmail.com
You can use the following line to implement your wait:

waitFor(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".mjHome")));

or

withTimeoutOf(10,TimeUnit.SECONDS).waitFor(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".mjHome")));

Harry King

unread,
May 27, 2015, 11:04:04 AM5/27/15
to thucydid...@googlegroups.com, hazme...@gmail.com
Thanks John,

I opted for your first suggestion and stored the Expected Condition in the page object, which gets me:
loginPage.waitFor(mJNavigation.toBePresentInDOM());

Many thanks for the help!

Harry
Reply all
Reply to author
Forward
0 new messages