javascriptjavahtmlunit

How to use javascript DOM implementation from HtmlUnit


The problem is that I need to extract javascript DOM implementation like window and document variables. I need to do it because DOM is huge and I need only some of the variables to evaluate my code. I use Java RhinoEngine to evaluate the code but it doesn't have browser environment variables, so I want to create a minimal variable environment for evaluating scripts with the same variables (variables don't change and the code changes)

I've checked the HtmlUnit code but wasn't able to find out how to copy DOM implementation from it properly.


Solution

  • You can execute javascript in the context of the page/window and get back the value - maybe that is what you are asking for?

    Simple sample to get a string from window

    String script = "window.name;";
    String result = (String) page.executeJavaScript(script).getJavaScriptResult();
    

    The script can be everything you can place in a script tag e.g. including function definitions.