added events.filter and events.onfilter methods
Showing
3 changed files
with
35 additions
and
13 deletions
... | @@ -653,7 +653,7 @@ Casper.prototype.open = function(location, options) { | ... | @@ -653,7 +653,7 @@ Casper.prototype.open = function(location, options) { |
653 | this.setHttpAuth(httpAuth.username, httpAuth.password); | 653 | this.setHttpAuth(httpAuth.username, httpAuth.password); |
654 | } | 654 | } |
655 | this.emit('open', location); | 655 | this.emit('open', location); |
656 | this.page.open(location); | 656 | this.page.open(this.filter('open.location', location) || location); |
657 | return this; | 657 | return this; |
658 | }; | 658 | }; |
659 | 659 | ... | ... |
... | @@ -213,3 +213,29 @@ EventEmitter.prototype.listeners = function(type) { | ... | @@ -213,3 +213,29 @@ EventEmitter.prototype.listeners = function(type) { |
213 | } | 213 | } |
214 | return this._events[type]; | 214 | return this._events[type]; |
215 | }; | 215 | }; |
216 | |||
217 | // Added for CasperJS: filters a value attached to an event | ||
218 | EventEmitter.prototype.filter = function() { | ||
219 | var type = arguments[0]; | ||
220 | if (!this._filters) { | ||
221 | return; | ||
222 | } | ||
223 | var filter = this._filters[type]; | ||
224 | if (!filter || typeof filter !== 'function') { | ||
225 | return; | ||
226 | } | ||
227 | return filter.apply(null, Array.prototype.splice.call(arguments, 1)); | ||
228 | }; | ||
229 | |||
230 | EventEmitter.prototype.setFilter = function(type, filterFn) { | ||
231 | if (!this._filters) this._filters = {}; | ||
232 | if ('function' !== typeof filterFn) { | ||
233 | throw new Error('setFilter only takes instances of Function'); | ||
234 | } | ||
235 | if (!this._filters[type]) { | ||
236 | this._filters[type] = filterFn; | ||
237 | return true; | ||
238 | } | ||
239 | // TODO: process multiple filters? in which order? disallow? | ||
240 | return false; | ||
241 | }; | ... | ... |
... | @@ -5,28 +5,23 @@ if (!phantom.casperLoaded) { | ... | @@ -5,28 +5,23 @@ if (!phantom.casperLoaded) { |
5 | 5 | ||
6 | var fs = require('fs'); | 6 | var fs = require('fs'); |
7 | var utils = require('utils'); | 7 | var utils = require('utils'); |
8 | |||
9 | // Overriding Casper.open to prefix all test urls | ||
10 | require('casper').Casper.extend({ | ||
11 | open: function(location, options) { | ||
12 | options = utils.isType(options, "object") ? options : {}; | ||
13 | this.requestUrl = location; | ||
14 | var url = 'file://' + phantom.casperPath + '/' + location; | ||
15 | this.page.open(url); | ||
16 | return this; | ||
17 | } | ||
18 | }); | ||
19 | |||
20 | var casper = require('casper').create({ | 8 | var casper = require('casper').create({ |
21 | faultTolerant: false | 9 | faultTolerant: false |
22 | }); | 10 | }); |
23 | 11 | ||
12 | // Overriding Casper.open to prefix all test urls | ||
13 | casper.setFilter('open.location', function(location) { | ||
14 | return 'file://' + phantom.casperPath + '/' + location; | ||
15 | }); | ||
16 | |||
24 | var tests = []; | 17 | var tests = []; |
18 | |||
25 | if (casper.cli.args.length) { | 19 | if (casper.cli.args.length) { |
26 | tests = casper.cli.args.filter(function(path) { | 20 | tests = casper.cli.args.filter(function(path) { |
27 | return fs.isFile(path) || fs.isDirectory(path); | 21 | return fs.isFile(path) || fs.isDirectory(path); |
28 | }); | 22 | }); |
29 | } | 23 | } |
24 | |||
30 | if (!tests.length) { | 25 | if (!tests.length) { |
31 | if (casper.cli.args.length > 0) { | 26 | if (casper.cli.args.length > 0) { |
32 | casper.echo('No valid test path passed, exiting.', 'ERROR').exit(1); | 27 | casper.echo('No valid test path passed, exiting.', 'ERROR').exit(1); |
... | @@ -35,4 +30,5 @@ if (!tests.length) { | ... | @@ -35,4 +30,5 @@ if (!tests.length) { |
35 | casper.echo('Running complete CasperJS test suite', 'INFO'); | 30 | casper.echo('Running complete CasperJS test suite', 'INFO'); |
36 | tests = [fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'suites'))]; | 31 | tests = [fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'suites'))]; |
37 | } | 32 | } |
33 | |||
38 | casper.test.runSuites.apply(casper.test, tests); | 34 | casper.test.runSuites.apply(casper.test, tests); | ... | ... |
-
Please register or sign in to post a comment