2014-05-12 60 views
2

我有一个运行在Sails.js MVC框架上的node.js应用程序。该应用程序支持来自多个客户端的使用socket.io的实时连接。它支持不同的角色。例如,您可以作为标准用户或主持人登录。mocha-casperjs多客户端node.js的无头测试应用程序

为了测试,我使用mocha-casperjs(以及chai和其他对我的问题不重要的东西),并使用grunt mocha_phantomjs从grunt运行测试。测试在我的项目的根文件夹中的Gruntfile.js文件中指定。他们规定是这样的:

Gruntfile.js:

mocha_casperjs: { 
    files: { 
    src: [ 
     'test/single-client.js', 
     'test/multi-client.js', 
    ] 
    } 
} 

下面是测试情况下,我想验证:一旦主持人点击一个特定的按钮,div.container应该同时出现在主持人的视图以及标准用户的观点。

现在,测试单客户端方面效果很好。下面是一个基本的场景:

测试/单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 

不过,我挣扎在测试的多客户端方面这个测试用例:div.container不应该只出现在主持人的视图上,也应该出现在标准用户的视图上。据我所知,启动前面在源代码中定义的测试的casper.run()方法将由mocha-phantomjs模块调用。因此,像这样将无法工作:

测试/多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()通话只创建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实例。

由于我想测试应用程序的实时方面,所以永久注销并不是一个选项。

有没有人有建议如何在使用摩卡casperjs时实现casperjs的多个实例?

+0

这是奇怪的,因为它看起来并不像摩卡casperjs内部使用casperjs测试命令/环境,其中只允许一个实例。 –

+0

你能指定你的意思吗?因为在我的测试程序中,我不会自己调用casper.run(),所以我认为mocha-casperjs模块会这样做,这使得难以“注入”其中run()将会是casper的第二个实例调用。 – gdlm

+0

我搜索了mocha-casperjs的源代码,但没有找到任何证据证明使用了'casperjs test'。所以你应该可以使用第二个casperjs实例。第一个由mocha-casperjs注入,第二个由你创建。 'casper.test'对象在两个实例之间可能会有所不同。然后,您可能需要通过手动检查外部接口而不使用2.实例中的'casper.test'来获取您需要的(assert-)信息。然后你可以在1.实例上调用'test.pass'或'test.fail'。 –

回答

2

在任何框架中,您都会遇到问题,因为您需要两组Cookie,基本上是两台运行的浏览器。即使您创建了两个casper实例,您仍然处于共享cookie的phantomjs进程中。

有什么可以做的就是创建一个新网页实例,并交换了这一点在当前卡斯帕实例,以及交换饼干:

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; 
     }) 
    }) 
相关问题