使用CasperJS登录/注销Meteor应用程序

使用CasperJS登录/注销Meteor应用程序,meteor,casperjs,Meteor,Casperjs,我正在尝试使用CasperJS登录Meteor应用程序 下面是我的casper脚本的外观: var casper = require('casper').create({ verbose: true, logLevel: 'debug' }); casper.start('http://localhost:3000/', function() { this.test.assertTitle('app', 'App title is as expected');

我正在尝试使用CasperJS登录Meteor应用程序

下面是我的casper脚本的外观:

var casper = require('casper').create({
    verbose: true,
    logLevel: 'debug'
});

casper.start('http://localhost:3000/', function() {
    this.test.assertTitle('app', 'App title is as expected');
    this.test.assertExists('#login-sign-in-link', 'Sign in link exists');
    this.capture('step-1.png');
    this.click('a#login-sign-in-link');
    this.test.assertExists('#login-email', 'Email field found');
    this.test.assertExists('#login-password', 'Password field found');
    this.capture('step-2.png');
    this.evaluate(function (username, password) {
        document.querySelector('#login-email').value = username;
        document.querySelector('#login-password').value = password;
        }, {
            username: 'a@b.com',
            password: 'testtest'
        });
    this.capture('step-3.png');
    this.click('div#login-buttons-password');
    this.test.assertExists("a#login-name-link","Signed in");
    this.capture('step-4.png');
});

casper.run();
结果如下:

ubuntu:~/tmp/casper$ casperjs test meteor-login.js 
Test file: meteor-login.js                                                      
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: http://localhost:3000/, HTTP GET
[debug] [phantom] Navigation requested: url=http://localhost:3000/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://localhost:3000/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/2 http://localhost:3000/ (HTTP 200)
PASS App title is as expected
PASS Sign in link exists
[debug] [phantom] Capturing page to /home/alexei/tmp/casper/step-1.png
[info] [phantom] Capture saved to /home/alexei/tmp/casper/step-1.png
[debug] [phantom] Mouse event 'mousedown' on selector: a#login-sign-in-link
[debug] [phantom] Mouse event 'mouseup' on selector: a#login-sign-in-link
[debug] [phantom] Mouse event 'click' on selector: a#login-sign-in-link
PASS Email field found
PASS Password field found
[debug] [phantom] Capturing page to /home/alexei/tmp/casper/step-2.png
[info] [phantom] Capture saved to /home/alexei/tmp/casper/step-2.png
[debug] [phantom] Capturing page to /home/alexei/tmp/casper/step-3.png
[info] [phantom] Capture saved to /home/alexei/tmp/casper/step-3.png
[debug] [phantom] Mouse event 'mousedown' on selector: div#login-buttons-password
[debug] [phantom] Mouse event 'mouseup' on selector: div#login-buttons-password
[debug] [phantom] Mouse event 'click' on selector: div#login-buttons-password
FAIL Signed in
#    type: assertExists
#    subject: false
#    selector: "a#login-name-link"
[info] [phantom] Step anonymous 2/2: done in 1061ms.
[info] [phantom] Done 2 steps in 1061ms
ubuntu:~/tmp/casper$ 
请告知如何使用CasperJS测试Meteor的登录/注销


谢谢。

您需要在每次单击操作后定义步骤,最好在加载DOM后等待关键元素在DOM中可用:

var casper=require('casper').create();
卡斯珀,开始http://localhost:3000/,函数(){
this.test.assertTitle('app','app title与预期一致');
this.test.assertExists(“#登录链接”,“登录链接存在”);
这个.capture('step-1.png');
点击('a#登录链接');
});
casper.waitForSelector(“#登录电子邮件”,函数(){
this.test.assertExists(“#登录密码”,“找到密码字段”);
这个.capture('step-2.png');
此.evaluate(函数(用户名、密码){
document.querySelector(“#登录电子邮件”).value=用户名;
document.querySelector(“#登录密码”).value=password;
}, {
用户名:'a@b.com',
密码:“testtest”
});
这个.capture('step-3.png');
点击('div#登录按钮密码');
});
casper.waitForSelector('a#login name link',function()){
此.test.pass(“登录”);
这个.capture('step-4.png');
});
casper.run();

我建议OP投票支持答案:)