如何测试页面上的警告框是否被调用?我可以抓取警报框的文本并对其进行评估吗?
我在 CasperJS 中的点击是这样完成的:
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.');
我该如何检查?
最佳答案
您必须收听remote.alert
event :
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);
});
关于javascript - CasperJS 和警告框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14696463/