How do you emulate a browser button in HtmlUnit?

I did not find a clear and obvious suggestion to emulate the browser back button in HtmlUnit. Did you do it? If so, how?

The best I came 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 way to return to a single page in a story using htmlunit? What are the consequences of returning to the above code? Although I have not convinced myself that HtmlUnit really does any exact emulation of the javascript interpretation of different browsers, how close have I come to using the full HtmlUnit feature to emulate the browser back button using the above code? Is there a way to get better emulation of the browser back button than this?

Also, I noticed the History class in HtmlUnit, but it seems completely useless. Thoughts?

+5
source share
1 answer

I may be a little late, but it may be useful for others. You can return to history by referring to WebWindow history as follows:

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

There are other useful methods. You can view the API here .

+2
source

All Articles