YUITest.TestCase.prototype = { // ... wait: function (segment, timeout) { var delay = (typeof segment === 'number') ? segment : (typeof timeout === 'number') ? timeout : YUITest.MAX_WAIT; throw new YUITest.Wait((typeof segment === 'function' ? segment : YUITest._waitTimeout), delay); }, /** Delays the current test until _condition_ returns a truthy value. If _condition_ fails to return a truthy value before _timeout_ milliseconds have passed, the test fails. Default _timeout_ is 10s. _condition_ will be executed every _increment_ milliseconds (default 100). @method waitFor @param {Function} condition Function executed to indicate whether to execute _segment_ @param {Function} segment Function to check the success or failure of this test @param {Number} [timeout=10000] Maximum number of milliseconds to wait for _condition_ to return true @param {Number} [increment=100] Milliseconds to wait before checking _condition_ **/ waitFor: function (condition, segment, timeout, increment) { var self = this, endTime; if ((typeof condition !== 'function') || (typeof segment !== 'function')) { self.fail('waitFor() called with invalid parameters. condition and segment must be functions.'); } if (typeof timeout !== 'number') { timeout = YUITest.MAX_WAIT; } endTime = (+new Date()) + timeout; if (typeof increment !== 'number') { increment = 100; } self.wait(function () { if (condition()) { segment(); } else { var now = (+new Date()); if (now > endTime) { YUITest._waitTimeout(); } else { self.waitFor(condition, segment, (endTime - now), increment); } }, increment); }, //... }; /** Calls `YUITest.Assert.fail()` with a message indicating `wait()` was called, but `resume()` was never called. @method _waitTimeout @static @protected **/ YUITest._waitTimeout = function () { YUITest.Assert.fail("Timeout: wait() called but resume() never called."); }; YUITest.MAX_WAIT = 10000;