10

I would like to use Phantomjs and Jasmine for unit testing javascript. Phantomjs's website recommends that I use a separate thing to handle this, specifically, PhantomJasmine. I downloaded PhantomJasmine and ran the example. Everything went perfectly. I then added the line var system = require('system'); to the top of example_spec.js. The Phantomjs now throws an error when I try to run the example I get "ReferenceError: Can't find variable: require". So, I want to do need things like launch the browser, but it looks like I cannot combine jasmine AND phantomjs syntax. I can only run pure jasmin using Phantomjs. How can I use both in the same file? like:

console.log('Loading a web page');
var page = new WebPage();
var url = "http://www.phantomjs.org/";
page.open(url, function (status) {
    //Page is loaded!
    describe("A suite", function() {
      it("contains spec with an expectation", function() {
        expect(true).toBe(true);
      });
    });
    phantom.exit();
});
Johnno Nolan
  • 29,228
  • 19
  • 111
  • 160
Hohohodown
  • 718
  • 1
  • 8
  • 23

1 Answers1

4

It's not quite how you should be using it. The code to control phantomjs shouldn't be muddied with jasmine specs and vice versa.

You should separate your specs into .js files and create a standard jasmine setup with an HTML spec runner which loads the spec .js files and open that HTML file in phantomjs.

Another solution is to use grunt with phantomjs and jasmine to have automatically run jasmine specs.

https://github.com/cowboy/grunt
https://github.com/creynders/grunt-jasmine-task

Creynders
  • 4,573
  • 20
  • 21
  • I have my files seperated. I get the same error. The inside content of my head tag in the html file is: where execPage.js contains pure phantomjs code, and jaztest1 contains pure jasmine code. I also looked into Grunt. It is just another was to run Jasmine with phantom js. I need to know how to run Jasmine AND Phantomjs scripts with eachother. ie: navigate to page, execute jasmine. – Hohohodown Aug 10 '12 at 17:00
  • I think I'm getting the problem better. Here is the scenario: I have tests.js that is some jasmine tests. I want to navigate to google.com, and check to make sure the search bar shows up. google.com does not have any jasmine includes. How can I do this? – Hohohodown Aug 10 '12 at 17:36
  • Call the jasmine script in the html page, not through PhantomJS. Basically you set it up EXACTLY as you would when you want to run the jasmine tests manually and then just pass the URL of that HTML page to PhantomJS to run it in the command-line. – Creynders Aug 13 '12 at 08:18
  • Thank you! I figured out by doing what you said (but before I read your response). Now I just need to get waitfor() working correctly. – Hohohodown Aug 27 '12 at 20:06
  • I'm not sure I understand the fix. If you have a specrunner html that loads the specs for testing and you want one of the tests to load a google page, how do I get grunt to pass the google URL to jasmine to open? – curmudgeon Jul 19 '13 at 21:41