Search code examples
twitter-bootstraphtmlunit

conditional resource loading with HtmlUnit (via play's WithBrowser)


I have a web application written in play 2.0 and have automated tests that use WithBrowser for functional testing (http://www.playframework.com/documentation/2.0/ScalaFunctionalTest).

The web app uses a responsive layout through bootstrap, and in order to deal with XSS issues on IE8, we had to add browser-specific code to a page:

<!--[if lt IE 9]>
<script ...></script>
<script ...></script>
<![endif]-->

In one of the scripts from this segment is some code that writes to the local file system:

if ("ActiveXObject in win) {
  AXO = new ActiveXObject("htmlfile")
  AXO.open();
  AXO.write('...');
  AXO.close()
}

I run the automated tests using the default browser, HtmlUnit. When the tests run, HtmlUnit seems to include the scripts and the tests fail with an error. Is there a way to get HtmlUnit to treat the IE-only snippet as a comment, and not execute the scripts in that section?


Solution

  • It is not clear why you say the default browser is HTMLUnit. When you create an instance of a WebClient and use the default browser constructor it gets the browser from BrowserVersion.getDefault() which returns INTERNET_EXPLORER_8, according to the latest HTMLUnit documentation. So it makes sense that the code gets executed.

    As a side note, I'm not so sure HTMLUnit provides such a complete support for ActiveX objects so that might be the cause of issues too.

    You could also test if the conditional tag itself is being taken into account by HTMLUnit by using the FF browser version and checking if that code gets executed too.