Commit e03dbc90 e03dbc90da35057bcdf158913f9da5c48387069d by Nicolas Perriault

closes #373 - Casper.waitForText() RegExp support

1 parent 97955a17
...@@ -4,6 +4,7 @@ CasperJS Changelog ...@@ -4,6 +4,7 @@ CasperJS Changelog
4 XXXX-XX-XX, v1.0.2 4 XXXX-XX-XX, v1.0.2
5 ------------------ 5 ------------------
6 6
7 - closed [#373](https://github.com/n1k0/casperjs/issues/373) - added RegExp support to `Casper.waitForText()`
7 - fixed [#368](https://github.com/n1k0/casperjs/issues/368) - Remote JS error is thrown when a click target is missing after `click()` 8 - fixed [#368](https://github.com/n1k0/casperjs/issues/368) - Remote JS error is thrown when a click target is missing after `click()`
8 - 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) 9 - 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)
9 10
......
...@@ -1796,20 +1796,24 @@ Casper.prototype.waitForSelector = function waitForSelector(selector, then, onTi ...@@ -1796,20 +1796,24 @@ Casper.prototype.waitForSelector = function waitForSelector(selector, then, onTi
1796 }; 1796 };
1797 1797
1798 /** 1798 /**
1799 * Waits until the page contains given HTML text. 1799 * Waits until the page contains given HTML text or matches a given RegExp.
1800 * 1800 *
1801 * @param String text Text to wait for 1801 * @param String|RegExp pattern Text or RegExp to wait for
1802 * @param Function then The next step to perform (optional) 1802 * @param Function then The next step to perform (optional)
1803 * @param Function onTimeout A callback function to call on timeout (optional) 1803 * @param Function onTimeout A callback function to call on timeout (optional)
1804 * @param Number timeout The max amount of time to wait, in milliseconds (optional) 1804 * @param Number timeout The max amount of time to wait, in milliseconds (optional)
1805 * @return Casper 1805 * @return Casper
1806 */ 1806 */
1807 Casper.prototype.waitForText = function(text, then, onTimeout, timeout) { 1807 Casper.prototype.waitForText = function(pattern, then, onTimeout, timeout) {
1808 "use strict"; 1808 "use strict";
1809 this.checkStarted(); 1809 this.checkStarted();
1810 timeout = timeout ? timeout : this.options.waitTimeout; 1810 timeout = timeout ? timeout : this.options.waitTimeout;
1811 return this.waitFor(function _check() { 1811 return this.waitFor(function _check() {
1812 return this.getPageContent().indexOf(text) !== -1; 1812 var content = this.getPageContent();
1813 if (utils.isRegExp(pattern)) {
1814 return pattern.test(content);
1815 }
1816 return content.indexOf(pattern) !== -1;
1813 }, then, onTimeout, timeout); 1817 }, then, onTimeout, timeout);
1814 }; 1818 };
1815 1819
......
...@@ -372,6 +372,18 @@ function isObject(value) { ...@@ -372,6 +372,18 @@ function isObject(value) {
372 exports.isObject = isObject; 372 exports.isObject = isObject;
373 373
374 /** 374 /**
375 * Checks if value is a RegExp
376 *
377 * @param mixed value
378 * @return Boolean
379 */
380 function isRegExp(value) {
381 "use strict";
382 return isType(value, "regexp");
383 }
384 exports.isRegExp = isRegExp;
385
386 /**
375 * Checks if value is a javascript String 387 * Checks if value is a javascript String
376 * 388 *
377 * @param mixed value 389 * @param mixed value
......
...@@ -32,6 +32,14 @@ casper.thenOpen('tests/site/waitFor.html').waitForText('<li>four</li>', function ...@@ -32,6 +32,14 @@ casper.thenOpen('tests/site/waitFor.html').waitForText('<li>four</li>', function
32 this.test.fail('Casper.waitForText() can wait for text'); 32 this.test.fail('Casper.waitForText() can wait for text');
33 }); 33 });
34 34
35 casper.thenOpen('tests/site/waitFor.html').waitForText(/four/i, function() {
36 this.test.comment('Casper.waitForText()');
37 this.test.pass('Casper.waitForText() can wait for regexp');
38 }, function() {
39 this.test.comment('Casper.waitForText()');
40 this.test.fail('Casper.waitForText() can wait for regexp');
41 });
42
35 casper.run(function() { 43 casper.run(function() {
36 this.test.done(3); 44 this.test.done(4);
37 }); 45 });
......