我正在尝试使用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();
Run Code Online (Sandbox Code Playgroud)
结果如下:
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$
Run Code Online (Sandbox Code Playgroud)
请问有关如何通过CasperJS登录/退出Meteor的建议吗?
谢谢.
您需要在每次单击操作后定义步骤,理想情况下等待在加载后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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
791 次 |
| 最近记录: |