Commit 9bad6047 9bad6047bc732ff7af9b94c06262dd946e2ff46b by Nathan Black

Add a 'details' property bag to waitFor.timeout events

1 parent 3b8f3e1b
...@@ -483,9 +483,11 @@ Emitted when a ``Casper.wait()`` operation starts. ...@@ -483,9 +483,11 @@ Emitted when a ``Casper.wait()`` operation starts.
483 ``waitFor.timeout`` 483 ``waitFor.timeout``
484 ~~~~~~~~~~~~~~~~~~~ 484 ~~~~~~~~~~~~~~~~~~~
485 485
486 **Arguments:** ``None`` 486 **Arguments:** ``[timeout, details]``
487
488 Emitted when the execution time of a ``Casper.wait*()`` operation has exceeded the value of ``timeout``.
487 489
488 Emitted when the execution time of a ``Casper.wait*()`` operation has exceeded the value of ``Casper.options.stepTimeout``. 490 ``deatils`` is a property bag describing what was being waited on. For example, if ``waitForSelector`` timed out, ``details`` will have a ``selector`` string property that was the selector that did not show up in time.
489 491
490 492
491 .. index:: filters 493 .. index:: filters
......
...@@ -1943,7 +1943,7 @@ You can also write the same thing like this:: ...@@ -1943,7 +1943,7 @@ You can also write the same thing like this::
1943 ``waitFor()`` 1943 ``waitFor()``
1944 ------------------------------------------------------------------------------- 1944 -------------------------------------------------------------------------------
1945 1945
1946 **Signature:** ``waitFor(Function testFx[, Function then, Function onTimeout, Number timeout])`` 1946 **Signature:** ``waitFor(Function testFx[, Function then, Function onTimeout, Number timeout, Object details])``
1947 1947
1948 Waits until a function returns true to process any next step. 1948 Waits until a function returns true to process any next step.
1949 1949
...@@ -1977,6 +1977,9 @@ Example using the ``onTimeout`` callback:: ...@@ -1977,6 +1977,9 @@ Example using the ``onTimeout`` callback::
1977 1977
1978 casper.run(); 1978 casper.run();
1979 1979
1980 ``details`` is a property bag of various information that will be passed to the ``waitFor.timeout`` event, if it is emitted.
1981 This can be used for better error messages or to conditionally ignore some timeout events.
1982
1980 .. _casper_waitforpopup: 1983 .. _casper_waitforpopup:
1981 1984
1982 .. index:: Popups, New window, window.open, Tabs 1985 .. index:: Popups, New window, window.open, Tabs
......
...@@ -2003,9 +2003,10 @@ Casper.prototype.waitDone = function waitDone() { ...@@ -2003,9 +2003,10 @@ Casper.prototype.waitDone = function waitDone() {
2003 * @param Function then The next step to perform (optional) 2003 * @param Function then The next step to perform (optional)
2004 * @param Function onTimeout A callback function to call on timeout (optional) 2004 * @param Function onTimeout A callback function to call on timeout (optional)
2005 * @param Number timeout The max amount of time to wait, in milliseconds (optional) 2005 * @param Number timeout The max amount of time to wait, in milliseconds (optional)
2006 * @param Object details A property bag of information about the condition being waited on (optional)
2006 * @return Casper 2007 * @return Casper
2007 */ 2008 */
2008 Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) { 2009 Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout, details) {
2009 "use strict"; 2010 "use strict";
2010 this.checkStarted(); 2011 this.checkStarted();
2011 timeout = timeout ? timeout : this.options.waitTimeout; 2012 timeout = timeout ? timeout : this.options.waitTimeout;
...@@ -2019,7 +2020,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) { ...@@ -2019,7 +2020,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
2019 this.waitStart(); 2020 this.waitStart();
2020 var start = new Date().getTime(); 2021 var start = new Date().getTime();
2021 var condition = false; 2022 var condition = false;
2022 var interval = setInterval(function _check(self, testFx, timeout, onTimeout) { 2023 var interval = setInterval(function _check(self) {
2023 /*jshint maxstatements:20*/ 2024 /*jshint maxstatements:20*/
2024 if ((new Date().getTime() - start < timeout) && !condition) { 2025 if ((new Date().getTime() - start < timeout) && !condition) {
2025 condition = testFx.call(self, self); 2026 condition = testFx.call(self, self);
...@@ -2029,7 +2030,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) { ...@@ -2029,7 +2030,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
2029 if (!condition) { 2030 if (!condition) {
2030 self.log("Casper.waitFor() timeout", "warning"); 2031 self.log("Casper.waitFor() timeout", "warning");
2031 var onWaitTimeout = onTimeout ? onTimeout : self.options.onWaitTimeout; 2032 var onWaitTimeout = onTimeout ? onTimeout : self.options.onWaitTimeout;
2032 self.emit('waitFor.timeout', timeout, onWaitTimeout); 2033 self.emit('waitFor.timeout', timeout, details || { testFx: testFx });
2033 clearInterval(interval); // refs #383 2034 clearInterval(interval); // refs #383
2034 if (!utils.isFunction(onWaitTimeout)) { 2035 if (!utils.isFunction(onWaitTimeout)) {
2035 throw new CasperError('Invalid timeout function'); 2036 throw new CasperError('Invalid timeout function');
...@@ -2047,7 +2048,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) { ...@@ -2047,7 +2048,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
2047 if (then) { 2048 if (then) {
2048 self.then(then); 2049 self.then(then);
2049 } 2050 }
2050 }, this.options.retryTimeout, this, testFx, timeout, onTimeout); 2051 }, this.options.retryTimeout, this);
2051 this.waiters.push(interval); 2052 this.waiters.push(interval);
2052 }); 2053 });
2053 }; 2054 };
...@@ -2071,7 +2072,7 @@ Casper.prototype.waitForPopup = function waitForPopup(urlPattern, then, onTimeou ...@@ -2071,7 +2072,7 @@ Casper.prototype.waitForPopup = function waitForPopup(urlPattern, then, onTimeou
2071 } catch (e) { 2072 } catch (e) {
2072 return false; 2073 return false;
2073 } 2074 }
2074 }, then, onTimeout, timeout); 2075 }, then, onTimeout, timeout, { popup: urlPattern });
2075 }; 2076 };
2076 2077
2077 /** 2078 /**
...@@ -2090,7 +2091,7 @@ Casper.prototype.waitForResource = function waitForResource(test, then, onTimeou ...@@ -2090,7 +2091,7 @@ Casper.prototype.waitForResource = function waitForResource(test, then, onTimeou
2090 timeout = timeout ? timeout : this.options.waitTimeout; 2091 timeout = timeout ? timeout : this.options.waitTimeout;
2091 return this.waitFor(function _check() { 2092 return this.waitFor(function _check() {
2092 return this.resourceExists(test); 2093 return this.resourceExists(test);
2093 }, then, onTimeout, timeout); 2094 }, then, onTimeout, timeout, { resource: test });
2094 }; 2095 };
2095 2096
2096 /** 2097 /**
...@@ -2112,7 +2113,7 @@ Casper.prototype.waitForUrl = function waitForUrl(url, then, onTimeout, timeout) ...@@ -2112,7 +2113,7 @@ Casper.prototype.waitForUrl = function waitForUrl(url, then, onTimeout, timeout)
2112 return url.test(this.getCurrentUrl()); 2113 return url.test(this.getCurrentUrl());
2113 } 2114 }
2114 throw new CasperError('invalid url argument'); 2115 throw new CasperError('invalid url argument');
2115 }, then, onTimeout, timeout); 2116 }, then, onTimeout, timeout, { url: url });
2116 }; 2117 };
2117 2118
2118 /** 2119 /**
...@@ -2131,7 +2132,7 @@ Casper.prototype.waitForSelector = function waitForSelector(selector, then, onTi ...@@ -2131,7 +2132,7 @@ Casper.prototype.waitForSelector = function waitForSelector(selector, then, onTi
2131 timeout = timeout ? timeout : this.options.waitTimeout; 2132 timeout = timeout ? timeout : this.options.waitTimeout;
2132 return this.waitFor(function _check() { 2133 return this.waitFor(function _check() {
2133 return this.exists(selector); 2134 return this.exists(selector);
2134 }, then, onTimeout, timeout); 2135 }, then, onTimeout, timeout, { selector: selector });
2135 }; 2136 };
2136 2137
2137 /** 2138 /**
...@@ -2153,7 +2154,7 @@ Casper.prototype.waitForText = function(pattern, then, onTimeout, timeout) { ...@@ -2153,7 +2154,7 @@ Casper.prototype.waitForText = function(pattern, then, onTimeout, timeout) {
2153 return pattern.test(content); 2154 return pattern.test(content);
2154 } 2155 }
2155 return content.indexOf(pattern) !== -1; 2156 return content.indexOf(pattern) !== -1;
2156 }, then, onTimeout, timeout); 2157 }, then, onTimeout, timeout, { text: pattern });
2157 }; 2158 };
2158 2159
2159 /** 2160 /**
...@@ -2173,7 +2174,7 @@ Casper.prototype.waitForSelectorTextChange = function(selector, then, onTimeout, ...@@ -2173,7 +2174,7 @@ Casper.prototype.waitForSelectorTextChange = function(selector, then, onTimeout,
2173 var currentSelectorText = this.fetchText(selector); 2174 var currentSelectorText = this.fetchText(selector);
2174 return this.waitFor(function _check() { 2175 return this.waitFor(function _check() {
2175 return currentSelectorText !== this.fetchText(selector); 2176 return currentSelectorText !== this.fetchText(selector);
2176 }, then, onTimeout, timeout); 2177 }, then, onTimeout, timeout, { selectorTextChange: selector });
2177 }; 2178 };
2178 2179
2179 /** 2180 /**
...@@ -2192,7 +2193,10 @@ Casper.prototype.waitWhileSelector = function waitWhileSelector(selector, then, ...@@ -2192,7 +2193,10 @@ Casper.prototype.waitWhileSelector = function waitWhileSelector(selector, then,
2192 timeout = timeout ? timeout : this.options.waitTimeout; 2193 timeout = timeout ? timeout : this.options.waitTimeout;
2193 return this.waitFor(function _check() { 2194 return this.waitFor(function _check() {
2194 return !this.exists(selector); 2195 return !this.exists(selector);
2195 }, then, onTimeout, timeout); 2196 }, then, onTimeout, timeout, {
2197 selector: selector,
2198 waitWhile: true
2199 });
2196 }; 2200 };
2197 2201
2198 /** 2202 /**
...@@ -2211,7 +2215,7 @@ Casper.prototype.waitUntilVisible = function waitUntilVisible(selector, then, on ...@@ -2211,7 +2215,7 @@ Casper.prototype.waitUntilVisible = function waitUntilVisible(selector, then, on
2211 timeout = timeout ? timeout : this.options.waitTimeout; 2215 timeout = timeout ? timeout : this.options.waitTimeout;
2212 return this.waitFor(function _check() { 2216 return this.waitFor(function _check() {
2213 return this.visible(selector); 2217 return this.visible(selector);
2214 }, then, onTimeout, timeout); 2218 }, then, onTimeout, timeout, { visible: selector });
2215 }; 2219 };
2216 2220
2217 /** 2221 /**
...@@ -2230,7 +2234,10 @@ Casper.prototype.waitWhileVisible = function waitWhileVisible(selector, then, on ...@@ -2230,7 +2234,10 @@ Casper.prototype.waitWhileVisible = function waitWhileVisible(selector, then, on
2230 timeout = timeout ? timeout : this.options.waitTimeout; 2234 timeout = timeout ? timeout : this.options.waitTimeout;
2231 return this.waitFor(function _check() { 2235 return this.waitFor(function _check() {
2232 return !this.visible(selector); 2236 return !this.visible(selector);
2233 }, then, onTimeout, timeout); 2237 }, then, onTimeout, timeout, {
2238 visible: selector,
2239 waitWhile: true
2240 });
2234 }; 2241 };
2235 2242
2236 /** 2243 /**
......