mocha-casperjs多客户端node.js应用程序的无头testing
我有一个运行在Sails.js MVC框架上的node.js应用程序。 该应用程序支持来自多个客户端使用socket.io实时连接。 它支持不同的angular色。 例如,您可以以标准用户身份或主持人身份login。
为了testing,我使用mocha-casperjs(以及chai和其他对我的问题不重要的东西),并使用grunt mocha_phantomjs
从grunt运行testing。 testing在我的项目的根文件夹中的Gruntfile.js文件中指定。 他们是这样指定的:
Gruntfile.js:
mocha_casperjs: { files: { src: [ 'test/single-client.js', 'test/multi-client.js', ] } }
这是我想validation的一个testing案例:一旦主持人点击一个特定的button,一个div.container
应该出现在主持人的视图以及标准用户的视图。
现在,testing单客户端方面效果很好。 这是一个基本的情况:
testing/单client.js:
describe('Clicking on a Button as a Moderator', function() { it('results in div.container to appear on the Moderators view', function() { casper .start('http://localhost:1337') .logInAsMod() //Note: This is pseudocode for submitting a login form .thenClick('button') .then(function() { ('div.container').should.be.inDOM.and.visible; }) }) }
这导致:
Clicking on a Button as a Moderator ✓ results in div.container to appear on the Moderator's view
不过,我在testing这个testing用例的多客户端方面正在努力: div.container
不仅应该出现在主持人的视图上,还应该出现在标准用户的视图上。 据我所知,启动前面定义的testing的casper.run()
方法将被mocha-phantomjs模块调用。 所以像这样的东西不会工作:
testing/多client.js:
describe('Clicking on a Button as a Moderator', function() { it('results in div.container to appear on the Moderators view and on the standard users view', function() { casper .start('http://localhost:1337') .logInAsMod() //Note: This is pseudocode for submitting a login form .thenClick('button') .then(function() { ('div.container').should.be.inDOM.and.visible; }) casper .logInAsUser() //Note: This is pseudocode for submitting a login form .then(function() { ('div.container').should.be.inDOM.and.visible; }) }) }
上面代码的问题是, casper.run()
调用只会创build一个casperjs实例。 如果我可以写这样的话,这将是非常好的:
var casperMod = require('casper').create(); var casperUser = require('casper').create(); casperMod .start('http://localhost:1337') .logInAsMod() //Note: This is pseudocode for submitting a login form casperUser .start('http://localhost:1337') .logInAsUser() //Note: This is pseudocode for submitting a login form casperMod .thenClick('button') .then(function() { ('div.container').should.be.inDOM.and.visible; }) casperUser .then(function() { ('div.container').should.be.inDOM.and.visible; })
所以我会有两个casperjs的实例,我可以编写例程。 但是,这会导致Error: Cannot find module 'casper'
因为mocha-casperjs框架不支持包含另一个casperjs实例。
永久注销并不是一个选项,因为我想testing应用程序的实时方面。
有没有人有使用摩卡casperjs如何实现casperjs的多个实例的build议?
你会在任何框架中遇到问题,因为你需要两套cookie,基本上是两个浏览器运行。 即使你创build了两个casper实例,你仍然在一个共享cookie的phantomjs进程中。
有什么可以做的是创build一个新的WebPage实例,并交换出当前casper实例,加上交换cookies:
casper .start('http://localhost:1337') .logInAsMod() .thenClick('button') .then(function() { ('div.container').should.be.inDOM.and.visible; }) .then(function() { var userPage = require('webpage').create(), modCookies = phantom.cookies phantom.clearCookies() casper .logInAsUser() .then(function() { ('div.container').should.be.inDOM.and.visible; }) })