Running Jasmine Tests With PhantomJS

Jasmine + PhantomJSI finally got to mess around with PhantomJS and Jasmine, and couldn’t find a resource that gives clear, concise, accurate information, without language specific or build tool specific coupling, so I decided to write it myself.
Fortunately, it is way simpler than I thought.

PhantomJS

PhantomJS is a standalone, cross platform command line application which does not require any specific server environment or programming language support.

Running the Tests

In order to run our jasmine test suite on the command line, all we need is:

Once we got these, all we have to do is run the following command:
phantomjs /path/to/run-jasmine.js http://localhost/js/test/unit/
It doesn’t take more than a few seconds(!) and we are provided with the test results.

This is it.
Now, lets run it with our build tool.

* For the purpose of this article, lets assume PhantomJS is installed on the system, our jasmine runner is located at /path/to/run-jasmine.js and our jasmine test suite is located at http://localhost/js/test/unit/

Hooking with Build Tools

GNU Make

Adding the following target to Makefile and running `make js_unit_test` should do the trick:

js_unit_test:
	@phantomjs /path/to/run-jasmine.js http://localhost/js/test/unit/

Rake (Ruby Make)

Adding the following task to Rakefile and running `rake js_unit_test` should do the trick:

task :js_unit_test do
  exec 'phantomjs /path/to/run-jasmine.js http://localhost/js/test/unit/'
end

Apache Ant

Adding the following target to build.xml running `ant js_unit_test` should do the trick:

<target name="js_unit_test" description="Runs Javascript Unit Tests">
    <exec executable="phantomjs" failonerror="true">
       <arg line="/path/to/run-jasmine.js" />
       <arg line="http://localhost/js/test/unit/" />
    </exec>
</target>

Others

Adding this to any build tool should be as simple as it gets.
The script will exit with 0 code on success, and 1 on failure, which is what most build tools need to know.

Conclusions

Following three simple steps enables us run our unit tests on a real WebKit based browser from the command line – no programming required.
This method is NOT specific to AMD test suites. The URI could have a file:// scheme rather than http://

Lead UI Engineer and a Software Architect.
Over 12 years of professional experience, writing complex web applications, and still learning something new every day.
Currently working for Okta in San Francisco

Twitter LinkedIn Google+ 

11 thoughts on “Running Jasmine Tests With PhantomJS

    1. Uzi Kilon Post author

      Actually I tweaked a little bit the original example so it works with AMD.
      The problem for me was, when the document is ready, jasmine is not yet ready, so I need to add an extra condition in the waitFor script.
      I eventually changed the script to be more jasmine specific
      https://gist.github.com/4540969

      Reply
      1. Colin

        thanks for the great article. this is not even related to the posted code, but is sort of tangentially related for users of dojo. make sure to use dojo/ready instead of window.onload, which means changing the default jasmine specrunner slightly. i found jasmine was using window.onload but i needed to switch it to dojo/ready when using phantomjs. cheers!

        Reply
  1. Dirk Hain

    Great write up, Uzi.
    Did you end up running it on a CI tool like jenkins? If so I’d be interested in the pitfalls you encountered. I am currently headed that way and the goal is to run jasmine + casper on a headless Jenkins using phantomjs + Qt + Xvfb.

    Reply
  2. Edmund

    Hi Uzi,
    I am facing some issues after following what you have share in your article.

    I run the test using the command below:
    phontomjs /lib/run-jasmine.js http://localhost:9000/SpecRunner.html

    I received the following error:
    TypeError: ‘null’ is not an object (evaluating ‘document.body.querySelector(‘.description’).innerText’)
    phantomjs://webpage.evaluate():3
    phantomjs://webpage.evaluate():22
    phantomjs://webpage.evaluate():22

    I am new to Jasmine and phantomjs, could you share with me if you have seen such error before and what’s wrong with my tests?

    Seems that another guy also faces the same problem as me here”
    http://stackoverflow.com/questions/18907081/jscover-with-phantomjs-typeerror-null-is-not-an-object

    Thanks so much!

    Reply
  3. Pingback: Running Jasmine tests with PhantomJS (CI setup part two) | Ramblings

  4. Rick O'Shea

    The run-jasmine.js failed due to the “description” problem. The tweaked version just timed out. Somebody named Scott posted a link to a working version with the following diff, but where did his post/link go? Seems to have vanished.

    return document.querySelector(‘.symbolSummary’) !== null &&
    return document.querySelector(‘.symbol-summary’) !== null &&
    > document.querySelector(‘.symbol-summary .pending’) === null;
    25c25
    #details > .specDetail.failed’);

    > var list = document.querySelectorAll(‘.results > .failures > .spec-detail.failed’);
    32c32
    res = el.querySelector(‘.result-message’).innerText;
    41c41
    .passingAlert.bar’).innerText);

    > console.log(document.que

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *