2

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();
    }

}
1
  • Is htmlUnit the problem or comparison? Commented Nov 25, 2011 at 11:11

1 Answer 1

1

"Executing" the Javascript source won't do anything. I expect you get very little back from result.getNewPage(). Try changing the example to hit a real site, and explain what result you expect to see, then we can try executing your example.

That said, one thing that might help you is to think of HtmlUnit as a browser you control via Java. You don't "run" the Javascript in a page, HtmlUnit runs it. You pretend you are a human user clicking on things, but you do the "clicking" via Java code.

In your example, you would navigate the DOM in your page to find something that you use to trigger the Javascript -- perhaps clicking a button or an image. The result of calling click() will give you a new page resulting from whatever the Javascript did.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Rodney, Thanks for the reply. Yes, I understand that htmlUnit is a headless java browser and I have to call click() events and other events to execute the javascript. What I basically want to do is: websites generally have lot of javascripts included in them using <script> tag. I want to know if this javascripts are actually used by the page or not. So given a page I want to check if it uses all the javascripts it includes. Can this be done with htmlUnit? Am I using the right tool? Kindly suggest me if there are better ways to do it. Thanks, Irfan
Like any browser, HtmlUnit will execute any <script> content it finds, and will then wait for user interaction to trigger the Javascript. I don't know that HtmlUnit itself will let you programmatically test if some particular bit of code is triggered (I seem to recall doing exactly that via the debugger, thought it was pretty ugly), but there might be some hooks in the JS library HtmlUnit uses. Probably not though. Maybe the Firebug JS debugger would help you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.