Commit 6baa46ce 6baa46ce8fd47a4fc18ab37b34b3013f16463ed4 by Nicolas Perriault

added events.filter and events.onfilter methods

1 parent aea6e574
......@@ -653,7 +653,7 @@ Casper.prototype.open = function(location, options) {
this.setHttpAuth(httpAuth.username, httpAuth.password);
}
this.emit('open', location);
this.page.open(location);
this.page.open(this.filter('open.location', location) || location);
return this;
};
......
......@@ -213,3 +213,29 @@ EventEmitter.prototype.listeners = function(type) {
}
return this._events[type];
};
// Added for CasperJS: filters a value attached to an event
EventEmitter.prototype.filter = function() {
var type = arguments[0];
if (!this._filters) {
return;
}
var filter = this._filters[type];
if (!filter || typeof filter !== 'function') {
return;
}
return filter.apply(null, Array.prototype.splice.call(arguments, 1));
};
EventEmitter.prototype.setFilter = function(type, filterFn) {
if (!this._filters) this._filters = {};
if ('function' !== typeof filterFn) {
throw new Error('setFilter only takes instances of Function');
}
if (!this._filters[type]) {
this._filters[type] = filterFn;
return true;
}
// TODO: process multiple filters? in which order? disallow?
return false;
};
......
......@@ -5,28 +5,23 @@ if (!phantom.casperLoaded) {
var fs = require('fs');
var utils = require('utils');
// Overriding Casper.open to prefix all test urls
require('casper').Casper.extend({
open: function(location, options) {
options = utils.isType(options, "object") ? options : {};
this.requestUrl = location;
var url = 'file://' + phantom.casperPath + '/' + location;
this.page.open(url);
return this;
}
});
var casper = require('casper').create({
faultTolerant: false
});
// Overriding Casper.open to prefix all test urls
casper.setFilter('open.location', function(location) {
return 'file://' + phantom.casperPath + '/' + location;
});
var tests = [];
if (casper.cli.args.length) {
tests = casper.cli.args.filter(function(path) {
return fs.isFile(path) || fs.isDirectory(path);
});
}
if (!tests.length) {
if (casper.cli.args.length > 0) {
casper.echo('No valid test path passed, exiting.', 'ERROR').exit(1);
......@@ -35,4 +30,5 @@ if (!tests.length) {
casper.echo('Running complete CasperJS test suite', 'INFO');
tests = [fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'suites'))];
}
casper.test.runSuites.apply(casper.test, tests);
......