2013-02-04 13 views
5

Come si verifica che sia stata richiamata una casella di avviso sulla mia pagina? Posso prendere il testo della casella di avviso e valutarlo?CasperJS e caselle di avviso

mio scatto in CasperJS è fatto in questo modo:

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()"]'); 
}); 

I UserLogin controlli di funzionamento e, in questo caso, restituisce questo:

alert('Login has failed.'); 

Come posso controllare questo?

risposta

13

Dovete ascoltare il 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/); 
}); 

un tentativo di rendere un po 'più sincrono:

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

versione 1.1-beta4 fornisce il casper.waitForAlert function. Con esso è possibile scrivere test più belli quando è necessario reagire a diversi avvisi sulla pagina.