Commit 55103a57 55103a57e0560d4687d049ec38701be1f02e6d41 by Nicolas Perriault

fixes #148 - broken utils.isWebPage()

1 parent 0245dcfb
...@@ -5,7 +5,8 @@ XXXX-XX-XX, v0.6.11 ...@@ -5,7 +5,8 @@ XXXX-XX-XX, v0.6.11
5 ------------------- 5 -------------------
6 6
7 - closed [#132](https://github.com/n1k0/casperjs/issues/132) - added ability to include js/coffee files using a dedicated option when using the `casper test` command 7 - closed [#132](https://github.com/n1k0/casperjs/issues/132) - added ability to include js/coffee files using a dedicated option when using the `casper test` command
8 - fixes [#140](https://github.com/n1k0/casperjs/issues/140) - `casper test` now resolves local paths urls 8 - fixed [#140](https://github.com/n1k0/casperjs/issues/140) - `casper test` now resolves local paths urls
9 - fixed [#148](https://github.com/n1k0/casperjs/issues/148) - `utils.isWebPage()` was broken
9 - closed [#144](https://github.com/n1k0/casperjs/issues/144) - added a `safeLogs` option to blur password values in debug logs. **This option is set to `true` by default.** 10 - closed [#144](https://github.com/n1k0/casperjs/issues/144) - added a `safeLogs` option to blur password values in debug logs. **This option is set to `true` by default.**
10 - added [`Casper.userAgent()`](http://casperjs.org/api.html#casper.userAgent) to ease a more dynamic setting of user-agent string 11 - added [`Casper.userAgent()`](http://casperjs.org/api.html#casper.userAgent) to ease a more dynamic setting of user-agent string
11 - added `Tester.assertTitleMatch()` method 12 - added `Tester.assertTitleMatch()` method
......
1 Subproject commit ed91017f48fd77e8c3a268ecdbb78adac7bce0b5 1 Subproject commit 76cc32f262a3ef924a67f33bb17432e3a5c33e03
......
...@@ -400,6 +400,8 @@ Casper.prototype.die = function die(message, status) { ...@@ -400,6 +400,8 @@ Casper.prototype.die = function die(message, status) {
400 * 400 *
401 * @param String url The url of the resource to download 401 * @param String url The url of the resource to download
402 * @param String targetPath The destination file path 402 * @param String targetPath The destination file path
403 * @param String method The HTTP method to use (default: GET)
404 * @param String data Optional data to pass performing the request
403 * @return Casper 405 * @return Casper
404 */ 406 */
405 Casper.prototype.download = function download(url, targetPath, method, data) { 407 Casper.prototype.download = function download(url, targetPath, method, data) {
......
...@@ -100,7 +100,7 @@ var Tester = function Tester(casper, options) { ...@@ -100,7 +100,7 @@ var Tester = function Tester(casper, options) {
100 if (failure.details) { 100 if (failure.details) {
101 this.comment(' details: ' + failure.details); 101 this.comment(' details: ' + failure.details);
102 } 102 }
103 if (failure.values && Object.keys(failure.values).length > 0) { 103 if (utils.isObject(failure.values) && Object.keys(failure.values).length > 0) {
104 for (var name in failure.values) { 104 for (var name in failure.values) {
105 this.comment(' ' + name + ': ' + utils.serialize(failure.values[name])); 105 this.comment(' ' + name + ': ' + utils.serialize(failure.values[name]));
106 } 106 }
......
...@@ -30,7 +30,10 @@ ...@@ -30,7 +30,10 @@
30 30
31 /*global CasperError console exports phantom require*/ 31 /*global CasperError console exports phantom require*/
32 32
33 /** 33 (function(exports) {
34 "use strict";
35
36 /**
34 * Provides a better typeof operator equivalent, able to retrieve the array 37 * Provides a better typeof operator equivalent, able to retrieve the array
35 * type. 38 * type.
36 * 39 *
...@@ -38,68 +41,63 @@ ...@@ -38,68 +41,63 @@
38 * @return String 41 * @return String
39 * @see http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/ 42 * @see http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
40 */ 43 */
41 function betterTypeOf(input) { 44 function betterTypeOf(input) {
42 "use strict";
43 try { 45 try {
44 return Object.prototype.toString.call(input).match(/^\[object\s(.*)\]$/)[1].toLowerCase(); 46 return Object.prototype.toString.call(input).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
45 } catch (e) { 47 } catch (e) {
46 return typeof input; 48 return typeof input;
47 } 49 }
48 } 50 }
49 exports.betterTypeOf = betterTypeOf; 51 exports.betterTypeOf = betterTypeOf;
50 52
51 /** 53 /**
52 * Dumps a JSON representation of passed value to the console. Used for 54 * Dumps a JSON representation of passed value to the console. Used for
53 * debugging purpose only. 55 * debugging purpose only.
54 * 56 *
55 * @param Mixed value 57 * @param Mixed value
56 */ 58 */
57 function dump(value) { 59 function dump(value) {
58 "use strict";
59 console.log(serialize(value)); 60 console.log(serialize(value));
60 } 61 }
61 exports.dump = dump; 62 exports.dump = dump;
62 63
63 /** 64 /**
64 * Returns the file extension in lower case. 65 * Returns the file extension in lower case.
65 * 66 *
66 * @param String file File path 67 * @param String file File path
67 * @return string 68 * @return string
68 */ 69 */
69 function fileExt(file) { 70 function fileExt(file) {
70 "use strict";
71 try { 71 try {
72 return file.split('.').pop().toLowerCase().trim(); 72 return file.split('.').pop().toLowerCase().trim();
73 } catch(e) { 73 } catch(e) {
74 return ''; 74 return '';
75 } 75 }
76 } 76 }
77 exports.fileExt = fileExt; 77 exports.fileExt = fileExt;
78 78
79 /** 79 /**
80 * Takes a string and append blanks until the pad value is reached. 80 * Takes a string and append blanks until the pad value is reached.
81 * 81 *
82 * @param String text 82 * @param String text
83 * @param Number pad Pad value (optional; default: 80) 83 * @param Number pad Pad value (optional; default: 80)
84 * @return String 84 * @return String
85 */ 85 */
86 function fillBlanks(text, pad) { 86 function fillBlanks(text, pad) {
87 "use strict";
88 pad = pad || 80; 87 pad = pad || 80;
89 if (text.length < pad) { 88 if (text.length < pad) {
90 text += new Array(pad - text.length + 1).join(' '); 89 text += new Array(pad - text.length + 1).join(' ');
91 } 90 }
92 return text; 91 return text;
93 } 92 }
94 exports.fillBlanks = fillBlanks; 93 exports.fillBlanks = fillBlanks;
95 94
96 /** 95 /**
97 * Formats a string with passed parameters. Ported from nodejs `util.format()`. 96 * Formats a string with passed parameters. Ported from nodejs `util.format()`.
98 * 97 *
99 * @return String 98 * @return String
100 */ 99 */
101 function format(f) { 100 function format(f) {
102 "use strict";
103 var i = 1; 101 var i = 1;
104 var args = arguments; 102 var args = arguments;
105 var len = args.length; 103 var len = args.length;
...@@ -126,18 +124,17 @@ function format(f) { ...@@ -126,18 +124,17 @@ function format(f) {
126 } 124 }
127 } 125 }
128 return str; 126 return str;
129 } 127 }
130 exports.format = format; 128 exports.format = format;
131 129
132 /** 130 /**
133 * Inherit the prototype methods from one constructor into another. 131 * Inherit the prototype methods from one constructor into another.
134 * 132 *
135 * @param {function} ctor Constructor function which needs to inherit the 133 * @param {function} ctor Constructor function which needs to inherit the
136 * prototype. 134 * prototype.
137 * @param {function} superCtor Constructor function to inherit prototype from. 135 * @param {function} superCtor Constructor function to inherit prototype from.
138 */ 136 */
139 function inherits(ctor, superCtor) { 137 function inherits(ctor, superCtor) {
140 "use strict";
141 ctor.super_ = ctor.__super__ = superCtor; 138 ctor.super_ = ctor.__super__ = superCtor;
142 ctor.prototype = Object.create(superCtor.prototype, { 139 ctor.prototype = Object.create(superCtor.prototype, {
143 constructor: { 140 constructor: {
...@@ -147,122 +144,114 @@ function inherits(ctor, superCtor) { ...@@ -147,122 +144,114 @@ function inherits(ctor, superCtor) {
147 configurable: true 144 configurable: true
148 } 145 }
149 }); 146 });
150 } 147 }
151 exports.inherits = inherits; 148 exports.inherits = inherits;
152 149
153 /** 150 /**
154 * Checks if value is a javascript Array 151 * Checks if value is a javascript Array
155 * 152 *
156 * @param mixed value 153 * @param mixed value
157 * @return Boolean 154 * @return Boolean
158 */ 155 */
159 function isArray(value) { 156 function isArray(value) {
160 "use strict";
161 return Array.isArray(value) || isType(value, "array"); 157 return Array.isArray(value) || isType(value, "array");
162 } 158 }
163 exports.isArray = isArray; 159 exports.isArray = isArray;
164 160
165 /** 161 /**
166 * Checks if passed argument is an instance of Capser object. 162 * Checks if passed argument is an instance of Capser object.
167 * 163 *
168 * @param mixed value 164 * @param mixed value
169 * @return Boolean 165 * @return Boolean
170 */ 166 */
171 function isCasperObject(value) { 167 function isCasperObject(value) {
172 "use strict";
173 return value instanceof require('casper').Casper; 168 return value instanceof require('casper').Casper;
174 } 169 }
175 exports.isCasperObject = isCasperObject; 170 exports.isCasperObject = isCasperObject;
176 171
177 /** 172 /**
178 * Checks if value is a phantomjs clipRect-compatible object 173 * Checks if value is a phantomjs clipRect-compatible object
179 * 174 *
180 * @param mixed value 175 * @param mixed value
181 * @return Boolean 176 * @return Boolean
182 */ 177 */
183 function isClipRect(value) { 178 function isClipRect(value) {
184 "use strict";
185 return isType(value, "cliprect") || ( 179 return isType(value, "cliprect") || (
186 isObject(value) && 180 isObject(value) &&
187 isNumber(value.top) && isNumber(value.left) && 181 isNumber(value.top) && isNumber(value.left) &&
188 isNumber(value.width) && isNumber(value.height) 182 isNumber(value.width) && isNumber(value.height)
189 ); 183 );
190 } 184 }
191 exports.isClipRect = isClipRect; 185 exports.isClipRect = isClipRect;
192 186
193 /** 187 /**
194 * Checks if value is a javascript Function 188 * Checks if value is a javascript Function
195 * 189 *
196 * @param mixed value 190 * @param mixed value
197 * @return Boolean 191 * @return Boolean
198 */ 192 */
199 function isFunction(value) { 193 function isFunction(value) {
200 "use strict";
201 return isType(value, "function"); 194 return isType(value, "function");
202 } 195 }
203 exports.isFunction = isFunction; 196 exports.isFunction = isFunction;
204 197
205 /** 198 /**
206 * Checks if a file is apparently javascript compatible (.js or .coffee). 199 * Checks if a file is apparently javascript compatible (.js or .coffee).
207 * 200 *
208 * @param String file Path to the file to test 201 * @param String file Path to the file to test
209 * @return Boolean 202 * @return Boolean
210 */ 203 */
211 function isJsFile(file) { 204 function isJsFile(file) {
212 "use strict";
213 var ext = fileExt(file); 205 var ext = fileExt(file);
214 return isString(ext, "string") && ['js', 'coffee'].indexOf(ext) !== -1; 206 return isString(ext, "string") && ['js', 'coffee'].indexOf(ext) !== -1;
215 } 207 }
216 exports.isJsFile = isJsFile; 208 exports.isJsFile = isJsFile;
217 209
218 /** 210 /**
219 * Checks if the provided value is null 211 * Checks if the provided value is null
220 * 212 *
221 * @return Boolean 213 * @return Boolean
222 */ 214 */
223 function isNull(value) { 215 function isNull(value) {
224 "use strict";
225 return isType(value, "null"); 216 return isType(value, "null");
226 } 217 }
227 exports.isNull = isNull; 218 exports.isNull = isNull;
228 219
229 /** 220 /**
230 * Checks if value is a javascript Number 221 * Checks if value is a javascript Number
231 * 222 *
232 * @param mixed value 223 * @param mixed value
233 * @return Boolean 224 * @return Boolean
234 */ 225 */
235 function isNumber(value) { 226 function isNumber(value) {
236 "use strict";
237 return isType(value, "number"); 227 return isType(value, "number");
238 } 228 }
239 exports.isNumber = isNumber; 229 exports.isNumber = isNumber;
240 230
241 /** 231 /**
242 * Checks if value is a javascript Object 232 * Checks if value is a javascript Object
243 * 233 *
244 * @param mixed value 234 * @param mixed value
245 * @return Boolean 235 * @return Boolean
246 */ 236 */
247 function isObject(value) { 237 function isObject(value) {
248 "use strict"; 238 var objectTypes = ["array", "object", "qtruntimeobject"];
249 return isType(value, "object"); 239 return objectTypes.indexOf(betterTypeOf(value)) >= 0;
250 } 240 }
251 exports.isObject = isObject; 241 exports.isObject = isObject;
252 242
253 /** 243 /**
254 * Checks if value is a javascript String 244 * Checks if value is a javascript String
255 * 245 *
256 * @param mixed value 246 * @param mixed value
257 * @return Boolean 247 * @return Boolean
258 */ 248 */
259 function isString(value) { 249 function isString(value) {
260 "use strict";
261 return isType(value, "string"); 250 return isType(value, "string");
262 } 251 }
263 exports.isString = isString; 252 exports.isString = isString;
264 253
265 /** 254 /**
266 * Shorthands for checking if a value is of the given type. Can check for 255 * Shorthands for checking if a value is of the given type. Can check for
267 * arrays. 256 * arrays.
268 * 257 *
...@@ -270,50 +259,43 @@ exports.isString = isString; ...@@ -270,50 +259,43 @@ exports.isString = isString;
270 * @param String typeName The type name ("string", "number", "function", etc.) 259 * @param String typeName The type name ("string", "number", "function", etc.)
271 * @return Boolean 260 * @return Boolean
272 */ 261 */
273 function isType(what, typeName) { 262 function isType(what, typeName) {
274 "use strict";
275 if (typeof typeName !== "string" || !typeName) { 263 if (typeof typeName !== "string" || !typeName) {
276 throw new CasperError("You must pass isType() a typeName string"); 264 throw new CasperError("You must pass isType() a typeName string");
277 } 265 }
278 return betterTypeOf(what).toLowerCase() === typeName.toLowerCase(); 266 return betterTypeOf(what).toLowerCase() === typeName.toLowerCase();
279 } 267 }
280 exports.isType = isType; 268 exports.isType = isType;
281 269
282 /** 270 /**
283 * Checks if the provided value is undefined 271 * Checks if the provided value is undefined
284 * 272 *
285 * @return Boolean 273 * @return Boolean
286 */ 274 */
287 function isUndefined(value) { 275 function isUndefined(value) {
288 "use strict";
289 return isType(value, "undefined"); 276 return isType(value, "undefined");
290 } 277 }
291 exports.isUndefined = isUndefined; 278 exports.isUndefined = isUndefined;
292 279
293 /** 280 /**
294 * Checks if the provided var is a WebPage instance 281 * Checks if the provided var is a WebPage instance
295 * 282 *
296 * @param mixed what 283 * @param mixed what
297 * @return Boolean 284 * @return Boolean
298 */ 285 */
299 function isWebPage(what) { 286 function isWebPage(what) {
300 "use strict"; 287 return betterTypeOf(what) === "qtruntimeobject" && what.objectName === 'WebPage';
301 if (!what || !isObject(what)) {
302 return false;
303 } 288 }
304 return what.toString().indexOf('WebPage(') === 0; 289 exports.isWebPage = isWebPage;
305 }
306 exports.isWebPage = isWebPage;
307 290
308 /** 291 /**
309 * Object recursive merging utility. 292 * Object recursive merging utility.
310 * 293 *
311 * @param Object origin the origin object 294 * @param Object origin the origin object
312 * @param Object add the object to merge data into origin 295 * @param Object add the object to merge data into origin
313 * @return Object 296 * @return Object
314 */ 297 */
315 function mergeObjects(origin, add) { 298 function mergeObjects(origin, add) {
316 "use strict";
317 for (var p in add) { 299 for (var p in add) {
318 try { 300 try {
319 if (add[p].constructor === Object) { 301 if (add[p].constructor === Object) {
...@@ -326,18 +308,17 @@ function mergeObjects(origin, add) { ...@@ -326,18 +308,17 @@ function mergeObjects(origin, add) {
326 } 308 }
327 } 309 }
328 return origin; 310 return origin;
329 } 311 }
330 exports.mergeObjects = mergeObjects; 312 exports.mergeObjects = mergeObjects;
331 313
332 /** 314 /**
333 * Creates an (SG|X)ML node element. 315 * Creates an (SG|X)ML node element.
334 * 316 *
335 * @param String name The node name 317 * @param String name The node name
336 * @param Object attributes Optional attributes 318 * @param Object attributes Optional attributes
337 * @return HTMLElement 319 * @return HTMLElement
338 */ 320 */
339 function node(name, attributes) { 321 function node(name, attributes) {
340 "use strict";
341 var _node = document.createElement(name); 322 var _node = document.createElement(name);
342 for (var attrName in attributes) { 323 for (var attrName in attributes) {
343 var value = attributes[attrName]; 324 var value = attributes[attrName];
...@@ -346,27 +327,26 @@ function node(name, attributes) { ...@@ -346,27 +327,26 @@ function node(name, attributes) {
346 } 327 }
347 } 328 }
348 return _node; 329 return _node;
349 } 330 }
350 exports.node = node; 331 exports.node = node;
351 332
352 /** 333 /**
353 * Serializes a value using JSON. 334 * Serializes a value using JSON.
354 * 335 *
355 * @param Mixed value 336 * @param Mixed value
356 * @return String 337 * @return String
357 */ 338 */
358 function serialize(value) { 339 function serialize(value) {
359 "use strict";
360 if (isArray(value)) { 340 if (isArray(value)) {
361 value = value.map(function _map(prop) { 341 value = value.map(function _map(prop) {
362 return isFunction(prop) ? prop.toString().replace(/\s{2,}/, '') : prop; 342 return isFunction(prop) ? prop.toString().replace(/\s{2,}/, '') : prop;
363 }); 343 });
364 } 344 }
365 return JSON.stringify(value, null, 4); 345 return JSON.stringify(value, null, 4);
366 } 346 }
367 exports.serialize = serialize; 347 exports.serialize = serialize;
368 348
369 /** 349 /**
370 * Returns unique values from an array. 350 * Returns unique values from an array.
371 * 351 *
372 * Note: ugly code is ugly, but efficient: http://jsperf.com/array-unique2/8 352 * Note: ugly code is ugly, but efficient: http://jsperf.com/array-unique2/8
...@@ -374,8 +354,7 @@ exports.serialize = serialize; ...@@ -374,8 +354,7 @@ exports.serialize = serialize;
374 * @param Array array 354 * @param Array array
375 * @return Array 355 * @return Array
376 */ 356 */
377 function unique(array) { 357 function unique(array) {
378 "use strict";
379 var o = {}, 358 var o = {},
380 r = []; 359 r = [];
381 for (var i = 0, len = array.length; i !== len; i++) { 360 for (var i = 0, len = array.length; i !== len; i++) {
...@@ -386,5 +365,6 @@ function unique(array) { ...@@ -386,5 +365,6 @@ function unique(array) {
386 } 365 }
387 } 366 }
388 return r; 367 return r;
389 } 368 }
390 exports.unique = unique; 369 exports.unique = unique;
370 })(exports);
......
...@@ -29,6 +29,13 @@ t.comment('fillBlanks()'); ...@@ -29,6 +29,13 @@ t.comment('fillBlanks()');
29 } 29 }
30 })(); 30 })();
31 31
32 t.comment('isArray()');
33 (function() {
34 t.assertEquals(utils.isArray([]), true, 'isArray() checks for an Array');
35 t.assertEquals(utils.isArray({}), false, 'isArray() checks for an Array');
36 t.assertEquals(utils.isArray("foo"), false, 'isArray() checks for an Array');
37 })();
38
32 t.comment('isClipRect()'); 39 t.comment('isClipRect()');
33 (function() { 40 (function() {
34 testCases = [ 41 testCases = [
...@@ -44,6 +51,26 @@ t.comment('isClipRect()'); ...@@ -44,6 +51,26 @@ t.comment('isClipRect()');
44 }); 51 });
45 })(); 52 })();
46 53
54 t.comment('isObject()');
55 (function() {
56 t.assertEquals(utils.isObject({}), true, 'isObject() checks for an Object');
57 t.assertEquals(utils.isObject([]), true, 'isObject() checks for an Object');
58 t.assertEquals(utils.isObject(1), false, 'isObject() checks for an Object');
59 t.assertEquals(utils.isObject("1"), false, 'isObject() checks for an Object');
60 t.assertEquals(utils.isObject(function(){}), false, 'isObject() checks for an Object');
61 t.assertEquals(utils.isObject(new Function('return {};')()), true, 'isObject() checks for an Object');
62 t.assertEquals(utils.isObject(require('webpage').create()), true, 'isObject() checks for an Object');
63 t.assertEquals(utils.isObject(null), false, 'isObject() checks for an Object');
64 })();
65
66 t.comment('isWebPage()');
67 (function() {
68 var pageModule = require('webpage');
69 t.assertEquals(utils.isWebPage(pageModule), false, 'isWebPage() checks for a WebPage instance');
70 t.assertEquals(utils.isWebPage(pageModule.create()), true, 'isWebPage() checks for a WebPage instance');
71 t.assertEquals(utils.isWebPage(null), false, 'isWebPage() checks for a WebPage instance');
72 })();
73
47 t.comment('isJsFile()'); 74 t.comment('isJsFile()');
48 (function() { 75 (function() {
49 testCases = { 76 testCases = {
......