phantomjs headless javascript unit testing with unit test injection

I have a PHP application that is very javascript heavy and although it has quite a few javascript functions most of them use jquery in some way. This presents a small issue with unit testing where I require a fully loaded Document Object Model (DOM) to interact with. Enter phantomjs a headless webkit that provides the capability to load a webpage, build a full DOM and interact with it. However most examples i came across on the internet seem to assume that the unit tests and qunit are already loaded into the HTML page being called. I didn’t want to mix my testing with my production code in any way and maintaining a seperate HTML page for the unit tests would defeat the point of having the tests, especially with so much integration with elements in the DOM. So i have taken the runner.js example from qunit and modified it to inject qunit, my unit tests and even handle logging into my website to get to the main page to test.

The phantomjs code is available as a gist https://gist.github.com/quixand/5916851

resources I used to understand this included http://stackoverflow.com/questions/9246438/how-to-submit-a-form-using-phantomjs http://stackoverflow.com/questions/9457891/how-to-detect-if-domcontentloaded-was-fired and http://www.kilbasa.com/kilbasacom/dynamically-executing-qunit-tests-against-existing-pages via http://forum.jquery.com/topic/injecting-qunit-tests-into-an-existing-page