Commit e03dbc90 e03dbc90da35057bcdf158913f9da5c48387069d by Nicolas Perriault

closes #373 - Casper.waitForText() RegExp support

1 parent 97955a17
......@@ -4,6 +4,7 @@ CasperJS Changelog
XXXX-XX-XX, v1.0.2
------------------
- closed [#373](https://github.com/n1k0/casperjs/issues/373) - added RegExp support to `Casper.waitForText()`
- fixed [#368](https://github.com/n1k0/casperjs/issues/368) - Remote JS error is thrown when a click target is missing after `click()`
- merged PR [#357](https://github.com/n1k0/casperjs/pull/357) - fire the `input` event after setting input value (required to support [angular.js](http://angularjs.org/) apps)
......
......@@ -1796,20 +1796,24 @@ Casper.prototype.waitForSelector = function waitForSelector(selector, then, onTi
};
/**
* Waits until the page contains given HTML text.
* Waits until the page contains given HTML text or matches a given RegExp.
*
* @param String text Text to wait for
* @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 String|RegExp pattern Text or RegExp to wait for
* @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)
* @return Casper
*/
Casper.prototype.waitForText = function(text, then, onTimeout, timeout) {
Casper.prototype.waitForText = function(pattern, then, onTimeout, timeout) {
"use strict";
this.checkStarted();
timeout = timeout ? timeout : this.options.waitTimeout;
return this.waitFor(function _check() {
return this.getPageContent().indexOf(text) !== -1;
var content = this.getPageContent();
if (utils.isRegExp(pattern)) {
return pattern.test(content);
}
return content.indexOf(pattern) !== -1;
}, then, onTimeout, timeout);
};
......
......@@ -372,6 +372,18 @@ function isObject(value) {
exports.isObject = isObject;
/**
* Checks if value is a RegExp
*
* @param mixed value
* @return Boolean
*/
function isRegExp(value) {
"use strict";
return isType(value, "regexp");
}
exports.isRegExp = isRegExp;
/**
* Checks if value is a javascript String
*
* @param mixed value
......
......@@ -32,6 +32,14 @@ casper.thenOpen('tests/site/waitFor.html').waitForText('<li>four</li>', function
this.test.fail('Casper.waitForText() can wait for text');
});
casper.thenOpen('tests/site/waitFor.html').waitForText(/four/i, function() {
this.test.comment('Casper.waitForText()');
this.test.pass('Casper.waitForText() can wait for regexp');
}, function() {
this.test.comment('Casper.waitForText()');
this.test.fail('Casper.waitForText() can wait for regexp');
});
casper.run(function() {
this.test.done(3);
this.test.done(4);
});
......