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.

e.g.

        new Wait("JS Page title did not change"){

            @Override
            public boolean until() {
                try{
                    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?

Gone!

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

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

        @Override
        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…

e.g.

ExpectedConditionFactory weCanSee = new ExpectedConditionFactory();

Which looks a bit like…

public class ExpectedConditionFactory {

    public  ExpectedCondition<Boolean> pageTitleStartsWith(
            String titleOfPageStartswith) {
        return new PageTitleStartsWithExpectedCondtion(titleOfPageStartswith);
    }

}

So the calling code looks more like…

new WebDriverWait(driver, 10){
    }.until(weCanSee.pageTitleStartsWith(TITLE_OF_PAGE_STARTS_WITH_THIS));

Then create a new class that implements the ExpectedCondition…

public class PageTitleStartsWithExpectedCondtion implements
        ExpectedCondition<Boolean> {

    private String titleOfPageStartsWith;

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

    @Override
    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);

wait.until(weCanSee.pageTitleStartsWith(TITLE_OF_PAGE_STARTS_WITH_THIS));

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.

wait.until(weCanSee.pageTitleStartsWith(TITLE_OF_PAGE_STARTS_WITH_THIS));

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

Bonus.

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

This entry was posted in Selenium Simplified Blog, WebDriver. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *