javascript - Implementing waitFor functionality with phantomjs-node -


i have tried , tested - success - phantomjs example waitfor. however, having difficulty implementing via phantomjs-node module because page.evaluate gets evaluated in callback.

phantomjs implementation

page.open("http://twitter.com/#!/sencha", function () {     waitfor(function() {          // here easy evaluate method returns         return page.evaluate(function() {             return $("#signin-dropdown").is(":visible");         });      }, function() {        console.log("the sign-in dialog should visible now.");        phantom.exit();     });   } }); 

however, phantomjs-node evaluate function gets returned data in callback:

page.evaluate(     function(){ /* return thing */ },     function callback(thing) {  /* write code thing */ } ) 

using phantomjs-node, how can run function on page after element visible?

just in case link above dead, here implementation of waitfor function

/** * wait until test condition true or timeout occurs. useful waiting * on server response or ui change (fadein, etc.) occur. * * @param testfx javascript condition evaluates boolean, * can passed in string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or * callback function. * @param onready when testfx condition fulfilled, * can passed in string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or * callback function. * @param timeoutmillis max amount of time wait. if not specified, 3 sec used. */ function waitfor(testfx, onready, timeoutmillis) { var maxtimeoutmillis = timeoutmillis ? timeoutmillis : 3000, //< default max timout 3s     start = new date().gettime(),     condition = false,     interval = setinterval(function() {         if ( (new date().gettime() - start < maxtimeoutmillis) && !condition ) {             // if not time-out yet , condition not yet fulfilled             condition = (typeof(testfx) === "string" ? eval(testfx) : testfx()); //<    defensive code         } else {             if(!condition) {                 // if condition still not fulfilled (timeout condition 'false')                 console.log("'waitfor()' timeout");                 phantom.exit(1);             } else {                 // condition fulfilled (timeout and/or condition 'true')                 console.log("'waitfor()' finished in " + (new date().gettime() - start) + "ms.");                 typeof(onready) === "string" ? eval(onready) : onready(); //< it's supposed once condition fulfilled                 clearinterval(interval); //< stop interval             }         }     }, 250); //< repeat check every 250ms }; 

thanks in advance.

ran problem today, thought i'd share solution.

  // custom helper function   function wait(testfx, onready, maxwait, start) {     var start = start || new date().gettime()     if (new date().gettime() - start < maxwait) {       testfx(function(result) {         if (result) {           onready()         } else {           settimeout(function() {             wait(testfx, onready, maxwait, start)           }, 250)         }       })     } else {       console.error('page timed out')       ph.exit()     }   } 

the first step creating new wait function. takes same parameters original waitfor function, works little differently. instead of using interval, have run wait function recursively, after callback test function testfx has been triggered. also, note don't need pass in value start, gets set automatically.

  wait(function (cb) {     return page.evaluate(function ()        // check if on page (should return true/false)       return     }, cb)   }, function () { // onready function     // code   }, 5000) // maxwait 

in example, i'm setting callback testfx function callback page.evaluate, returns true/false value based on whether or not able find element on page. alternatively, create callback page.evaluate , trigger testfx callback it, shown below:

  wait(function (cb) {     return page.evaluate(function ()        // check if on page (should return true/false)       return     }, function(result) {       var newresult = dosomethingcrazy(result)       cb(newresult)     }   }, function () { // onready function     // code   }, 5000) // maxwait 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -