javascript - 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="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/

相关文章:

javascript - 哈希链接禁用 HTML 中的相对链接

javascript - 如何在函数生成的两个文件之间共享变量

php - 每个 Behat 上下文的不同环境 (Symfony 4.x)

python - numpy 测试 assert_array_less 是否与 np.inf 一起正常工作?

c# - 要很好地实现单元测试,需要关注哪些 C# 项目?

javascript - UAT 环境中 url 未定义的奇怪问题。在本地它正在工作 :

javascript - 有条件地在新选项卡中打开网址

node.js - Nodejs/Express API 的简单 Mocha/Chai 测试

unit-testing - 自测试代码与单独测试相比有哪些优点?

objective-c - 如何在 Objective-C Xcode 包中的每个单元测试之前实现 setup() 和teardown()?