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?

James Kingsbery
  • 7,298
  • 2
  • 38
  • 67
  • i have the same problem even doing: – Ray Tayek Nov 05 '10 at 16:26
  • 1
    doing: final WebClient webClient = new WebClient(); webClient.setJavaScriptEnabled(true); webClient.waitForBackgroundJavaScriptStartingBefore(10000); – Ray Tayek Nov 05 '10 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. – James Kingsbery Feb 02 '16 at 18:59

1 Answers1

1

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

nici
  • 191
  • 4
  • 5
  • 2
    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. – James Kingsbery Jun 08 '10 at 15:12