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.
``waitFor.timeout``
~~~~~~~~~~~~~~~~~~~
**Arguments:** ``None``
**Arguments:** ``[timeout, details]``
Emitted when the execution time of a ``Casper.wait*()`` operation has exceeded the value of ``timeout``.
Emitted when the execution time of a ``Casper.wait*()`` operation has exceeded the value of ``Casper.options.stepTimeout``.
``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.
.. index:: filters
......
......@@ -1943,7 +1943,7 @@ You can also write the same thing like this::
``waitFor()``
-------------------------------------------------------------------------------
**Signature:** ``waitFor(Function testFx[, Function then, Function onTimeout, Number timeout])``
**Signature:** ``waitFor(Function testFx[, Function then, Function onTimeout, Number timeout, Object details])``
Waits until a function returns true to process any next step.
......@@ -1977,6 +1977,9 @@ Example using the ``onTimeout`` callback::
casper.run();
``details`` is a property bag of various information that will be passed to the ``waitFor.timeout`` event, if it is emitted.
This can be used for better error messages or to conditionally ignore some timeout events.
.. _casper_waitforpopup:
.. index:: Popups, New window, window.open, Tabs
......
......@@ -2003,9 +2003,10 @@ Casper.prototype.waitDone = function waitDone() {
* @param Function then The next step to perform (optional)
* @param Function onTimeout A callback function to call on timeout (optional)
* @param Number timeout The max amount of time to wait, in milliseconds (optional)
* @param Object details A property bag of information about the condition being waited on (optional)
* @return Casper
*/
Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout, details) {
"use strict";
this.checkStarted();
timeout = timeout ? timeout : this.options.waitTimeout;
......@@ -2019,7 +2020,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
this.waitStart();
var start = new Date().getTime();
var condition = false;
var interval = setInterval(function _check(self, testFx, timeout, onTimeout) {
var interval = setInterval(function _check(self) {
/*jshint maxstatements:20*/
if ((new Date().getTime() - start < timeout) && !condition) {
condition = testFx.call(self, self);
......@@ -2029,7 +2030,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
if (!condition) {
self.log("Casper.waitFor() timeout", "warning");
var onWaitTimeout = onTimeout ? onTimeout : self.options.onWaitTimeout;
self.emit('waitFor.timeout', timeout, onWaitTimeout);
self.emit('waitFor.timeout', timeout, details || { testFx: testFx });
clearInterval(interval); // refs #383
if (!utils.isFunction(onWaitTimeout)) {
throw new CasperError('Invalid timeout function');
......@@ -2047,7 +2048,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
if (then) {
self.then(then);
}
}, this.options.retryTimeout, this, testFx, timeout, onTimeout);
}, this.options.retryTimeout, this);
this.waiters.push(interval);
});
};
......@@ -2071,7 +2072,7 @@ Casper.prototype.waitForPopup = function waitForPopup(urlPattern, then, onTimeou
} catch (e) {
return false;
}
}, then, onTimeout, timeout);
}, then, onTimeout, timeout, { popup: urlPattern });
};
/**
......@@ -2090,7 +2091,7 @@ Casper.prototype.waitForResource = function waitForResource(test, then, onTimeou
timeout = timeout ? timeout : this.options.waitTimeout;
return this.waitFor(function _check() {
return this.resourceExists(test);
}, then, onTimeout, timeout);
}, then, onTimeout, timeout, { resource: test });
};
/**
......@@ -2112,7 +2113,7 @@ Casper.prototype.waitForUrl = function waitForUrl(url, then, onTimeout, timeout)
return url.test(this.getCurrentUrl());
}
throw new CasperError('invalid url argument');
}, then, onTimeout, timeout);
}, then, onTimeout, timeout, { url: url });
};
/**
......@@ -2131,7 +2132,7 @@ Casper.prototype.waitForSelector = function waitForSelector(selector, then, onTi
timeout = timeout ? timeout : this.options.waitTimeout;
return this.waitFor(function _check() {
return this.exists(selector);
}, then, onTimeout, timeout);
}, then, onTimeout, timeout, { selector: selector });
};
/**
......@@ -2153,7 +2154,7 @@ Casper.prototype.waitForText = function(pattern, then, onTimeout, timeout) {
return pattern.test(content);
}
return content.indexOf(pattern) !== -1;
}, then, onTimeout, timeout);
}, then, onTimeout, timeout, { text: pattern });
};
/**
......@@ -2173,7 +2174,7 @@ Casper.prototype.waitForSelectorTextChange = function(selector, then, onTimeout,
var currentSelectorText = this.fetchText(selector);
return this.waitFor(function _check() {
return currentSelectorText !== this.fetchText(selector);
}, then, onTimeout, timeout);
}, then, onTimeout, timeout, { selectorTextChange: selector });
};
/**
......@@ -2192,7 +2193,10 @@ Casper.prototype.waitWhileSelector = function waitWhileSelector(selector, then,
timeout = timeout ? timeout : this.options.waitTimeout;
return this.waitFor(function _check() {
return !this.exists(selector);
}, then, onTimeout, timeout);
}, then, onTimeout, timeout, {
selector: selector,
waitWhile: true
});
};
/**
......@@ -2211,7 +2215,7 @@ Casper.prototype.waitUntilVisible = function waitUntilVisible(selector, then, on
timeout = timeout ? timeout : this.options.waitTimeout;
return this.waitFor(function _check() {
return this.visible(selector);
}, then, onTimeout, timeout);
}, then, onTimeout, timeout, { visible: selector });
};
/**
......@@ -2230,7 +2234,10 @@ Casper.prototype.waitWhileVisible = function waitWhileVisible(selector, then, on
timeout = timeout ? timeout : this.options.waitTimeout;
return this.waitFor(function _check() {
return !this.visible(selector);
}, then, onTimeout, timeout);
}, then, onTimeout, timeout, {
visible: selector,
waitWhile: true
});
};
/**
......