2

I have run into a problem while trying to test a web application with HtmlUnit. Please bear in mind that I haven't used HtmlUnit very long so I might be missing something trivial.

For some reason externally referenced javascripts aren't being loaded. When I print the page as xml all i get is this:

<script language="javascript" src="urltofile.js">  
</script>  

As you can see the script tag is empty when it should contain the javascript. What am I missing? I have googled for a solution all morning but haven't found any relevant information. Any help would be greatly appreciated!

nici
  • 21
  • 1
  • 2

2 Answers2

3

After many many hours of googling I found the solution to be very simple. All I had to do was to wait a few seconds after the page is retrieved to allow the javascript some time to execute before doing anything else. I can honestly say finding the solution was a bit of an anticlimax, but at least I can move along to more important stuff now. :)

nici
  • 191
  • 4
  • 5
  • You generally want to do stuff on document ready or window onload, just to be sure. Also I think it is generally considered good hygene to include type="text/javascript" to a script tag. – Gabriel May 18 '10 at 06:44
  • 1
    I had encountered the same problem in htmlunit 2.8 and also found that these webClient functions didn't help (though the API documents that these two functions are experimental and not fully functional): webClient.waitForBackgroundJavaScript() or webClient.waitForBackgroundJavaScriptStartingBefore(). – DannyTree Jul 24 '11 at 09:45
1

That tag loads an external file: "urltofile.js", so it's not strange to have nothing into because the js code is loaded from that file and not injected into the existing markup.

mamoo
  • 8,156
  • 2
  • 28
  • 38
  • Ah, I see. Any shot-in-the-dark ideas on how I can inject the script into the existing code? – nici May 17 '10 at 11:24