javascript - nightwatchjs - how to wait till ajax call is completed -
i using nightwatchjs browser automation. 1 common usecase seeing is, of content in webpage updated through data ajax call. so, in testing, looking way hold testing until result ajax. not find api in nightwatch or selenium this.
i have tried waitforelementvisible
, feel not suffice. happen if ajax call doesn't return data.
has tried before?
if know ajax path here way how can solved, idea attach 'ajaxcomplete' event client , match executed request path:
client .timeoutsasyncscript(15000) // set async exec timeout .click('.btn-submit') // ajax form submit .executeasync( function(targeturl, done){ var nightwatchajaxcallback = function(ajaxurl) { if(ajaxurl.indexof(targeturl) === 0) { // if url match notify script xhr done done(true); } }; if(!window.nightwatchajaxinited) { // make sure ajaxcomplete attached once window.nightwatchajaxinited = true; $(document).ajaxcomplete(function(e, res, req) { nightwatchajaxcallback(req.url); }); } }, ['/ajaxpath'], // expected xhr request path function(status){ // executes once ajax done client.verify.containstext('.entry', 'lorem ipsup...') // verify post created } );
here command named 'ajaxwait' created code above:
exports.command = function(targeturl, action, callback) { this.timeoutsasyncscript(15000); action(); this.executeasync(function(targeturl, done){ var nightwatchajaxcallback = function(ajaxurl) { if(ajaxurl.indexof(targeturl) === 0) { done(true); } }; if(!window.nightwatchajaxinited) { window.nightwatchajaxinited = true; $(document).ajaxcomplete(function(e, res, req) { nightwatchajaxcallback(req.url); }); } }, [targeturl], function(status){ callback(); }); };
and call should this:
client.ajaxwait('/ajaxpath', function(){ // ajax form submit client.click('.btn-submit') // ajax form submit }, function(){ // executes once ajax done client.verify.containstext('.entry', 'lorem ipsup...') // verify post created })
Comments
Post a Comment