Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you emulate the browser's back button in HtmlUnit?

I have not found a clear and obvious suggestion to emulate the browser's 'Back' button in HtmlUnit. Have you done this? If so, how?

The best thing I've come up with is to execute javascript on the current page:

ScriptResult result = currentPage.executeJavaScript("javascript:window.history.back();");
currentPage = (HtmlPage)result.getNewPage();

Is there a suggested method to go back one page in history with htmlunit? What are the implications of going back with the above code? While I haven't convinced myself that HtmlUnit actually does any accurate emulation of different browsers' javascript interpretation, how close do I come to using HtmlUnit's full capacity to emulate a browser's back button using the above code? Is there a way to get a better emulation of a browser's back button than this?

Also, I've noticed the History class in HtmlUnit, but it seems pretty worthless. Thoughts?

like image 931
fooMonster Avatar asked Nov 04 '22 12:11

fooMonster


1 Answers

Probably I'm a bit late, but this might be useful for others too. You can go back in history by accessing the history of the WebWindow like this:

webClient.getWebWindows().get(0).getHistory().back();

There are other useful methods too. You can take a look at the API here.

like image 138
Mosty Mostacho Avatar answered Nov 09 '22 09:11

Mosty Mostacho