首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >phantomjs+ mocha +节点+打开网页

phantomjs+ mocha +节点+打开网页
EN

Stack Overflow用户
提问于 2014-03-11 06:27:14
回答 1查看 892关注 0票数 1

我在试着做一些非常简单的事情。或者我是这样想的。

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

代码语言:javascript
复制
<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>

我的测试文件看起来

代码语言:javascript
复制
(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是什么,但我需要需要网页。

我该如何解决这个问题呢?

EN

回答 1

Stack Overflow用户

发布于 2014-03-11 10:33:01

你可能想用casper.js来做这件事,这会更容易:

代码语言:javascript
复制
casper.test.begin('my test', function suite(test) {
    casper.start("your url", function() {
        test.assertTitle("expected title");
    });
    casper.run(function() {
        test.done();
    });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22312695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档