I am trying to execute javascript using htmlUnit. After executing the javascript, I want to check if the page had some changes due to javascript execution. i.e I want to compare the html page before and after javascript execution...
Any ideas how I can do it...
Here's the sample code explaining what I actually intent to do...
public static void main(String[] args) {
final WebClient webClient = new WebClient();
HtmlPage page;
try {
page = webClient
.getPage("http://www.somepage.com");
System.out.println(page.asXml());
System.out.println(page.getByXPath("//script"));
BufferedInputStream buffer = null;
// System.out.print("getWebSite " + urlValue + "\n");
URL url = new URL(
"http://www.somepage.com/someJS.js");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
buffer = new BufferedInputStream(urlc.getInputStream());
StringBuilder builder = new StringBuilder();
int byteRead;
while ((byteRead = buffer.read()) != -1)
builder.append((char) byteRead);
buffer.close();
ScriptResult result = page.executeJavaScript(builder.toString());
Object jsResult = result.getJavaScriptResult();
HtmlPage afterExecution = (HtmlPage) result.getNewPage();
System.out.println(afterExecution.asXml());
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}