added format() helper; removed 'casper.' suffix in event names
Showing
12 changed files
with
209 additions
and
108 deletions
1 | Copyright (c) 2011 Nicolas Perriault | 1 | Copyright (c) 2011-2012 Nicolas Perriault |
2 | 2 | ||
3 | Permission is hereby granted, free of charge, to any person obtaining a copy | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy |
4 | of this software and associated documentation files (the "Software"), to deal | 4 | of this software and associated documentation files (the "Software"), to deal | ... | ... |
... | @@ -4,7 +4,9 @@ | ... | @@ -4,7 +4,9 @@ |
4 | * Documentation: http://n1k0.github.com/casperjs/ | 4 | * Documentation: http://n1k0.github.com/casperjs/ |
5 | * Repository: http://github.com/n1k0/casperjs | 5 | * Repository: http://github.com/n1k0/casperjs |
6 | * | 6 | * |
7 | * Copyright (c) 2011 Nicolas Perriault | 7 | * Copyright (c) 2011-2012 Nicolas Perriault |
8 | * | ||
9 | * Part of source code is Copyright Joyent, Inc. and other Node contributors. | ||
8 | * | 10 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | * copy of this software and associated documentation files (the "Software"), | 12 | * copy of this software and associated documentation files (the "Software"), |
... | @@ -25,6 +27,7 @@ | ... | @@ -25,6 +27,7 @@ |
25 | * DEALINGS IN THE SOFTWARE. | 27 | * DEALINGS IN THE SOFTWARE. |
26 | * | 28 | * |
27 | */ | 29 | */ |
30 | |||
28 | if (!phantom.casperLoaded) { | 31 | if (!phantom.casperLoaded) { |
29 | // see http://semver.org/ | 32 | // see http://semver.org/ |
30 | phantom.casperVersion = { | 33 | phantom.casperVersion = { |
... | @@ -149,6 +152,7 @@ if (!phantom.casperLoaded) { | ... | @@ -149,6 +152,7 @@ if (!phantom.casperLoaded) { |
149 | paths.push(fs.pathJoin(dir, 'node_modules', path)); | 152 | paths.push(fs.pathJoin(dir, 'node_modules', path)); |
150 | dir = fs.dirname(dir); | 153 | dir = fs.dirname(dir); |
151 | } | 154 | } |
155 | paths.push(fs.pathJoin(requireDir, 'lib', path)); | ||
152 | paths.push(fs.pathJoin(requireDir, 'modules', path)); | 156 | paths.push(fs.pathJoin(requireDir, 'modules', path)); |
153 | } | 157 | } |
154 | paths.forEach(function(testPath) { | 158 | paths.forEach(function(testPath) { |
... | @@ -221,7 +225,9 @@ if (!!phantom.casperArgs.options.version) { | ... | @@ -221,7 +225,9 @@ if (!!phantom.casperArgs.options.version) { |
221 | console.log(phantom.casperVersion.toString()); | 225 | console.log(phantom.casperVersion.toString()); |
222 | phantom.exit(0); | 226 | phantom.exit(0); |
223 | } else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) { | 227 | } else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) { |
228 | var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.'); | ||
224 | console.log('CasperJS version ' + phantom.casperVersion.toString() + ' at ' + phantom.casperPath); | 229 | console.log('CasperJS version ' + phantom.casperVersion.toString() + ' at ' + phantom.casperPath); |
230 | console.log('Using PhantomJS version ' + phantomVersion); | ||
225 | console.log('Usage: casperjs script.(js|coffee) [options...]'); | 231 | console.log('Usage: casperjs script.(js|coffee) [options...]'); |
226 | console.log('Read the docs http://n1k0.github.com/casperjs/'); | 232 | console.log('Read the docs http://n1k0.github.com/casperjs/'); |
227 | phantom.exit(0); | 233 | phantom.exit(0); | ... | ... |
... | @@ -4,7 +4,9 @@ | ... | @@ -4,7 +4,9 @@ |
4 | * Documentation: http://n1k0.github.com/casperjs/ | 4 | * Documentation: http://n1k0.github.com/casperjs/ |
5 | * Repository: http://github.com/n1k0/casperjs | 5 | * Repository: http://github.com/n1k0/casperjs |
6 | * | 6 | * |
7 | * Copyright (c) 2011 Nicolas Perriault | 7 | * Copyright (c) 2011-2012 Nicolas Perriault |
8 | * | ||
9 | * Part of source code is Copyright Joyent, Inc. and other Node contributors. | ||
8 | * | 10 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | * copy of this software and associated documentation files (the "Software"), | 12 | * copy of this software and associated documentation files (the "Software"), |
... | @@ -26,9 +28,13 @@ | ... | @@ -26,9 +28,13 @@ |
26 | * | 28 | * |
27 | */ | 29 | */ |
28 | 30 | ||
31 | var colorizer = require('colorizer'); | ||
29 | var events = require('events'); | 32 | var events = require('events'); |
30 | var fs = require('fs'); | 33 | var fs = require('fs'); |
34 | var mouse = require('mouse'); | ||
35 | var tester = require('tester'); | ||
31 | var utils = require('utils'); | 36 | var utils = require('utils'); |
37 | var f = utils.format; | ||
32 | 38 | ||
33 | exports.create = function(options) { | 39 | exports.create = function(options) { |
34 | return new Casper(options); | 40 | return new Casper(options); |
... | @@ -74,7 +80,7 @@ var Casper = function(options) { | ... | @@ -74,7 +80,7 @@ var Casper = function(options) { |
74 | // properties | 80 | // properties |
75 | this.checker = null; | 81 | this.checker = null; |
76 | this.cli = phantom.casperArgs; | 82 | this.cli = phantom.casperArgs; |
77 | this.colorizer = require('colorizer').create(); | 83 | this.colorizer = colorizer.create(); |
78 | this.currentUrl = 'about:blank'; | 84 | this.currentUrl = 'about:blank'; |
79 | this.currentHTTPStatus = 200; | 85 | this.currentHTTPStatus = 200; |
80 | this.defaultWaitTimeout = 5000; | 86 | this.defaultWaitTimeout = 5000; |
... | @@ -88,7 +94,7 @@ var Casper = function(options) { | ... | @@ -88,7 +94,7 @@ var Casper = function(options) { |
88 | warning: 'COMMENT', | 94 | warning: 'COMMENT', |
89 | error: 'ERROR' | 95 | error: 'ERROR' |
90 | }; | 96 | }; |
91 | this.mouse = require('mouse').create(this); | 97 | this.mouse = mouse.create(this); |
92 | this.options = utils.mergeObjects(this.defaults, options); | 98 | this.options = utils.mergeObjects(this.defaults, options); |
93 | this.page = null; | 99 | this.page = null; |
94 | this.pendingWait = false; | 100 | this.pendingWait = false; |
... | @@ -102,12 +108,12 @@ var Casper = function(options) { | ... | @@ -102,12 +108,12 @@ var Casper = function(options) { |
102 | this.started = false; | 108 | this.started = false; |
103 | this.step = -1; | 109 | this.step = -1; |
104 | this.steps = []; | 110 | this.steps = []; |
105 | this.test = require('tester').create(this); | 111 | this.test = tester.create(this); |
106 | }; | 112 | }; |
107 | 113 | ||
114 | // Casper class is an EventEmitter | ||
108 | utils.inherits(Casper, events.EventEmitter); | 115 | utils.inherits(Casper, events.EventEmitter); |
109 | 116 | ||
110 | |||
111 | /** | 117 | /** |
112 | * Go a step back in browser's history | 118 | * Go a step back in browser's history |
113 | * | 119 | * |
... | @@ -115,7 +121,7 @@ utils.inherits(Casper, events.EventEmitter); | ... | @@ -115,7 +121,7 @@ utils.inherits(Casper, events.EventEmitter); |
115 | */ | 121 | */ |
116 | Casper.prototype.back = function() { | 122 | Casper.prototype.back = function() { |
117 | return this.then(function() { | 123 | return this.then(function() { |
118 | this.emit('casper.back'); | 124 | this.emit('back'); |
119 | this.evaluate(function() { | 125 | this.evaluate(function() { |
120 | history.back(); | 126 | history.back(); |
121 | }); | 127 | }); |
... | @@ -158,12 +164,12 @@ Casper.prototype.capture = function(targetFile, clipRect) { | ... | @@ -158,12 +164,12 @@ Casper.prototype.capture = function(targetFile, clipRect) { |
158 | } | 164 | } |
159 | previousClipRect = this.page.clipRect; | 165 | previousClipRect = this.page.clipRect; |
160 | this.page.clipRect = clipRect; | 166 | this.page.clipRect = clipRect; |
161 | this.log('Capturing page to ' + targetFile + ' with clipRect' + JSON.stringify(clipRect), "debug"); | 167 | this.log(f("Capturing page to %s with clipRect %s", targetFile, JSON.stringify(clipRect)), "debug"); |
162 | } else { | 168 | } else { |
163 | this.log('Capturing page to ' + targetFile, "debug"); | 169 | this.log(f('Capturing page to %s', targetFile), "debug"); |
164 | } | 170 | } |
165 | if (!this.page.render(targetFile)) { | 171 | if (!this.page.render(targetFile)) { |
166 | this.log('Failed to save screenshot to ' + targetFile + '; please check permissions', "error"); | 172 | this.log(f("Failed to save screenshot to %s; please check permissions", targetFile), "error"); |
167 | } | 173 | } |
168 | if (previousClipRect) { | 174 | if (previousClipRect) { |
169 | this.page.clipRect = previousClipRect; | 175 | this.page.clipRect = previousClipRect; |
... | @@ -197,9 +203,10 @@ Casper.prototype.checkStep = function(self, onComplete) { | ... | @@ -197,9 +203,10 @@ Casper.prototype.checkStep = function(self, onComplete) { |
197 | self.runStep(step); | 203 | self.runStep(step); |
198 | } else { | 204 | } else { |
199 | self.result.time = new Date().getTime() - self.startTime; | 205 | self.result.time = new Date().getTime() - self.startTime; |
200 | self.log("Done " + self.steps.length + " steps in " + self.result.time + 'ms.', "info"); | 206 | self.log(f("Done %s steps in %dms", self.steps.length, self.result.time), "info"); |
201 | self.page.content = ''; // avoid having previously loaded DOM contents being still active (refs #34) | 207 | self.page.content = ''; // avoid having previously loaded DOM contents being still active (refs #34) |
202 | clearInterval(self.checker); | 208 | clearInterval(self.checker); |
209 | self.emit('step.complete'); | ||
203 | if (utils.isFunction(onComplete)) { | 210 | if (utils.isFunction(onComplete)) { |
204 | try { | 211 | try { |
205 | onComplete.call(self, self); | 212 | onComplete.call(self, self); |
... | @@ -207,7 +214,7 @@ Casper.prototype.checkStep = function(self, onComplete) { | ... | @@ -207,7 +214,7 @@ Casper.prototype.checkStep = function(self, onComplete) { |
207 | self.log("Could not complete final step: " + err, "error"); | 214 | self.log("Could not complete final step: " + err, "error"); |
208 | } | 215 | } |
209 | } else { | 216 | } else { |
210 | // default behavior is to exit phantom | 217 | // default behavior is to exit |
211 | self.exit(); | 218 | self.exit(); |
212 | } | 219 | } |
213 | } | 220 | } |
... | @@ -224,7 +231,7 @@ Casper.prototype.checkStep = function(self, onComplete) { | ... | @@ -224,7 +231,7 @@ Casper.prototype.checkStep = function(self, onComplete) { |
224 | Casper.prototype.click = function(selector, fallbackToHref) { | 231 | Casper.prototype.click = function(selector, fallbackToHref) { |
225 | fallbackToHref = utils.isType(fallbackToHref, "undefined") ? true : !!fallbackToHref; | 232 | fallbackToHref = utils.isType(fallbackToHref, "undefined") ? true : !!fallbackToHref; |
226 | this.log("Click on selector: " + selector, "debug"); | 233 | this.log("Click on selector: " + selector, "debug"); |
227 | this.emit('casper.click', selector, fallbackToHref); | 234 | this.emit('click', selector, fallbackToHref); |
228 | return this.evaluate(function(selector, fallbackToHref) { | 235 | return this.evaluate(function(selector, fallbackToHref) { |
229 | return __utils__.click(selector, fallbackToHref); | 236 | return __utils__.click(selector, fallbackToHref); |
230 | }, { | 237 | }, { |
... | @@ -245,7 +252,7 @@ Casper.prototype.createStep = function(fn, options) { | ... | @@ -245,7 +252,7 @@ Casper.prototype.createStep = function(fn, options) { |
245 | throw new Error("createStep(): a step definition must be a function"); | 252 | throw new Error("createStep(): a step definition must be a function"); |
246 | } | 253 | } |
247 | fn.options = utils.isObject(options) ? options : {}; | 254 | fn.options = utils.isObject(options) ? options : {}; |
248 | this.emit('casper.step.created', fn); | 255 | this.emit('step.created', fn); |
249 | return fn; | 256 | return fn; |
250 | }; | 257 | }; |
251 | 258 | ||
... | @@ -285,7 +292,7 @@ Casper.prototype.die = function(message, status) { | ... | @@ -285,7 +292,7 @@ Casper.prototype.die = function(message, status) { |
285 | this.result.time = new Date().getTime() - this.startTime; | 292 | this.result.time = new Date().getTime() - this.startTime; |
286 | message = utils.isString(message) && message.length > 0 ? message : DEFAULT_DIE_MESSAGE; | 293 | message = utils.isString(message) && message.length > 0 ? message : DEFAULT_DIE_MESSAGE; |
287 | this.log(message, "error"); | 294 | this.log(message, "error"); |
288 | this.emit('casper.die', message, status); | 295 | this.emit('die', message, status); |
289 | if (utils.isFunction(this.options.onDie)) { | 296 | if (utils.isFunction(this.options.onDie)) { |
290 | this.options.onDie.call(this, this, message, status); | 297 | this.options.onDie.call(this, this, message, status); |
291 | } | 298 | } |
... | @@ -304,7 +311,7 @@ Casper.prototype.download = function(url, targetPath) { | ... | @@ -304,7 +311,7 @@ Casper.prototype.download = function(url, targetPath) { |
304 | try { | 311 | try { |
305 | fs.write(targetPath, cu.decode(this.base64encode(url)), 'w'); | 312 | fs.write(targetPath, cu.decode(this.base64encode(url)), 'w'); |
306 | } catch (e) { | 313 | } catch (e) { |
307 | this.log("Error while downloading " + url + " to " + targetPath + ": " + e, "error"); | 314 | this.log(f("Error while downloading %s to %s: %s", url, targetPath, e), "error"); |
308 | } | 315 | } |
309 | return this; | 316 | return this; |
310 | }; | 317 | }; |
... | @@ -419,7 +426,7 @@ Casper.prototype.visible = function(selector) { | ... | @@ -419,7 +426,7 @@ Casper.prototype.visible = function(selector) { |
419 | * @return Casper | 426 | * @return Casper |
420 | */ | 427 | */ |
421 | Casper.prototype.exit = function(status) { | 428 | Casper.prototype.exit = function(status) { |
422 | this.emit('casper.exit', status); | 429 | this.emit('exit', status); |
423 | phantom.exit(status); | 430 | phantom.exit(status); |
424 | return this; | 431 | return this; |
425 | }; | 432 | }; |
... | @@ -452,7 +459,7 @@ Casper.prototype.fill = function(selector, vals, submit) { | ... | @@ -452,7 +459,7 @@ Casper.prototype.fill = function(selector, vals, submit) { |
452 | if (!utils.isObject(vals)) { | 459 | if (!utils.isObject(vals)) { |
453 | throw new Error("Form values must be provided as an object"); | 460 | throw new Error("Form values must be provided as an object"); |
454 | } | 461 | } |
455 | this.emit('casper.fill', selector, vals, submit); | 462 | this.emit('fill', selector, vals, submit); |
456 | var fillResults = this.evaluate(function(selector, values) { | 463 | var fillResults = this.evaluate(function(selector, values) { |
457 | return __utils__.fill(selector, values); | 464 | return __utils__.fill(selector, values); |
458 | }, { | 465 | }, { |
... | @@ -500,7 +507,7 @@ Casper.prototype.fill = function(selector, vals, submit) { | ... | @@ -500,7 +507,7 @@ Casper.prototype.fill = function(selector, vals, submit) { |
500 | */ | 507 | */ |
501 | Casper.prototype.forward = function(then) { | 508 | Casper.prototype.forward = function(then) { |
502 | return this.then(function() { | 509 | return this.then(function() { |
503 | this.emit('casper.forward'); | 510 | this.emit('forward'); |
504 | this.evaluate(function() { | 511 | this.evaluate(function() { |
505 | history.forward(); | 512 | history.forward(); |
506 | }); | 513 | }); |
... | @@ -549,7 +556,7 @@ Casper.prototype.getGlobal = function(name) { | ... | @@ -549,7 +556,7 @@ Casper.prototype.getGlobal = function(name) { |
549 | try { | 556 | try { |
550 | result.value = JSON.stringify(window[name]); | 557 | result.value = JSON.stringify(window[name]); |
551 | } catch (e) { | 558 | } catch (e) { |
552 | var message = 'Unable to JSON encode window.' + name + ': ' + e; | 559 | var message = f("Unable to JSON encode window.%s: %s", name, e); |
553 | __utils__.log(message, "error"); | 560 | __utils__.log(message, "error"); |
554 | result.error = message; | 561 | result.error = message; |
555 | } | 562 | } |
... | @@ -601,14 +608,14 @@ Casper.prototype.log = function(message, level, space) { | ... | @@ -601,14 +608,14 @@ Casper.prototype.log = function(message, level, space) { |
601 | if (level in this.logFormats && utils.isFunction(this.logFormats[level])) { | 608 | if (level in this.logFormats && utils.isFunction(this.logFormats[level])) { |
602 | message = this.logFormats[level](message, level, space); | 609 | message = this.logFormats[level](message, level, space); |
603 | } else { | 610 | } else { |
604 | var levelStr = this.colorizer.colorize('[' + level + ']', this.logStyles[level]); | 611 | var levelStr = this.colorizer.colorize(f('[%s]', level), this.logStyles[level]); |
605 | message = levelStr + ' [' + space + '] ' + message; | 612 | message = f('%s [%s] %s', levelStr, space, message); |
606 | } | 613 | } |
607 | if (this.options.verbose) { | 614 | if (this.options.verbose) { |
608 | this.echo(message); // direct output | 615 | this.echo(message); // direct output |
609 | } | 616 | } |
610 | this.result.log.push(entry); | 617 | this.result.log.push(entry); |
611 | this.emit('casper.log', entry); | 618 | this.emit('log', entry); |
612 | return this; | 619 | return this; |
613 | }; | 620 | }; |
614 | 621 | ||
... | @@ -642,10 +649,10 @@ Casper.prototype.open = function(location, options) { | ... | @@ -642,10 +649,10 @@ Casper.prototype.open = function(location, options) { |
642 | username: httpAuthMatch[1], | 649 | username: httpAuthMatch[1], |
643 | password: httpAuthMatch[2] | 650 | password: httpAuthMatch[2] |
644 | }; | 651 | }; |
645 | this.emit('casper.http.auth', httpAuth); | 652 | this.emit('http.auth', httpAuth); |
646 | this.setHttpAuth(httpAuth.username, httpAuth.password); | 653 | this.setHttpAuth(httpAuth.username, httpAuth.password); |
647 | } | 654 | } |
648 | this.emit('casper.open', location); | 655 | this.emit('open', location); |
649 | this.page.open(location); | 656 | this.page.open(location); |
650 | return this; | 657 | return this; |
651 | }; | 658 | }; |
... | @@ -695,8 +702,8 @@ Casper.prototype.run = function(onComplete, time) { | ... | @@ -695,8 +702,8 @@ Casper.prototype.run = function(onComplete, time) { |
695 | this.log("No steps defined, aborting", "error"); | 702 | this.log("No steps defined, aborting", "error"); |
696 | return this; | 703 | return this; |
697 | } | 704 | } |
698 | this.log("Running suite: " + this.steps.length + " step" + (this.steps.length > 1 ? "s" : ""), "info"); | 705 | this.log(f("Running suite: %d step%s", this.steps.length, this.steps.length > 1 ? "s" : ""), "info"); |
699 | this.emit('casper.run'); | 706 | this.emit('run'); |
700 | this.checker = setInterval(this.checkStep, (time ? time: 250), this, onComplete); | 707 | this.checker = setInterval(this.checkStep, (time ? time: 250), this, onComplete); |
701 | return this; | 708 | return this; |
702 | }; | 709 | }; |
... | @@ -708,16 +715,16 @@ Casper.prototype.run = function(onComplete, time) { | ... | @@ -708,16 +715,16 @@ Casper.prototype.run = function(onComplete, time) { |
708 | */ | 715 | */ |
709 | Casper.prototype.runStep = function(step) { | 716 | Casper.prototype.runStep = function(step) { |
710 | var skipLog = utils.isObject(step.options) && step.options.skipLog === true; | 717 | var skipLog = utils.isObject(step.options) && step.options.skipLog === true; |
711 | var stepInfo = "Step " + (this.step) + "/" + this.steps.length; | 718 | var stepInfo = f("Step %d/%d", this.step, this.steps.length); |
712 | var stepResult; | 719 | var stepResult; |
713 | if (!skipLog) { | 720 | if (!skipLog) { |
714 | this.log(stepInfo + ' ' + this.getCurrentUrl() + ' (HTTP ' + this.currentHTTPStatus + ')', "info"); | 721 | this.log(stepInfo + f('%s (HTTP %d)', this.getCurrentUrl(), this.currentHTTPStatus), "info"); |
715 | } | 722 | } |
716 | if (utils.isNumber(this.options.stepTimeout) && this.options.stepTimeout > 0) { | 723 | if (utils.isNumber(this.options.stepTimeout) && this.options.stepTimeout > 0) { |
717 | var stepTimeoutCheckInterval = setInterval(function(self, start, stepNum) { | 724 | var stepTimeoutCheckInterval = setInterval(function(self, start, stepNum) { |
718 | if (new Date().getTime() - start > self.options.stepTimeout) { | 725 | if (new Date().getTime() - start > self.options.stepTimeout) { |
719 | if (self.step == stepNum) { | 726 | if (self.step == stepNum) { |
720 | self.emit('casper.step.timeout'); | 727 | self.emit('step.timeout'); |
721 | if (utils.isFunction(self.options.onStepTimeout)) { | 728 | if (utils.isFunction(self.options.onStepTimeout)) { |
722 | self.options.onStepTimeout.call(self, self); | 729 | self.options.onStepTimeout.call(self, self); |
723 | } else { | 730 | } else { |
... | @@ -728,7 +735,7 @@ Casper.prototype.runStep = function(step) { | ... | @@ -728,7 +735,7 @@ Casper.prototype.runStep = function(step) { |
728 | } | 735 | } |
729 | }, this.options.stepTimeout, this, new Date().getTime(), this.step); | 736 | }, this.options.stepTimeout, this, new Date().getTime(), this.step); |
730 | } | 737 | } |
731 | this.emit('casper.step.start', step); | 738 | this.emit('step.start', step); |
732 | try { | 739 | try { |
733 | stepResult = step.call(this, this); | 740 | stepResult = step.call(this, this); |
734 | } catch (e) { | 741 | } catch (e) { |
... | @@ -742,8 +749,8 @@ Casper.prototype.runStep = function(step) { | ... | @@ -742,8 +749,8 @@ Casper.prototype.runStep = function(step) { |
742 | this.options.onStepComplete.call(this, this, stepResult); | 749 | this.options.onStepComplete.call(this, this, stepResult); |
743 | } | 750 | } |
744 | if (!skipLog) { | 751 | if (!skipLog) { |
745 | this.emit('casper.step.complete', stepResult); | 752 | this.emit('step.complete', stepResult); |
746 | this.log(stepInfo + ": done in " + (new Date().getTime() - this.startTime) + "ms.", "info"); | 753 | this.log(stepInfo + f(": done in %dms.", new Date().getTime() - this.startTime), "info"); |
747 | } | 754 | } |
748 | }; | 755 | }; |
749 | 756 | ||
... | @@ -775,7 +782,7 @@ Casper.prototype.setHttpAuth = function(username, password) { | ... | @@ -775,7 +782,7 @@ Casper.prototype.setHttpAuth = function(username, password) { |
775 | * @return Casper | 782 | * @return Casper |
776 | */ | 783 | */ |
777 | Casper.prototype.start = function(location, then) { | 784 | Casper.prototype.start = function(location, then) { |
778 | this.emit('casper.starting'); | 785 | this.emit('starting'); |
779 | this.log('Starting...', "info"); | 786 | this.log('Starting...', "info"); |
780 | this.startTime = new Date().getTime(); | 787 | this.startTime = new Date().getTime(); |
781 | this.history = []; | 788 | this.history = []; |
... | @@ -783,7 +790,7 @@ Casper.prototype.start = function(location, then) { | ... | @@ -783,7 +790,7 @@ Casper.prototype.start = function(location, then) { |
783 | this.step = 0; | 790 | this.step = 0; |
784 | // Option checks | 791 | // Option checks |
785 | if (this.logLevels.indexOf(this.options.logLevel) < 0) { | 792 | if (this.logLevels.indexOf(this.options.logLevel) < 0) { |
786 | this.log("Unknown log level '" + this.options.logLevel + "', defaulting to 'warning'", "warning"); | 793 | this.log(f("Unknown log level '%d', defaulting to 'warning'", this.options.logLevel), "warning"); |
787 | this.options.logLevel = "warning"; | 794 | this.options.logLevel = "warning"; |
788 | } | 795 | } |
789 | // WebPage | 796 | // WebPage |
... | @@ -802,19 +809,19 @@ Casper.prototype.start = function(location, then) { | ... | @@ -802,19 +809,19 @@ Casper.prototype.start = function(location, then) { |
802 | this.page.viewportSize = this.options.viewportSize; | 809 | this.page.viewportSize = this.options.viewportSize; |
803 | } | 810 | } |
804 | this.started = true; | 811 | this.started = true; |
805 | this.emit('casper.started'); | 812 | this.emit('started'); |
806 | if (utils.isNumber(this.options.timeout) && this.options.timeout > 0) { | 813 | if (utils.isNumber(this.options.timeout) && this.options.timeout > 0) { |
807 | this.log("Execution timeout set to " + this.options.timeout + 'ms', "info"); | 814 | this.log(f("Execution timeout set to %dms", this.options.timeout), "info"); |
808 | setTimeout(function(self) { | 815 | setTimeout(function(self) { |
809 | self.emit('casper.timeout'); | 816 | self.emit('timeout'); |
810 | if (utils.isFunction(self.options.onTimeout)) { | 817 | if (utils.isFunction(self.options.onTimeout)) { |
811 | self.options.onTimeout.call(self, self); | 818 | self.options.onTimeout.call(self, self); |
812 | } else { | 819 | } else { |
813 | self.die("Timeout of " + self.options.timeout + "ms exceeded, exiting."); | 820 | self.die(f("Timeout of %dms exceeded, exiting.", self.options.timeout)); |
814 | } | 821 | } |
815 | }, this.options.timeout, this); | 822 | }, this.options.timeout, this); |
816 | } | 823 | } |
817 | this.emit('casper.page.initialized'); | 824 | this.emit('page.initialized'); |
818 | if (utils.isFunction(this.options.onPageInitialized)) { | 825 | if (utils.isFunction(this.options.onPageInitialized)) { |
819 | this.log("Post-configuring WebPage instance", "debug"); | 826 | this.log("Post-configuring WebPage instance", "debug"); |
820 | this.options.onPageInitialized.call(this, this.page); | 827 | this.options.onPageInitialized.call(this, this.page); |
... | @@ -858,7 +865,7 @@ Casper.prototype.then = function(step) { | ... | @@ -858,7 +865,7 @@ Casper.prototype.then = function(step) { |
858 | } | 865 | } |
859 | this.steps.splice(insertIndex, 0, step); | 866 | this.steps.splice(insertIndex, 0, step); |
860 | } | 867 | } |
861 | this.emit('casper.step.added', step); | 868 | this.emit('step.added', step); |
862 | return this; | 869 | return this; |
863 | }; | 870 | }; |
864 | 871 | ||
... | @@ -939,13 +946,13 @@ Casper.prototype.viewport = function(width, height) { | ... | @@ -939,13 +946,13 @@ Casper.prototype.viewport = function(width, height) { |
939 | throw new Error("Casper must be started in order to set viewport at runtime"); | 946 | throw new Error("Casper must be started in order to set viewport at runtime"); |
940 | } | 947 | } |
941 | if (!utils.isNumber(width) || !utils.isNumber(height) || width <= 0 || height <= 0) { | 948 | if (!utils.isNumber(width) || !utils.isNumber(height) || width <= 0 || height <= 0) { |
942 | throw new Error("Invalid viewport width/height set: " + width + 'x' + height); | 949 | throw new Error(f("Invalid viewport: %dx%d", width, height)); |
943 | } | 950 | } |
944 | this.page.viewportSize = { | 951 | this.page.viewportSize = { |
945 | width: width, | 952 | width: width, |
946 | height: height | 953 | height: height |
947 | }; | 954 | }; |
948 | this.emit('casper.viewport.changed', [width, height]); | 955 | this.emit('viewport.changed', [width, height]); |
949 | return this; | 956 | return this; |
950 | }; | 957 | }; |
951 | 958 | ||
... | @@ -968,7 +975,7 @@ Casper.prototype.wait = function(timeout, then) { | ... | @@ -968,7 +975,7 @@ Casper.prototype.wait = function(timeout, then) { |
968 | return this.then(function(self) { | 975 | return this.then(function(self) { |
969 | self.waitStart(); | 976 | self.waitStart(); |
970 | setTimeout(function() { | 977 | setTimeout(function() { |
971 | self.log("wait() finished wating for " + timeout + "ms.", "info"); | 978 | self.log(f("wait() finished wating for %dms.", timeout), "info"); |
972 | if (then) { | 979 | if (then) { |
973 | then.call(self, self); | 980 | then.call(self, self); |
974 | } | 981 | } |
... | @@ -978,12 +985,12 @@ Casper.prototype.wait = function(timeout, then) { | ... | @@ -978,12 +985,12 @@ Casper.prototype.wait = function(timeout, then) { |
978 | }; | 985 | }; |
979 | 986 | ||
980 | Casper.prototype.waitStart = function() { | 987 | Casper.prototype.waitStart = function() { |
981 | this.emit('casper.wait.start'); | 988 | this.emit('wait.start'); |
982 | this.pendingWait = true; | 989 | this.pendingWait = true; |
983 | }; | 990 | }; |
984 | 991 | ||
985 | Casper.prototype.waitDone = function() { | 992 | Casper.prototype.waitDone = function() { |
986 | this.emit('casper.wait.done'); | 993 | this.emit('wait.done'); |
987 | this.pendingWait = false; | 994 | this.pendingWait = false; |
988 | }; | 995 | }; |
989 | 996 | ||
... | @@ -1015,15 +1022,15 @@ Casper.prototype.waitFor = function(testFx, then, onTimeout, timeout) { | ... | @@ -1015,15 +1022,15 @@ Casper.prototype.waitFor = function(testFx, then, onTimeout, timeout) { |
1015 | self.waitDone(); | 1022 | self.waitDone(); |
1016 | if (!condition) { | 1023 | if (!condition) { |
1017 | self.log("Casper.waitFor() timeout", "warning"); | 1024 | self.log("Casper.waitFor() timeout", "warning"); |
1018 | self.emit('casper.waitFor.timeout'); | 1025 | self.emit('waitFor.timeout'); |
1019 | if (utils.isFunction(onTimeout)) { | 1026 | if (utils.isFunction(onTimeout)) { |
1020 | onTimeout.call(self, self); | 1027 | onTimeout.call(self, self); |
1021 | } else { | 1028 | } else { |
1022 | self.die("Timeout of " + timeout + "ms expired, exiting.", "error"); | 1029 | self.die(f("Timeout of %dms expired, exiting.", timeout), "error"); |
1023 | } | 1030 | } |
1024 | clearInterval(interval); | 1031 | clearInterval(interval); |
1025 | } else { | 1032 | } else { |
1026 | self.log("waitFor() finished in " + (new Date().getTime() - start) + "ms.", "info"); | 1033 | self.log(f("waitFor() finished in %dms.", new Date().getTime() - start), "info"); |
1027 | if (then) { | 1034 | if (then) { |
1028 | self.then(then); | 1035 | self.then(then); |
1029 | } | 1036 | } |
... | @@ -1147,7 +1154,7 @@ function createPage(casper) { | ... | @@ -1147,7 +1154,7 @@ function createPage(casper) { |
1147 | } | 1154 | } |
1148 | page.onAlert = function(message) { | 1155 | page.onAlert = function(message) { |
1149 | casper.log('[alert] ' + message, "info", "remote"); | 1156 | casper.log('[alert] ' + message, "info", "remote"); |
1150 | casper.emit('casper.remote.alert', message); | 1157 | casper.emit('remote.alert', message); |
1151 | if (utils.isFunction(casper.options.onAlert)) { | 1158 | if (utils.isFunction(casper.options.onAlert)) { |
1152 | casper.options.onAlert.call(casper, casper, message); | 1159 | casper.options.onAlert.call(casper, casper, message); |
1153 | } | 1160 | } |
... | @@ -1159,23 +1166,23 @@ function createPage(casper) { | ... | @@ -1159,23 +1166,23 @@ function createPage(casper) { |
1159 | msg = test[2]; | 1166 | msg = test[2]; |
1160 | } | 1167 | } |
1161 | casper.log(msg, level, "remote"); | 1168 | casper.log(msg, level, "remote"); |
1162 | casper.emit('casper.remote.message', msg); | 1169 | casper.emit('remote.message', msg); |
1163 | }; | 1170 | }; |
1164 | page.onLoadStarted = function() { | 1171 | page.onLoadStarted = function() { |
1165 | casper.loadInProgress = true; | 1172 | casper.loadInProgress = true; |
1166 | casper.resources = []; | 1173 | casper.resources = []; |
1167 | casper.emit('casper.load.started'); | 1174 | casper.emit('load.started'); |
1168 | }; | 1175 | }; |
1169 | page.onLoadFinished = function(status) { | 1176 | page.onLoadFinished = function(status) { |
1170 | if (status !== "success") { | 1177 | if (status !== "success") { |
1171 | casper.emit('casper.load.failed', { | 1178 | casper.emit('load.failed', { |
1172 | status: status, | 1179 | status: status, |
1173 | http_status: casper.currentHTTPStatus, | 1180 | http_status: casper.currentHTTPStatus, |
1174 | url: casper.requestUrl | 1181 | url: casper.requestUrl |
1175 | }); | 1182 | }); |
1176 | var message = 'Loading resource failed with status=' + status; | 1183 | var message = 'Loading resource failed with status=' + status; |
1177 | if (casper.currentHTTPStatus) { | 1184 | if (casper.currentHTTPStatus) { |
1178 | message += ' (HTTP ' + casper.currentHTTPStatus + ')'; | 1185 | message += f(' (HTTP %d)', casper.currentHTTPStatus); |
1179 | } | 1186 | } |
1180 | message += ': ' + casper.requestUrl; | 1187 | message += ': ' + casper.requestUrl; |
1181 | casper.log(message, "warning"); | 1188 | casper.log(message, "warning"); |
... | @@ -1190,9 +1197,9 @@ function createPage(casper) { | ... | @@ -1190,9 +1197,9 @@ function createPage(casper) { |
1190 | for (var i = 0; i < casper.options.clientScripts.length; i++) { | 1197 | for (var i = 0; i < casper.options.clientScripts.length; i++) { |
1191 | var script = casper.options.clientScripts[i]; | 1198 | var script = casper.options.clientScripts[i]; |
1192 | if (casper.page.injectJs(script)) { | 1199 | if (casper.page.injectJs(script)) { |
1193 | casper.log('Automatically injected ' + script + ' client side', "debug"); | 1200 | casper.log(f('Automatically injected %s client side', script), "debug"); |
1194 | } else { | 1201 | } else { |
1195 | casper.log('Failed injecting ' + script + ' client side', "warning"); | 1202 | casper.log(f('Failed injecting %s client side', script), "warning"); |
1196 | } | 1203 | } |
1197 | } | 1204 | } |
1198 | } | 1205 | } |
... | @@ -1206,11 +1213,11 @@ function createPage(casper) { | ... | @@ -1206,11 +1213,11 @@ function createPage(casper) { |
1206 | } | 1213 | } |
1207 | // history | 1214 | // history |
1208 | casper.history.push(casper.getCurrentUrl()); | 1215 | casper.history.push(casper.getCurrentUrl()); |
1209 | casper.emit('casper.load.finished', status); | 1216 | casper.emit('load.finished', status); |
1210 | casper.loadInProgress = false; | 1217 | casper.loadInProgress = false; |
1211 | }; | 1218 | }; |
1212 | page.onResourceReceived = function(resource) { | 1219 | page.onResourceReceived = function(resource) { |
1213 | casper.emit('casper.resource.received', resource); | 1220 | casper.emit('resource.received', resource); |
1214 | if (utils.isFunction(casper.options.onResourceReceived)) { | 1221 | if (utils.isFunction(casper.options.onResourceReceived)) { |
1215 | casper.options.onResourceReceived.call(casper, casper, resource); | 1222 | casper.options.onResourceReceived.call(casper, casper, resource); |
1216 | } | 1223 | } |
... | @@ -1219,7 +1226,7 @@ function createPage(casper) { | ... | @@ -1219,7 +1226,7 @@ function createPage(casper) { |
1219 | } | 1226 | } |
1220 | if (resource.url === casper.requestUrl && resource.stage === "start") { | 1227 | if (resource.url === casper.requestUrl && resource.stage === "start") { |
1221 | casper.currentHTTPStatus = resource.status; | 1228 | casper.currentHTTPStatus = resource.status; |
1222 | casper.emit('casper.http.status.' + resource.status, resource); | 1229 | casper.emit('http.status.' + resource.status, resource); |
1223 | if (utils.isObject(casper.options.httpStatusHandlers) && | 1230 | if (utils.isObject(casper.options.httpStatusHandlers) && |
1224 | resource.status in casper.options.httpStatusHandlers && | 1231 | resource.status in casper.options.httpStatusHandlers && |
1225 | utils.isFunction(casper.options.httpStatusHandlers[resource.status])) { | 1232 | utils.isFunction(casper.options.httpStatusHandlers[resource.status])) { |
... | @@ -1229,11 +1236,11 @@ function createPage(casper) { | ... | @@ -1229,11 +1236,11 @@ function createPage(casper) { |
1229 | } | 1236 | } |
1230 | }; | 1237 | }; |
1231 | page.onResourceRequested = function(request) { | 1238 | page.onResourceRequested = function(request) { |
1232 | casper.emit('casper.resource.requested', request); | 1239 | casper.emit('resource.requested', request); |
1233 | if (utils.isFunction(casper.options.onResourceRequested)) { | 1240 | if (utils.isFunction(casper.options.onResourceRequested)) { |
1234 | casper.options.onResourceRequested.call(casper, casper, request); | 1241 | casper.options.onResourceRequested.call(casper, casper, request); |
1235 | } | 1242 | } |
1236 | }; | 1243 | }; |
1237 | casper.emit('casper.page.created', page); | 1244 | casper.emit('page.created', page); |
1238 | return page; | 1245 | return page; |
1239 | } | 1246 | } | ... | ... |
... | @@ -4,7 +4,9 @@ | ... | @@ -4,7 +4,9 @@ |
4 | * Documentation: http://n1k0.github.com/casperjs/ | 4 | * Documentation: http://n1k0.github.com/casperjs/ |
5 | * Repository: http://github.com/n1k0/casperjs | 5 | * Repository: http://github.com/n1k0/casperjs |
6 | * | 6 | * |
7 | * Copyright (c) 2011 Nicolas Perriault | 7 | * Copyright (c) 2011-2012 Nicolas Perriault |
8 | * | ||
9 | * Part of source code is Copyright Joyent, Inc. and other Node contributors. | ||
8 | * | 10 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | * copy of this software and associated documentation files (the "Software"), | 12 | * copy of this software and associated documentation files (the "Software"), | ... | ... |
... | @@ -4,7 +4,9 @@ | ... | @@ -4,7 +4,9 @@ |
4 | * Documentation: http://n1k0.github.com/casperjs/ | 4 | * Documentation: http://n1k0.github.com/casperjs/ |
5 | * Repository: http://github.com/n1k0/casperjs | 5 | * Repository: http://github.com/n1k0/casperjs |
6 | * | 6 | * |
7 | * Copyright (c) 2011 Nicolas Perriault | 7 | * Copyright (c) 2011-2012 Nicolas Perriault |
8 | * | ||
9 | * Part of source code is Copyright Joyent, Inc. and other Node contributors. | ||
8 | * | 10 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | * copy of this software and associated documentation files (the "Software"), | 12 | * copy of this software and associated documentation files (the "Software"), | ... | ... |
... | @@ -4,7 +4,9 @@ | ... | @@ -4,7 +4,9 @@ |
4 | * Documentation: http://n1k0.github.com/casperjs/ | 4 | * Documentation: http://n1k0.github.com/casperjs/ |
5 | * Repository: http://github.com/n1k0/casperjs | 5 | * Repository: http://github.com/n1k0/casperjs |
6 | * | 6 | * |
7 | * Copyright (c) 2011 Nicolas Perriault | 7 | * Copyright (c) 2011-2012 Nicolas Perriault |
8 | * | ||
9 | * Part of source code is Copyright Joyent, Inc. and other Node contributors. | ||
8 | * | 10 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | * copy of this software and associated documentation files (the "Software"), | 12 | * copy of this software and associated documentation files (the "Software"), | ... | ... |
... | @@ -4,7 +4,9 @@ | ... | @@ -4,7 +4,9 @@ |
4 | * Documentation: http://n1k0.github.com/casperjs/ | 4 | * Documentation: http://n1k0.github.com/casperjs/ |
5 | * Repository: http://github.com/n1k0/casperjs | 5 | * Repository: http://github.com/n1k0/casperjs |
6 | * | 6 | * |
7 | * Copyright (c) 2011 Nicolas Perriault | 7 | * Copyright (c) 2011-2012 Nicolas Perriault |
8 | * | ||
9 | * Part of source code is Copyright Joyent, Inc. and other Node contributors. | ||
8 | * | 10 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | * copy of this software and associated documentation files (the "Software"), | 12 | * copy of this software and associated documentation files (the "Software"), | ... | ... |
1 | /*! | ||
2 | * Casper is a navigation utility for PhantomJS. | ||
3 | * | ||
4 | * Documentation: http://n1k0.github.com/casperjs/ | ||
5 | * Repository: http://github.com/n1k0/casperjs | ||
6 | * | ||
7 | * Copyright (c) 2011-2012 Nicolas Perriault | ||
8 | * | ||
9 | * Part of source code is Copyright Joyent, Inc. and other Node contributors. | ||
10 | * | ||
11 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
12 | * copy of this software and associated documentation files (the "Software"), | ||
13 | * to deal in the Software without restriction, including without limitation | ||
14 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
15 | * and/or sell copies of the Software, and to permit persons to whom the | ||
16 | * Software is furnished to do so, subject to the following conditions: | ||
17 | * | ||
18 | * The above copyright notice and this permission notice shall be included | ||
19 | * in all copies or substantial portions of the Software. | ||
20 | * | ||
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
22 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
24 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
27 | * DEALINGS IN THE SOFTWARE. | ||
28 | * | ||
29 | */ | ||
30 | |||
1 | var utils = require('utils'); | 31 | var utils = require('utils'); |
2 | 32 | ||
3 | exports.create = function(casper) { | 33 | exports.create = function(casper) { |
... | @@ -35,14 +65,14 @@ var Mouse = function(casper) { | ... | @@ -35,14 +65,14 @@ var Mouse = function(casper) { |
35 | if (!utils.isString(selector)) { | 65 | if (!utils.isString(selector)) { |
36 | throw new Error('No valid CSS selector passed: ' + selector); | 66 | throw new Error('No valid CSS selector passed: ' + selector); |
37 | } | 67 | } |
38 | casper.page.sendEvent.apply(casper.page, [type].concat(computeCenter(selector))) | 68 | casper.page.sendEvent.apply(casper.page, [type].concat(computeCenter(selector))); |
39 | break; | 69 | break; |
40 | case 2: | 70 | case 2: |
41 | // coordinates | 71 | // coordinates |
42 | if (!utils.isNumber(args[1]) || !utils.isNumber(args[2])) { | 72 | if (!utils.isNumber(args[1]) || !utils.isNumber(args[2])) { |
43 | throw new Error('No valid coordinates passed'); | 73 | throw new Error('No valid coordinates passed'); |
44 | } | 74 | } |
45 | casper.page.sendEvent(type, args[0], args[1]) | 75 | casper.page.sendEvent(type, args[0], args[1]); |
46 | break; | 76 | break; |
47 | default: | 77 | default: |
48 | throw new Error('Too many arguments'); | 78 | throw new Error('Too many arguments'); |
... | @@ -51,18 +81,18 @@ var Mouse = function(casper) { | ... | @@ -51,18 +81,18 @@ var Mouse = function(casper) { |
51 | 81 | ||
52 | this.click = function() { | 82 | this.click = function() { |
53 | processEvent('click', arguments); | 83 | processEvent('click', arguments); |
54 | }, | 84 | }; |
55 | 85 | ||
56 | this.down = function() { | 86 | this.down = function() { |
57 | processEvent('mousedown', arguments); | 87 | processEvent('mousedown', arguments); |
58 | }, | 88 | }; |
59 | 89 | ||
60 | this.move = function() { | 90 | this.move = function() { |
61 | processEvent('mousemove', arguments); | 91 | processEvent('mousemove', arguments); |
62 | }, | 92 | }; |
63 | 93 | ||
64 | this.up = function() { | 94 | this.up = function() { |
65 | processEvent('mouseup', arguments); | 95 | processEvent('mouseup', arguments); |
66 | } | 96 | }; |
67 | }; | 97 | }; |
68 | exports.Mouse = Mouse; | 98 | exports.Mouse = Mouse; | ... | ... |
... | @@ -4,7 +4,9 @@ | ... | @@ -4,7 +4,9 @@ |
4 | * Documentation: http://n1k0.github.com/casperjs/ | 4 | * Documentation: http://n1k0.github.com/casperjs/ |
5 | * Repository: http://github.com/n1k0/casperjs | 5 | * Repository: http://github.com/n1k0/casperjs |
6 | * | 6 | * |
7 | * Copyright (c) 2011 Nicolas Perriault | 7 | * Copyright (c) 2011-2012 Nicolas Perriault |
8 | * | ||
9 | * Part of source code is Copyright Joyent, Inc. and other Node contributors. | ||
8 | * | 10 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | * copy of this software and associated documentation files (the "Software"), | 12 | * copy of this software and associated documentation files (the "Software"), | ... | ... |
... | @@ -4,7 +4,9 @@ | ... | @@ -4,7 +4,9 @@ |
4 | * Documentation: http://n1k0.github.com/casperjs/ | 4 | * Documentation: http://n1k0.github.com/casperjs/ |
5 | * Repository: http://github.com/n1k0/casperjs | 5 | * Repository: http://github.com/n1k0/casperjs |
6 | * | 6 | * |
7 | * Copyright (c) 2011 Nicolas Perriault | 7 | * Copyright (c) 2011-2012 Nicolas Perriault |
8 | * | ||
9 | * Part of source code is Copyright Joyent, Inc. and other Node contributors. | ||
8 | * | 10 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | * copy of this software and associated documentation files (the "Software"), | 12 | * copy of this software and associated documentation files (the "Software"), |
... | @@ -86,6 +88,75 @@ function fillBlanks(text, pad) { | ... | @@ -86,6 +88,75 @@ function fillBlanks(text, pad) { |
86 | exports.fillBlanks = fillBlanks; | 88 | exports.fillBlanks = fillBlanks; |
87 | 89 | ||
88 | /** | 90 | /** |
91 | * Formats a string with passed parameters. Ported from nodejs `util.format()`. | ||
92 | * | ||
93 | * @return String | ||
94 | */ | ||
95 | function format(f) { | ||
96 | var i; | ||
97 | if (typeof f !== 'string') { | ||
98 | var objects = []; | ||
99 | for (i = 0; i < arguments.length; i++) { | ||
100 | objects.push(inspect(arguments[i])); | ||
101 | } | ||
102 | return objects.join(' '); | ||
103 | } | ||
104 | i = 1; | ||
105 | var args = arguments; | ||
106 | var len = args.length; | ||
107 | var str = String(f).replace(/%[sdj%]/g, function(x) { | ||
108 | if (i >= len) return x; | ||
109 | switch (x) { | ||
110 | case '%s': | ||
111 | return String(args[i++]); | ||
112 | case '%d': | ||
113 | return Number(args[i++]); | ||
114 | case '%j': | ||
115 | return JSON.stringify(args[i++]); | ||
116 | case '%%': | ||
117 | return '%'; | ||
118 | default: | ||
119 | return x; | ||
120 | } | ||
121 | }); | ||
122 | for (var x = args[i]; i < len; x = args[++i]) { | ||
123 | if (x === null || typeof x !== 'object') { | ||
124 | str += ' ' + x; | ||
125 | } else { | ||
126 | str += ' ' + inspect(x); | ||
127 | } | ||
128 | } | ||
129 | return str; | ||
130 | } | ||
131 | exports.format = format; | ||
132 | |||
133 | /** | ||
134 | * Inherit the prototype methods from one constructor into another. | ||
135 | * | ||
136 | * The Function.prototype.inherits from lang.js rewritten as a standalone | ||
137 | * function (not on Function.prototype). NOTE: If this file is to be loaded | ||
138 | * during bootstrapping this function needs to be revritten using some native | ||
139 | * functions as prototype setup using normal JavaScript does not work as | ||
140 | * expected during bootstrapping (see mirror.js in r114903). | ||
141 | * | ||
142 | * @param {function} ctor Constructor function which needs to inherit the | ||
143 | * prototype. | ||
144 | * @param {function} superCtor Constructor function to inherit prototype from. | ||
145 | */ | ||
146 | function inherits(ctor, superCtor) { | ||
147 | ctor.super_ = superCtor; | ||
148 | ctor.prototype = Object.create(superCtor.prototype, { | ||
149 | constructor: { | ||
150 | value: ctor, | ||
151 | enumerable: false, | ||
152 | writable: true, | ||
153 | configurable: true | ||
154 | } | ||
155 | }); | ||
156 | } | ||
157 | exports.inherits = inherits; | ||
158 | |||
159 | /** | ||
89 | * Checks if value is a javascript Array | 160 | * Checks if value is a javascript Array |
90 | * | 161 | * |
91 | * @param mixed value | 162 | * @param mixed value |
... | @@ -243,14 +314,14 @@ exports.mergeObjects = mergeObjects; | ... | @@ -243,14 +314,14 @@ exports.mergeObjects = mergeObjects; |
243 | * @return HTMLElement | 314 | * @return HTMLElement |
244 | */ | 315 | */ |
245 | function node(name, attributes) { | 316 | function node(name, attributes) { |
246 | var node = document.createElement(name); | 317 | var _node = document.createElement(name); |
247 | for (var attrName in attributes) { | 318 | for (var attrName in attributes) { |
248 | var value = attributes[attrName]; | 319 | var value = attributes[attrName]; |
249 | if (attributes.hasOwnProperty(attrName) && isString(attrName)) { | 320 | if (attributes.hasOwnProperty(attrName) && isString(attrName)) { |
250 | node.setAttribute(attrName, value); | 321 | _node.setAttribute(attrName, value); |
251 | } | 322 | } |
252 | } | 323 | } |
253 | return node; | 324 | return _node; |
254 | } | 325 | } |
255 | exports.node = node; | 326 | exports.node = node; |
256 | 327 | ||
... | @@ -269,28 +340,3 @@ function serialize(value) { | ... | @@ -269,28 +340,3 @@ function serialize(value) { |
269 | return JSON.stringify(value, null, 4); | 340 | return JSON.stringify(value, null, 4); |
270 | } | 341 | } |
271 | exports.serialize = serialize; | 342 | exports.serialize = serialize; |
272 | |||
273 | /** | ||
274 | * Inherit the prototype methods from one constructor into another. | ||
275 | * | ||
276 | * The Function.prototype.inherits from lang.js rewritten as a standalone | ||
277 | * function (not on Function.prototype). NOTE: If this file is to be loaded | ||
278 | * during bootstrapping this function needs to be revritten using some native | ||
279 | * functions as prototype setup using normal JavaScript does not work as | ||
280 | * expected during bootstrapping (see mirror.js in r114903). | ||
281 | * | ||
282 | * @param {function} ctor Constructor function which needs to inherit the | ||
283 | * prototype. | ||
284 | * @param {function} superCtor Constructor function to inherit prototype from. | ||
285 | */ | ||
286 | exports.inherits = function(ctor, superCtor) { | ||
287 | ctor.super_ = superCtor; | ||
288 | ctor.prototype = Object.create(superCtor.prototype, { | ||
289 | constructor: { | ||
290 | value: ctor, | ||
291 | enumerable: false, | ||
292 | writable: true, | ||
293 | configurable: true | ||
294 | } | ||
295 | }); | ||
296 | }; | ... | ... |
... | @@ -4,7 +4,9 @@ | ... | @@ -4,7 +4,9 @@ |
4 | * Documentation: http://n1k0.github.com/casperjs/ | 4 | * Documentation: http://n1k0.github.com/casperjs/ |
5 | * Repository: http://github.com/n1k0/casperjs | 5 | * Repository: http://github.com/n1k0/casperjs |
6 | * | 6 | * |
7 | * Copyright (c) 2011 Nicolas Perriault | 7 | * Copyright (c) 2011-2012 Nicolas Perriault |
8 | * | ||
9 | * Part of source code is Copyright Joyent, Inc. and other Node contributors. | ||
8 | * | 10 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | * copy of this software and associated documentation files (the "Software"), | 12 | * copy of this software and associated documentation files (the "Software"), | ... | ... |
1 | casper = require('casper').create() | 1 | casper = require('casper').create() |
2 | 2 | ||
3 | casper.on "casper.http.status.200", (resource) -> | 3 | casper.on "http.status.200", (resource) -> |
4 | casper.echo "#{resource.url} is OK", "INFO" | 4 | casper.echo "#{resource.url} is OK", "INFO" |
5 | 5 | ||
6 | casper.on "casper.http.status.301", (resource) -> | 6 | casper.on "http.status.301", (resource) -> |
7 | casper.echo "#{resource.url} is permanently redirected", "PARAMETER" | 7 | casper.echo "#{resource.url} is permanently redirected", "PARAMETER" |
8 | 8 | ||
9 | casper.on "casper.http.status.302", (resource) -> | 9 | casper.on "http.status.302", (resource) -> |
10 | casper.echo "#{resource.url} is temporarily redirected", "PARAMETER" | 10 | casper.echo "#{resource.url} is temporarily redirected", "PARAMETER" |
11 | 11 | ||
12 | casper.on "casper.http.status.404", (resource) -> | 12 | casper.on "http.status.404", (resource) -> |
13 | casper.echo "#{resource.url} is not found", "COMMENT" | 13 | casper.echo "#{resource.url} is not found", "COMMENT" |
14 | 14 | ||
15 | casper.on "casper.http.status.500", (resource) -> | 15 | casper.on "http.status.500", (resource) -> |
16 | casper.echo "#{resource.url} is in error", "ERROR" | 16 | casper.echo "#{resource.url} is in error", "ERROR" |
17 | 17 | ||
18 | links = [ | 18 | links = [ | ... | ... |
-
Please register or sign in to post a comment