2013-02-04 46 views
5

如何測試我的頁面上的警報框被調用?我可以抓取警示框的文字並評估它嗎?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()"]'); 
}); 

的用戶登陸功能檢查,在這種情況下,返回此:

alert('Login has failed.'); 

如何檢查呢?

回答

13

你必須聽remote.alertevent

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

版本1.1 BETA4提供casper.waitForAlert function。有了它,您可以在需要對頁面上的不同警報作出反應時編寫更好的測試。