Bug 1256439 - Run implicit wait callback once before start polling. r?automatedtester draft
authorHenrik Skupin <mail@hskupin.info>
Tue, 15 Mar 2016 14:38:32 +0100
changeset 340523 cecc95d39f2a4c789efabe1b0baf51844b8510dd
parent 340158 86c4213bc6289a6d29277d352814620572b0d194
child 516208 f2e2d06006aa962e44f55edd560fcbe2c14e1f75
push id12990
push userbmo:hskupin@gmail.com
push dateTue, 15 Mar 2016 14:15:14 +0000
reviewersautomatedtester
bugs1256439
milestone48.0a1
Bug 1256439 - Run implicit wait callback once before start polling. r?automatedtester By using a timer the first invocation of the callback is happening after the specified interval. That means by a default of 100ms we loose that time on every call of implicit wait, and it will slow-down the test execution drastically. To prevent this lets manually invoke the callback before starting the timer. MozReview-Commit-ID: Ayy1GPEaY92
testing/marionette/element.js
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -667,17 +667,17 @@ ElementManager.prototype = {
  */
 function implicitlyWaitFor(func, timeout, interval = 100) {
   let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
 
   return new Promise((resolve, reject) => {
     let startTime = new Date().getTime();
     let endTime = startTime + timeout;
 
-    let observer = function() {
+    let onTimer = function() {
       let res;
       try {
         res = func();
       } catch (e) {
         reject(e);
       }
 
       // empty arrays evaluate to true in JS,
@@ -687,17 +687,21 @@ function implicitlyWaitFor(func, timeout
       // allowing |func| to be evaluated at least once
       let col = element.isElementCollection(res);
       if (((col && res.length > 0 ) || (!col && !!res)) ||
           (startTime == endTime || new Date().getTime() >= endTime)) {
         resolve(res);
       }
     };
 
-    timer.init(observer, interval, Ci.nsITimer.TYPE_REPEATING_SLACK);
+    // Run a check immediately so we do not cause a delay in execution
+    // due to the set timer interval.
+    onTimer();
+
+    timer.init(onTimer, interval, Ci.nsITimer.TYPE_REPEATING_SLACK);
 
   // cancel timer and return result for yielding
   }).then(res => {
     timer.cancel();
     return res;
   });
 }