3

我正在尝试使用 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 进行测试吗?

谢谢你。

4

1 回答 1

5

您需要在每次点击操作后定义步骤,理想情况下等待关键元素在 DOM 加载后可用:

var casper = require('casper').create();

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');
});

casper.waitForSelector('#login-email', function() {
    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');
});

casper.waitForSelector('a#login-name-link', function() {
    this.test.pass('logged in');
    this.capture('step-4.png');
});

casper.run();
于 2013-07-04T20:44:14.763 回答