views:

363

answers:

3

I'd like to unit test (jUnit 4) the behavior of my pages when the browser's back button (or forward or refresh) is clicked.

Can I somehow simulate the browser's back/forward/refresh buttons in a unit test? Is there a utility class that provides such functionality?

2nd Edit:

I understand that the Wicket test facilities don't simulate a browser with a full history. From my understanding I would need the following two things to simulate a browser's behavior from a unit test:

(1) Wicket has to tell me what exact request (e.g. URL) is made when I call WicketTester.startPage() or WicketTester.clickLink().

(2) Wicket has to process the same request again, e.g. by accepting the URL previously recorded by (1).

I want to do this in a way that is compatible with WicketTester, FormTester and so on as I'm using the component finders, asserts, and more nice functionality in these classes. That means that I have to issue the requests from Wicket facilities and not from external clients like HttpUnit / HtmlUnit / Selenium.

+1  A: 

This is pretty certainly not supported by WicketTester, which explicitly uses a dummy WebApplication not supporting the back button to save resources.

I suspect it would be painful to simulate in Wicket...

Your best bet might be to use a browser-based testing tool like Selenium RC. I haven't tried it myself, but it does have a goBack() method that will simulate a click on the browser back button.

Don Roby
+1  A: 

Check out HtmlUnit, you can imitate back and forward events using the History class.

@Test
public void testHistory() throws IOException {
    // Create a web client
    final WebClient webClient = new WebClient();

    // Surf to a page
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net/");

    // Click "Get started" link
    page.getAnchorByHref("gettingStarted.html").click();

    // Get History
    History history = webClient.getCurrentWindow().getHistory();

    // Current page
    assertEquals("http://htmlunit.sourceforge.net/gettingStarted.html",
                 history.getUrl(history.getIndex()).toString());

    // Go back one page
    history.back();
    assertEquals("http://htmlunit.sourceforge.net/",
                 history.getUrl(history.getIndex()).toString());
}
antonj
A: 

You cannot simulate "back" functionality in wicket unit tests, it is completely out of the scope of what wicket does, you can however test almost anything that will happen when you click back

post something more specific about what you are trying to test, generally a back button will just give you a bunch of "detached" components in wicket, and you can test those

walnutmon
I guess you're right, it's not possible to do it with `WicketTester`. I tried to use the `WebRequestCycle` to do the trick but I couldn't make it. For now, I'll give up because I don't have more time for that. Nevertheless, what do you mean with "you can however test almost anything that will happen when you click back"?
Wolfgang
If you use the detach, and attach methods on components, you can simulate certain back button behaviors. Basically, when you navigate away from a wicket page it detaches the components and their models to save space, this will cause exceptions to be thrown if you act on those models objects unless they know how to reattach themselves.
walnutmon