I am currently working on creating functional tests using htmlunit.
On the webpage I am testing, there is an anchor which opens a page in a new tab when clicked. This anchor needs to be clicked to reveal additional check boxes and buttons.
If I wanted to save the page after clicking the anchor it would look something like this:
HtmlAnchor terms = tb.getFirstByXPath("//*[@id=\"terms_link\"]");
tb = terms.click();
However, the above code saves the webpage which is opened in the new tab as opposed to the web page in which the anchor was clicked.
Is there any way for me to save the page in which the anchor was clicked, or possibly save both html pages?
So after a bit of messing around I discovered that any actions which change the page are registered in the browser window regardless of whether or not you save them after an action.
In order to save the popup which occurs after clicking the an action just assign the resulting page to an HtmlPage object.
HtmlPage page = button.click
You can fetch the previous window and save it's modified contents with the following code. If you want to save the popup and the original page just save them to different object names.
page2 =(HtmlPage)(webClient.getWebWindows().get(0).getEnclosedPage());