Skip to main content

Jun 3, 2011 - 2 minute read - Selenium Simplified Blog WebDriver

Selenium 2.0rc2 - Hey, where's my unashamedly ugly wait

Warning, this blog post contains Java code. Some testers may want to look away now.

I was quite happy using the unashamedly ugly Selenium Wait class, for adhoc waits that I did not want to refactor into proper classes.


        new Wait(“JS Page title did not change”){

            public boolean until() {
                    return driver.getTitle().startsWith(TITLE_OF_PAGE_STARTS_WITH_THIS);
                }catch(Exception e){
                    // ignore not found exception
                return false;

I upgrade to Selenium 2.0rc2 and what happens?


“Pah,” thinks me, “no-one’s gonna stop me writing pug ugly waits”:

new WebDriverWait(driver, 10){
    }.until(new ExpectedCondition(){

        public Boolean apply(WebDriver driver) {
            return driver.getTitle().startsWith(TITLE_OF_PAGE_STARTS_WITH_THIS);

There you go. Still Ugly.

But Wait…

  • now we only have one wait construct to deal with,
  • this makes it easier to refactor the expected conditions out

Refactor it out?

Yup, we don’t let it stay ugly, particularly not if we use the same wait somewhere else.

We might create an ExpectedConditionFactory…


ExpectedConditionFactory weCanSee = new ExpectedConditionFactory();

Which looks a bit like…

public class ExpectedConditionFactory {

    public  ExpectedCondition pageTitleStartsWith(
            String titleOfPageStartswith) {
        return new PageTitleStartsWithExpectedCondtion(titleOfPageStartswith);


So the calling code looks more like…

new WebDriverWait(driver, 10){

Then create a new class that implements the ExpectedCondition…

public class PageTitleStartsWithExpectedCondtion implements
        ExpectedCondition {

    private String titleOfPageStartsWith;

    public PageTitleStartsWithExpectedCondtion(String titleOfPageStartsWith) {
        this.titleOfPageStartsWith = titleOfPageStartsWith;

    public Boolean apply(WebDriver driver) {
        return driver.getTitle().startsWith(titleOfPageStartsWith);

And I might even revisit the original “new WebDriverWait…” code and have:

WebDriverWait wait = new WebDriverWait(driver,10);


And then I can  tuck the declaration of the wait up in the Page Object constructor so I just see the tidy wait.until part in my Page Object methods

Yes, sadly the end result does not look ugly at all. Some people might almost call it readable.


But it does require more code, so you boost your “lines of test code written per day” metric.


Feel free to constructively mock my code in the comments - so as I can learn how to code more good like.

- in Selenium Simplified Blog WebDriver

You will need a Github account to comment. Or you can contact me with your comment.

I reserve the right to delete spam comments e.g. if your comment adds no value and its purpose is simply to create a backlink to another site offering training, or courses, or etc.