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) {
message = f('%s [%s] %s', levelStr, space, message);
}
if (this.options.verbose) {
this.echo(message); // direct output
this.echo(this.filter('log.message', message) || message); // direct output
}
this.result.log.push(entry);
this.emit('log', entry);
......@@ -675,17 +675,28 @@ Casper.prototype.repeat = function(times, then) {
/**
* Checks if a given resource was loaded by the remote page.
*
* @param Function/String test A test function or string. In case a string is passed, url matching will be tested.
* @param Function/String/RegExp test A test function, string or regular expression.
* In case a string is passed, url matching will be tested.
* @return Boolean
*/
Casper.prototype.resourceExists = function(test) {
var testFn;
if (utils.isString(test)) {
testFn = function (res) {
switch (utils.betterTypeOf(test)) {
case "string":
testFn = function(res) {
return res.url.search(test) !== -1;
};
} else {
break;
case "regexp":
testFn = function(res) {
return test.test(res.url);
};
break;
case "function":
testFn = test;
break;
default:
throw new Error("Invalid type");
}
return this.resources.some(testFn);
};
......
......@@ -8,12 +8,16 @@ do(casper) ->
"two resources found"
)
@test.assertResourceExists(
/phantom\.png/i
"phantom image found via test RegExp"
)
@test.assertResourceExists(
(res) -> res.url.match "phantom.png"
"phantom image found via test function"
"phantom image found via test Function"
)
@test.assertResourceExists(
"phantom.png"
"phantom image found via test string"
"phantom image found via test String"
)
onTimeout = -> @test.fail "waitForResource timeout occured"
@waitForResource "phantom.png", onTime, onTimeout
......