Friday, March 28, 2008

Testing RIAs with WebDriver

If you need to test Rich Internet Applications from Java, take a look at Webdriver.

Things I like about it:

  • It uses real browsers to run the tests, quirks and all
  • It's got a literate testing interface based on Hamcrest and inspired by LiFT.
Here's a very simple example.It's doing simple tests on a static html site:
public class HomePageTest extends HamcrestWebDriverTestCase {

@Override
protected WebDriver createDriver() {
return new HtmlUnitDriver();
}

public void testHomePageHasTitleAndHeading()
throws Exception {
goTo("http://test.intranet");
assertPresenceOf(title()
.with(text(startsWith("Agile"))));
assertPresenceOf(title()
.with(text(containsString("Cocking and Co."))));
assertPresenceOf(heading(1)
.with(text(equalTo("Agile Application Development"))));
}
}



This won't quite work out of the box, as there is no heading finder, but adding one is easy:

public class HeadingFinder extends HtmlTagFinder {
private final int level;

public HeadingFinder(int level) {
this.level = level;
}

@Override
protected String tagDescription() {
return "heading level "+level;
}

@Override
protected String tagName() {
return "h"+level;
}

public static HeadingFinder heading(int level) {
return new HeadingFinder(level);
}
}

It's fairly new, but there are now pre-built binaries available.

1 comment:

Robert Chatley said...

Thanks for showing how easy it is to add a custom finder. Glad you like it. One of the key things we learned from LiFT and Hamcrest was that it's important to allow people to slot in their own concepts so their tests map to their domain, rather than trying to provide a huge generic library. --Robert