From bcbe89fbdcbe3b736e3ef8887e6a1099e61e642b Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 10 Mar 2014 12:24:01 +0100 Subject: [PATCH] [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 --- openerp/tests/phantomtest.js | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/openerp/tests/phantomtest.js b/openerp/tests/phantomtest.js index 1cdfeabce76..bfd6fa4d0b2 100644 --- a/openerp/tests/phantomtest.js +++ b/openerp/tests/phantomtest.js @@ -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