5

I'm trying to write some automated tests of a website using HtmlUnit. Our web pages load two JavaScripts, one of which then loads several other JavaScripts dynamically into the DOM. These JavaScripts then do some UI initialization. My problem is that HtmlUnit updates the DOM with the new JavaScripts, but does not then seem to run those JavaScripts, because expected DOM elements do not appear.

Here are some things I tried:

  1. Viewing the page with toXml(). The page.toXml() method shows the JavaScripts added, but not the expected UI elements.
  2. Testing for the presence of expected DOM elements. These show up in a real web browser, and I can use jquery and firebug together to confirm that they are there.
  3. Trying to manually start the JavaScripts through the executeJavaScript. When I do this, it can find the method I'm invoking directly, but that method cannot find symbols in other JavaScripts.

Has anyone else ran into this issue?

3
  • i have the same problem even doing: Commented Nov 5, 2010 at 16:26
  • 1
    doing: final WebClient webClient = new WebClient(); webClient.setJavaScriptEnabled(true); webClient.waitForBackgroundJavaScriptStartingBefore(10000); Commented Nov 5, 2010 at 16:26
  • I guess it's worth noting to anyone who comes across this page that I've had a lot of success recently using Karma/Jasmine for unit testing JavaScript instead, and would recommend to anyone still using HtmlUnit that runs into these issues. Commented Feb 2, 2016 at 18:59

1 Answer 1

1

Did you try to insert a short pause after loading the page?

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

1 Comment

I tried both pausing and adding a JavaScript handler that provides a callback for when the page is updated, and neither seemed to work. Anyway, our solution was to not load JavaScript this way and to use Selenium, which doesn't have this issue.

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.