幻影+摩卡+节点+打开网页

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

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

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

和我的testing文件看起来

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

当运行使用摩卡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(); }); });