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