Commit 89a3092c 89a3092c465ae136beafa6d21a41f93e785294df by Nicolas Perriault

added format() helper; removed 'casper.' suffix in event names

1 parent 18b1423a
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"),
......
...@@ -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 = [
......