Skip to content

WicketSeleniumTester

ketola edited this page Mar 18, 2013 · 11 revisions

WicketSeleniumTester is testing tool for Wicket framework.

Wicket itself comes with a testing tool called WicketTester. With it, you can emulate user interaction on your application and execute your server side code as it would run when used through a browser. The problem with WicketTester is that it only executes your Java code and if your application uses features that rely on ajax calls and javascript (like ModalWindow), testing can become hard - and because the javascript doesn't get executed you cannot completely rely on the WicketTester tests.

WicketSeleniumTester starts a Jetty server inside (no web.xml needed) and you can use Selenium WebDriver java api to run tests on the rendered markup and you also have access to the Java objects so that you can make assertions on those as well. WicketSeleniumTester tries to be as similar to WicketTester as possible so that it could be easilly used as replacement when necessary.

Here's an example of a test with WicketSeleniumTester:

@Test
public void successfulFormSubmissionLeadsToConfirmPage()
{
    WicketSeleniumTester tester = new WicketSeleniumTester( new WicketApplication() );

    WebDriver d = tester.startPage( FormPage.class );

    // check the rendered page is correct
    assertEquals( FormPage.class, tester.getLastRenderedPage().getClass() );

    // fill the form and submit with Selenium api
    d.findElement( By.name( "name" ) ).sendKeys( "John Smith" );
    d.findElement( By.name( "address" ) ).sendKeys( "Sunset Boulevard" );
    d.findElement( By.name( "zip" ) ).sendKeys( "000111" );
    d.findElement( By.name( "city" ) ).sendKeys( "Oslo" );
    d.findElement( By.name( "country" ) ).sendKeys( "Vietnam" );
    d.findElement( By.cssSelector( "input[type='submit']" ) ).click();

    // response page should be ConfirmationPage - check both Class and the rendered markup
    assertEquals( ConfirmPage.class, tester.getLastRenderedPage().getClass() );
    assertEquals( "Confirm Page", d.getTitle() );

    // Check values on the page
    assertEquals( "John Smith", d.findElement( By.cssSelector( "span[wicket\\:id='name']" ) ).getText() );
    assertEquals( "Sunset Boulevard", d.findElement( By.cssSelector( "span[wicket\\:id='address']" ) ).getText() );
    assertEquals( "000111", d.findElement( By.cssSelector( "span[wicket\\:id='zip']" ) ).getText() );
    assertEquals( "Oslo", d.findElement( By.cssSelector( "span[wicket\\:id='city']" ) ).getText() );
    assertEquals( "Vietnam", d.findElement( By.cssSelector( "span[wicket\\:id='country']" ) ).getText() );

    // It's also possible to check values in the model object
    Person person = (Person) tester.getLastRenderedPage().getDefaultModelObject();

    assertEquals( "John Smith", person.getName() );
    assertEquals( "Sunset Boulevard", person.getAddress() );
    assertEquals( "000111", person.getZip() );
    assertEquals( "Oslo", person.getCity() );
    assertEquals( "Vietnam", person.getCountry() );

}

You can find more examples in the wicket-selenium-tester-examples module.

The development of WicketSeleniumTester is still in an early stage, the examples only cover simple scenarios but I'm pretty confident that this could become something useful in real application development if I have time and energy to work on this more.

I guess with this approach it could also be possible to have a continous integration server run the WicketSeleniumTester tests with different browsers.. that would be pretty cool!

Clone this wiki locally