phantomjs + mocha + node +打开一个网页

时间:2014-03-10 22:27:14

标签: node.js phantomjs mocha mocha-phantomjs

我正在尝试做一些非常简单的事情。或者我想。

我想做的就是使用phantomjs打开一个网页并断言它的标题。 我正在使用mocha-phantomjs来调用我的测试运行器,如下所示:

<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>

我的测试文件看起来

(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);

当使用mocha-phantomjs运行时,它会抱怨它不知道require是什么,但我需要网页。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可能希望使用casper.js,它更容易:

casper.test.begin('my test', function suite(test) {
    casper.start("your url", function() {
        test.assertTitle("expected title");
    });
    casper.run(function() {
        test.done();
    });
});
相关问题