Commit df51de7d df51de7d3143965eff60a73ad72e6a6b925d28f3 by Nicolas Perriault

added regexp support to Casper.resourceExists()

1 parent 87c0a322
...@@ -612,7 +612,7 @@ Casper.prototype.log = function(message, level, space) { ...@@ -612,7 +612,7 @@ Casper.prototype.log = function(message, level, space) {
612 message = f('%s [%s] %s', levelStr, space, message); 612 message = f('%s [%s] %s', levelStr, space, message);
613 } 613 }
614 if (this.options.verbose) { 614 if (this.options.verbose) {
615 this.echo(message); // direct output 615 this.echo(this.filter('log.message', message) || message); // direct output
616 } 616 }
617 this.result.log.push(entry); 617 this.result.log.push(entry);
618 this.emit('log', entry); 618 this.emit('log', entry);
...@@ -675,17 +675,28 @@ Casper.prototype.repeat = function(times, then) { ...@@ -675,17 +675,28 @@ Casper.prototype.repeat = function(times, then) {
675 /** 675 /**
676 * Checks if a given resource was loaded by the remote page. 676 * Checks if a given resource was loaded by the remote page.
677 * 677 *
678 * @param Function/String test A test function or string. In case a string is passed, url matching will be tested. 678 * @param Function/String/RegExp test A test function, string or regular expression.
679 * In case a string is passed, url matching will be tested.
679 * @return Boolean 680 * @return Boolean
680 */ 681 */
681 Casper.prototype.resourceExists = function(test) { 682 Casper.prototype.resourceExists = function(test) {
682 var testFn; 683 var testFn;
683 if (utils.isString(test)) { 684 switch (utils.betterTypeOf(test)) {
684 testFn = function (res) { 685 case "string":
686 testFn = function(res) {
685 return res.url.search(test) !== -1; 687 return res.url.search(test) !== -1;
686 }; 688 };
687 } else { 689 break;
690 case "regexp":
691 testFn = function(res) {
692 return test.test(res.url);
693 };
694 break;
695 case "function":
688 testFn = test; 696 testFn = test;
697 break;
698 default:
699 throw new Error("Invalid type");
689 } 700 }
690 return this.resources.some(testFn); 701 return this.resources.some(testFn);
691 }; 702 };
......
...@@ -8,12 +8,16 @@ do(casper) -> ...@@ -8,12 +8,16 @@ do(casper) ->
8 "two resources found" 8 "two resources found"
9 ) 9 )
10 @test.assertResourceExists( 10 @test.assertResourceExists(
11 /phantom\.png/i
12 "phantom image found via test RegExp"
13 )
14 @test.assertResourceExists(
11 (res) -> res.url.match "phantom.png" 15 (res) -> res.url.match "phantom.png"
12 "phantom image found via test function" 16 "phantom image found via test Function"
13 ) 17 )
14 @test.assertResourceExists( 18 @test.assertResourceExists(
15 "phantom.png" 19 "phantom.png"
16 "phantom image found via test string" 20 "phantom image found via test String"
17 ) 21 )
18 onTimeout = -> @test.fail "waitForResource timeout occured" 22 onTimeout = -> @test.fail "waitForResource timeout occured"
19 @waitForResource "phantom.png", onTime, onTimeout 23 @waitForResource "phantom.png", onTime, onTimeout
......