node.js - phantomjs+mocha+node+打开网页

标签 node.js phantomjs mocha.js 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 是什么,但我需要请求该网页。

我该如何解决这个问题?

最佳答案

您可能想使用 casper.js 来完成此操作,它更容易:

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

关于node.js - phantomjs+mocha+node+打开网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22312695/

相关文章:

javascript - 在nodejs中传递数组时从MySql表中删除批量记录

php - 如何从 php 执行 casperjs?

javascript - 使用 PhantomJS 和 node.js 保存和渲染网页

javascript - 如何使用 phantomjs 抓取网站?

node.js - 错误 : ENOENT: no such file or directory, 使用 chai 打开 '../test/testFiles/shadab.jpeg' 文件上传测试用例

node.js - 如何使用 sinon/mocha 模拟 npm 模块

javascript - NodeGit - 解决有利于 "theirs"的 merge 冲突

node.js - Electron js + Axios xhr.js :120 Refused to set unsafe header “user-agent”

node.js - AWS4 S3 POST 签名计算失败示例

javascript - 如何测试 Chai 中函数的回调?