Friday, April 02, 2010

[updated] JsUnit and Firefox mysterious timeout

Here is the thing. I was trying to get my JsUnit Tests to work using Firefox 3.x and it wont work no matter what.

[UPDATE] (Thank you Kevin, see comments)
Before you start using JsUnit you NEED to keep in mind that ALL inputs must be FULL qualified and not just relative paths.
[UPDATE]

I kept on getting timeout from the Runner. I use the runner locally rather than letting it be o server mode or even on my Apache config. So here is my test environment:

1. Software:
1.1. Firefox 3.6.2 (but the same happens on 3.x)
1.2. JsUnit 2.2
1.3. OS: Fedora 12

2. Run the JsUnit TestRunner locally, like: file:///var/local/jsunit/testRunner.html
3. Inform a local html test page, like: file:///home/gustavo/projects/jsunit-tsts/failingTest.html (which is a copy of $JSUNIT_HOME/tests/failingTest.html, setting the path to jsUnitCore.js correctly)
4. Hit Run

Then I got:

Reading Test Page file:///home/gustavo/projects/jsunit-tsts/failingTest.html timed out.
Make sure that the file exists and is a Test Page.


It does not make any sense, since running the same test from JsUnit it works: file:///var/local/jsunit/tests/failingTest.html

So I Google it and found this page: http://siliconforks.com/jscoverage/faq.html (oddly enough).

The answer is: Firefox has a very strict policy about accessing local files, thus preventing JsUnit TestRunner to run my tests.

Solution:

1. Go to your configuration page: about:config
2. Search for: security.fileuri.strict_origin_policy
3. And set it to FALSE

NOTE: This presents a security problem, so do yourself a favour and get another Firefox profile to run your tests and not to browse.

Now the tricky part: I like organizing and this means that I want to use Test Suites and modularize my tests. But the documentation does not tell you a very dirty story about that: You need full path when running tests like I do (local file URLs) and thus the relative paths do not work properly. Lets go to the example:

The test suite page snippet:

<script type="text/javascript">

function suite() {

var result = new top.jsUnitTestSuite();
var currentURL = location.href;

<!-- extract the current URL path -->
currentURL = currentURL.substr(0, currentURL.lastIndexOf("/", currentURL.length));

<!-- prepend each test page relative URL with the current URL path -->
result.addTestPage(currentURL + "/html_tst/my_tests.html");

return result;
}
</script>


The tree structure:


SOME WHERE IN YOUR DISK:
testSuite.html
html_tst/my_tests.html



If you do not fully qualify the test page path (the currentURL part) you get a time-out when trying to load the test suite with the sub-pages.

Have fun!