Javascript CasperJS和警报框
如何测试页面上的警报框是否被调用?我可以获取警报框的文本并对其进行评估吗 我在CasperJS中的单击是这样完成的:Javascript CasperJS和警报框,javascript,testing,tdd,phantomjs,casperjs,Javascript,Testing,Tdd,Phantomjs,Casperjs,如何测试页面上的警报框是否被调用?我可以获取警报框的文本并对其进行评估吗 我在CasperJS中的单击是这样完成的: casper.waitForSelector('a[href="javascript:UserLogin()"]', function success() { this.test.comment("Submiting the bad login info"); this.test.assertExists('a[href="javascri
casper.waitForSelector('a[href="javascript:UserLogin()"]',
function success() {
this.test.comment("Submiting the bad login info");
this.test.assertExists('a[href="javascript:UserLogin()"]');
this.click("a#h_login");
},
function fail() {
this.test.assertExists('a[href="javascript:UserLogin()"]');
});
UserLogin函数检查并在本例中返回以下内容:
alert('Login has failed.');
如何检查此项?您必须收听
遥控器。警报
:
尝试使其更加同步:
版本1.1-beta4提供了。有了它,当您需要对页面上的不同警报做出反应时,您可以编写更好的测试
casper.on('remote.alert', function(message) {
this.echo('alert message: ' + message);
// or if you want to test it
this.test.assertMatch(message, /Login has failed/);
});
function testAlert(message) {
this.test.assertMatch(message, /Login has failed/);
}
casper.then(function() {
// temporarily registering listener
this.on('remote.alert', testAlert);
});
casper.waitForSelector('#login', function success() {
this.test.pass('selector was found');
this.click("#login");
}, function fail() {
this.test.fail('selector was found');
});
casper.then(function() {
this.removeListener('remote.alert', testAlert);
});