1

I'm trying to do something very simple. Or so I thought.

All I want to do is use phantomjs to open a webpage and assert its title. I'm using mocha-phantomjs to invoke my test runner that looks like:

<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="../../node_modules/mocha/mocha.css" />
  </head>
  <body>
    <div id="mocha"></div>
    <script src="../../node_modules/mocha/mocha.js"></script>
    <script src="../../node_modules/chai/chai.js"></script>
    <script>
      mocha.ui('bdd');
      mocha.reporter('html');
    </script>
    <script src="test.js"></script>
    <script>
      if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
      else { mocha.run(); }
    </script>
  </body>
</html>

and my test file looks

(function() {
  var page, url;

  page = require('webpage');

  page = webpage.create();

  url = "http://localhost:3000";

  page.open(url, function(status) {
    var ua;
    if (status !== "success") {
      return console.log("Unable to access network");
    } else {
      ua = page.evaluate(function() {
        return document.title.should.equal('blah');
      });

      phantom.exit();
    }
  });

  describe('Sanity test', function() {
    return it('true should be true', function() {
      return true.should.equal(true);
    });
  });

}).call(this);

when run using mocha-phantomjs it complains that it doesn't know what require is but i need to require the webpage.

How can I solve this?

Johnno Nolan
  • 29,228
  • 19
  • 111
  • 160
  • You should also load the require.js in there. – Zlatko Mar 11 '14 at 01:07
  • What exactly is `webpage`? Is it a CommonJS (=server-side) module? If yes, you can't do `require('webpage')`, because PhantomJS is headless browser and it does not know how to require a module. – lukaszfiszer Mar 11 '14 at 02:06

1 Answers1

0

You might want to do it with casper.js, it's easier:

casper.test.begin('my test', function suite(test) {
    casper.start("your url", function() {
        test.assertTitle("expected title");
    });
    casper.run(function() {
        test.done();
    });
});
clonq
  • 56
  • 1