Mark of the web

Earlier, I blogged about running JavaScript tests from Visual Studio 2005 where I automate Internet Explorer to load the HTML tests files directly from disk (file:/// protocol). One hurdle in this approach is the Windows XP SP2 local machine zone lockdown which restricts running scripts in local files. (More info here).

The easiest fix to this problem seemed to be the Mark of the web. Fair enough, I went on to add the mark to my JavaScript html test files, like so:

<!-- saved from url=(0019)http://www.foo.com/ -->

Alas, no luck. I still got the annoying message that I had to tick off the warning message each time Internet Explorer started. The trick here is that the line with the code above must en with CR LF (as mentioned here). Yikes. This problem was caused by Visual Studio not using the “Microsoft/Windows/MS-DOS” standard CR LF, but Unix style line delimiters! I opened up the file in Notepad++, selected Format–>Convert to Windows format, and lo it worked like a charm!

JavaScript unit testing in Visual Studio 2005

In my current project, we see an increasing use of JavaScript in our web application. Web 2.0, ya know. Hence, we have a growing need for being able to create automated JavaScript tests. A colleague of mine, Aslak Hellesøy, suggested that we should have a look at script.aculo.us‘s unit test framework.

Fair enough, the script.aculo.us unit test framework seemed to do the trick. We use both prototype and script.aculo.us in our project, so the script.aculo.us unit test framework seemed to fit in nicely. My next concern would be to make it as easy as possible for the developers to run the tests as well as integrating
it into our continuous integration builds (we use TFS). The test results should somehow be made available in the test run report. The steps needed to achieve this would be something like
this:

  1. Write the JavaScript unit tests and place them in an HTML file
  2. From a Visual Studio Team System test method, fire up a browser that loads the HTML file and runs the test
  3. Fetch the result of the JavaScript unit tests and incorporate them into the test report

script.aculo.us provides the pieces for 1 and 2 above out of the box. It also supports POSTing the results back to a web server using Ajax. That could be an approach that would be a part of the solution for point 3 above. However, I felt that this approach is not an optimal solution. First of all because you would have to to
make the tests available thru a web server, the web server would need to receive the results, and store them somewhere. Then, the Visual Studio test would have to fetch the results and incorporate them. It all adds up to seem quite fragile. It is important that a test run has as few external dependencies as possible. Hence, I
wanted a separate approach. The browser should be able to load the JavaScript tests directly from the file system (file:///…). Then, after running the tests we should be able to traverse the DOM of
the HTML page directly to fetch the results.

As a result, we ended up with this approach:

  • Fire up Internet Explorer from .NET via its COM interface (Internet Explorer Object, SHDocVw.dll). You can find code for this here.
  • Load the file directly from the file system (file:///…) and execute the tests
  • Traverse the HMTL DOM to fetch the test results (using Microsoft HTML Object Library, mshtml.dll)
  • Report any error messages back using Assert.fail(“…message…”)