[IMP] phantomjs waitloop

* simplify waitFor itself
* log a "waiting for" message if the expression evaluates but is *false*

bzr revid: xmo@openerp.com-20140310112401-khvy0zcvwaaxa5om
This commit is contained in:
Xavier Morel 2014-03-10 12:24:01 +01:00
parent b3de4ca481
commit bcbe89fbdc
1 changed files with 12 additions and 18 deletions

View File

@ -2,22 +2,17 @@
function waitFor (ready, callback, timeout, timeoutMessageCallback) {
timeout = timeout || 10000;
var start = new Date().getTime();
var condition = ready();
var interval = setInterval(function() {
if ((new Date().getTime() - start < timeout) && !condition ) {
condition = ready();
var start = new Date;
(function waitLoop() {
if(new Date - start > timeout) {
error(timeoutMessageCallback ? timeoutMessageCallback() : "Timeout after "+timeout+" ms");
} else if (ready()) {
callback();
} else {
if(!condition) {
var message = timeoutMessageCallback ? timeoutMessageCallback() : "Timeout after "+timeout+" ms";
console.log("Waiting for " + ready);
error(message);
} else {
clearInterval(interval);
callback();
}
setTimeout(waitLoop, 250);
}
}, 250);
}());
}
function error(message) {
@ -118,13 +113,12 @@ function PhantomTest() {
console.log('loaded', url, status);
// process ready
waitFor(function() {
return self.page.evaluate(function (ready) {
return self.page.evaluate(function (ready) {
console.log("waiting for", ready);
var r = false;
try {
r = !!eval(ready);
} catch(ex) {
console.log("waiting for " + ready);
}
} catch(ex) { }
return r;
}, ready);
// run test