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!

5 comments:

Kevin Galligan said...

Did this. Still doesn't work. I'm running Ubuntu, although I assume that would make zero difference. Tried chrome, but it doesn't show the browse button.

Highly frustrating.

Kevin Galligan said...

As per...

http://digitalmihailo.blogspot.com/2008/06/make-jsunit-work-in-firefox-30.html

If you give the full link as an argument, it works.

file:///home/kevin/devel_remote/myproject/testRunner.html?testpage=/home/kevin/devel_remote/myproject/someTestPage.html

Gustavo said...

Hi Kevin,

Thanks for pointing out that this is a very relevant point. I just forgot to make it explicit clear.

Thanks Kevin and I hope you are enjoying JsUnit. If you need any help, please feel free to just let your question pop here.

Best Regards,

Gustavo

PlNG said...

If you use relative paths with JsUnit in FireFox, FF will only accept from where testRunner is running and deeper. Rather annoying. For Chrome you have to start it with a switch to disable some securities.

Fully qualified URL's lack mobility and it seems to be a strange way to implement security.

Gustavo said...

Hi PING,

I actually said you need to relax browser security to get it running. In this way what I meant was that you are indeed less secure.

Anyway, I probably wrote that in a confusing way. But absolute paths has nothing to do with security but with getting things working. And the changes I need to make to get JsUnit properly running are reducing the browser security, not improving it.

Best regards and thanks for the comment, I appreciate it.