使用mocha-phantomjs来自动化functiontesting

我的项目使用:Node,Coffeescript,SocketIO,Browserify和Mocha。 (用于标准服务器端unit testing的mocha)

我想用一个无头浏览器自动化一些客户端接口testing。 PhantomJS看起来像是理想的select(由于networking套接字的支持而select僵尸)。

PhantomJS页面警告说,这不是一个testing运行者,据我所知,他们build议使用mocha-phantomjs项目来驱动你的testing。

所以我已经能够运行示例testing(例如mocha-phantomjs tests/mixed.html ),但是我目前的问题实际上是在testing中使用PHANTOM。 所有mocha-phantomjs回购的样本testing似乎都使用标准的mocha服务器端unit testing。

例如,我可以轻松运行mocha-phantomjs tests/mixed.html来查看枯燥的旧unit testing。 或者我可以运行phantomjs tests/login.coffee来加载我的login屏幕…但是我怎么phantomjs tests/login.coffee两个结合起来,对我在login屏幕上看到的内容做出断言?

我在网上找不到任何这样的例子,我正在努力理解这个最好的方法。

希望这一切都有道理。 预先感谢您的帮助。

更新 :我发现作者( 这里 )的以下build议,但我真的不明白如何处理它: phantomjs lib/mocha-phantomjs.coffee test/mixed.html

这里有一个相当不错的教程用于testingMocha和Phantom.JS。

关于Mocha和PhantomJS的部分很简短,但基本的想法是将DOM断言和交互放入您的Mochatesting套件,通过testrunner.html文件运行在客户端,然后在testing者处指向mocha-phantomjs。 html文件。

换句话说,你的摩卡testing可能是这样的:

 describe("DOM Test", function () { var el = document.createElement("div"); el.id = "myDiv"; el.innerHTML = "Hello World!"; document.body.appendChild(el); var myEl = document.getElementById('myDiv'); it("has the right text", function () { (myEl.innerHTML).should.equal("Hello World!"); }); }); 

而testrunner.html文件将是正常的设置:

 <html> <head> <title> Tests </title> <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'); var should = chai.should(); </script> <script src="test/test.js"></script> <script> if (window.mochaPhantomJS) { mochaPhantomJS.run(); } else { mocha.run(); } </script> </body> </html> 

如果您更喜欢完全从node.js生态系统运行的解决scheme,那么值得考虑Zombie.JS 。 这个堆栈溢出问题提供了一个基本的例子。

折中的方法是,虽然Zombie.JS只需要使用节点模块就可以使用,而且速度非常快,但它不是一个“真正的”networking浏览器。 PhantomJS更接近于基于webkit的。 另外,mocha-phantomjs的第一种方法将允许您在不同的浏览器中运行客户端的Mochatesting,PhantomJS就是其中之一。