refactored testing system, tests have been split in files and updated
Showing
24 changed files
with
637 additions
and
382 deletions
... | @@ -660,11 +660,9 @@ | ... | @@ -660,11 +660,9 @@ |
660 | * @return Casper | 660 | * @return Casper |
661 | */ | 661 | */ |
662 | start: function(location, then) { | 662 | start: function(location, then) { |
663 | if (this.started) { | ||
664 | this.log("start failed: Casper has already started!", "error"); | ||
665 | } | ||
666 | this.log('Starting...', "info"); | 663 | this.log('Starting...', "info"); |
667 | this.startTime = new Date().getTime(); | 664 | this.startTime = new Date().getTime(); |
665 | this.history = []; | ||
668 | this.steps = []; | 666 | this.steps = []; |
669 | this.step = 0; | 667 | this.step = 0; |
670 | // Option checks | 668 | // Option checks | ... | ... |
... | @@ -45,7 +45,7 @@ | ... | @@ -45,7 +45,7 @@ |
45 | 'WARNING': { fg: 'red', bold: true }, | 45 | 'WARNING': { fg: 'red', bold: true }, |
46 | 'GREEN_BAR': { fg: 'white', bg: 'green', bold: true }, | 46 | 'GREEN_BAR': { fg: 'white', bg: 'green', bold: true }, |
47 | 'RED_BAR': { fg: 'white', bg: 'red', bold: true }, | 47 | 'RED_BAR': { fg: 'white', bg: 'red', bold: true }, |
48 | 'INFO_BAR': { fg: 'cyan', bold: true } | 48 | 'INFO_BAR': { bg: 'cyan', fg: 'white', bold: true } |
49 | }; | 49 | }; |
50 | 50 | ||
51 | /** | 51 | /** | ... | ... |
... | @@ -26,12 +26,17 @@ | ... | @@ -26,12 +26,17 @@ |
26 | * | 26 | * |
27 | */ | 27 | */ |
28 | (function(phantom) { | 28 | (function(phantom) { |
29 | var fs = require('fs'); | ||
30 | |||
29 | /** | 31 | /** |
30 | * Casper tester: makes assertions, stores test results and display then. | 32 | * Casper tester: makes assertions, stores test results and display then. |
31 | * | 33 | * |
32 | */ | 34 | */ |
33 | phantom.Casper.Tester = function(casper, options) { | 35 | phantom.Casper.Tester = function(casper, options) { |
36 | this.running = false; | ||
37 | this.suites = []; | ||
34 | this.options = isType(options, "object") ? options : {}; | 38 | this.options = isType(options, "object") ? options : {}; |
39 | |||
35 | if (!casper instanceof phantom.Casper) { | 40 | if (!casper instanceof phantom.Casper) { |
36 | throw "phantom.Casper.Tester needs a phantom.Casper instance"; | 41 | throw "phantom.Casper.Tester needs a phantom.Casper instance"; |
37 | } | 42 | } |
... | @@ -41,21 +46,6 @@ | ... | @@ -41,21 +46,6 @@ |
41 | var PASS = this.options.PASS || "PASS"; | 46 | var PASS = this.options.PASS || "PASS"; |
42 | var FAIL = this.options.FAIL || "FAIL"; | 47 | var FAIL = this.options.FAIL || "FAIL"; |
43 | 48 | ||
44 | function compareArrays(a, b) { | ||
45 | if (a.length !== b.length) { | ||
46 | return false; | ||
47 | } | ||
48 | a.forEach(function(item, i) { | ||
49 | if (isType(item, "array") && !compareArrays(item, b[i])) { | ||
50 | return false; | ||
51 | } | ||
52 | if (item !== b[i]) { | ||
53 | return false; | ||
54 | } | ||
55 | }); | ||
56 | return true; | ||
57 | } | ||
58 | |||
59 | // properties | 49 | // properties |
60 | this.testResults = { | 50 | this.testResults = { |
61 | passed: 0, | 51 | passed: 0, |
... | @@ -229,6 +219,10 @@ | ... | @@ -229,6 +219,10 @@ |
229 | return this.assertMatch(casper.getCurrentUrl(), pattern, message); | 219 | return this.assertMatch(casper.getCurrentUrl(), pattern, message); |
230 | }; | 220 | }; |
231 | 221 | ||
222 | this.bar = function(text, style) { | ||
223 | casper.echo(fillBlanks(text), style); | ||
224 | }; | ||
225 | |||
232 | /** | 226 | /** |
233 | * Render a colorized output. Basically a proxy method for | 227 | * Render a colorized output. Basically a proxy method for |
234 | * Casper.Colorizer#colorize() | 228 | * Casper.Colorizer#colorize() |
... | @@ -247,31 +241,11 @@ | ... | @@ -247,31 +241,11 @@ |
247 | }; | 241 | }; |
248 | 242 | ||
249 | /** | 243 | /** |
250 | * Tests equality between the two passed arguments. | 244 | * Declares the current test suite done. |
251 | * | 245 | * |
252 | * @param Mixed v1 | ||
253 | * @param Mixed v2 | ||
254 | * @param Boolean | ||
255 | */ | 246 | */ |
256 | this.testEquals = function(v1, v2) { | 247 | this.done = function() { |
257 | if (betterTypeOf(v1) !== betterTypeOf(v2)) { | 248 | this.running = false; |
258 | return false; | ||
259 | } | ||
260 | if (isType(v1, "function")) { | ||
261 | return v1.toString() === v2.toString(); | ||
262 | } | ||
263 | if (v1 instanceof Object && v2 instanceof Object) { | ||
264 | if (Object.keys(v1).length !== Object.keys(v2).length) { | ||
265 | return false; | ||
266 | } | ||
267 | for (var k in v1) { | ||
268 | if (!this.testEquals(v1[k], v2[k])) { | ||
269 | return false; | ||
270 | } | ||
271 | } | ||
272 | return true; | ||
273 | } | ||
274 | return v1 === v2; | ||
275 | }; | 249 | }; |
276 | 250 | ||
277 | /** | 251 | /** |
... | @@ -284,6 +258,20 @@ | ... | @@ -284,6 +258,20 @@ |
284 | }; | 258 | }; |
285 | 259 | ||
286 | /** | 260 | /** |
261 | * Executes a file. We can't use phantom.injectJs(testFile) because we | ||
262 | * wouldn't be able to catch any thrown exception. So eval is evil, but | ||
263 | * evil actually works^W helps sometimes. | ||
264 | * | ||
265 | * @param String file Absolute path to some js/coffee file | ||
266 | */ | ||
267 | this.exec = function(file) { | ||
268 | if (!fs.isFile(file) || !this.isJsFile(file)) { | ||
269 | throw "Can only exec() files with .js or .coffee extensions"; | ||
270 | } | ||
271 | eval(fs.read(file)); | ||
272 | }; | ||
273 | |||
274 | /** | ||
287 | * Adds a failed test entry to the stack. | 275 | * Adds a failed test entry to the stack. |
288 | * | 276 | * |
289 | * @param String message | 277 | * @param String message |
... | @@ -293,6 +281,31 @@ | ... | @@ -293,6 +281,31 @@ |
293 | }; | 281 | }; |
294 | 282 | ||
295 | /** | 283 | /** |
284 | * Recursively finds all test files contained in a given directory. | ||
285 | * | ||
286 | * @param String dir Path to some directory to scan | ||
287 | */ | ||
288 | this.findTestFiles = function(dir) { | ||
289 | var self = this; | ||
290 | if (!fs.isDirectory(dir)) { | ||
291 | return []; | ||
292 | } | ||
293 | var entries = fs.list(dir).filter(function(entry) { | ||
294 | return entry !== '.' && entry !== '..'; | ||
295 | }).map(function(entry) { | ||
296 | return fs.absolute(pathJoin(dir, entry)); | ||
297 | }); | ||
298 | entries.forEach(function(entry) { | ||
299 | if (fs.isDirectory(entry)) { | ||
300 | entries = entries.concat(self.findTestFiles(entry)); | ||
301 | } | ||
302 | }); | ||
303 | return entries.filter(function(entry) { | ||
304 | return self.isJsFile(fs.absolute(pathJoin(dir, entry))); | ||
305 | }); | ||
306 | }; | ||
307 | |||
308 | /** | ||
296 | * Formats a message to highlight some parts of it. | 309 | * Formats a message to highlight some parts of it. |
297 | * | 310 | * |
298 | * @param String message | 311 | * @param String message |
... | @@ -316,6 +329,20 @@ | ... | @@ -316,6 +329,20 @@ |
316 | }; | 329 | }; |
317 | 330 | ||
318 | /** | 331 | /** |
332 | * Checks if a file is apparently javascript compatible (.js or .coffee). | ||
333 | * | ||
334 | * @param String file Path to the file to test | ||
335 | * @return Boolean | ||
336 | */ | ||
337 | this.isJsFile = function(file) { | ||
338 | var ext; | ||
339 | try { | ||
340 | ext = file.split('.').pop().toLowerCase(); | ||
341 | } catch(e) {} | ||
342 | return isType(ext, "string") && ['js', 'coffee'].indexOf(ext) !== -1; | ||
343 | }; | ||
344 | |||
345 | /** | ||
319 | * Adds a successful test entry to the stack. | 346 | * Adds a successful test entry to the stack. |
320 | * | 347 | * |
321 | * @param String message | 348 | * @param String message |
... | @@ -340,13 +367,10 @@ | ... | @@ -340,13 +367,10 @@ |
340 | style = 'GREEN_BAR'; | 367 | style = 'GREEN_BAR'; |
341 | } | 368 | } |
342 | result = statusText + ' ' + total + ' tests executed, ' + this.testResults.passed + ' passed, ' + this.testResults.failed + ' failed.'; | 369 | result = statusText + ' ' + total + ' tests executed, ' + this.testResults.passed + ' passed, ' + this.testResults.failed + ' failed.'; |
343 | if (result.length < 80) { | 370 | casper.echo(this.colorize(fillBlanks(result), style)); |
344 | result += new Array(80 - result.length + 1).join(' '); | ||
345 | } | ||
346 | casper.echo(this.colorize(result, style)); | ||
347 | if (save && isType(require, "function")) { | 371 | if (save && isType(require, "function")) { |
348 | try { | 372 | try { |
349 | require('fs').write(save, exporter.getXML(), 'w'); | 373 | fs.write(save, exporter.getXML(), 'w'); |
350 | casper.echo('result log stored in ' + save, 'INFO'); | 374 | casper.echo('result log stored in ' + save, 'INFO'); |
351 | } catch (e) { | 375 | } catch (e) { |
352 | casper.echo('unable to write results to ' + save + '; ' + e, 'ERROR'); | 376 | casper.echo('unable to write results to ' + save + '; ' + e, 'ERROR'); |
... | @@ -356,5 +380,88 @@ | ... | @@ -356,5 +380,88 @@ |
356 | casper.exit(status || 0); | 380 | casper.exit(status || 0); |
357 | } | 381 | } |
358 | }; | 382 | }; |
383 | |||
384 | /** | ||
385 | * Runs al suites contained in the paths passed as arguments. | ||
386 | * | ||
387 | */ | ||
388 | this.runSuites = function() { | ||
389 | var testFiles = [], self = this; | ||
390 | if (arguments.length === 0) { | ||
391 | throw "No test suite to run"; | ||
392 | } | ||
393 | Array.prototype.forEach.call(arguments, function(path) { | ||
394 | if (!fs.exists(path)) { | ||
395 | self.bar("Path " + path + " doesn't exist", "RED_BAR"); | ||
396 | } | ||
397 | if (fs.isDirectory(path)) { | ||
398 | testFiles = testFiles.concat(self.findTestFiles(path)); | ||
399 | } else if (fs.isFile(path)) { | ||
400 | testFiles.push(path); | ||
401 | } | ||
402 | }); | ||
403 | if (testFiles.length === 0) { | ||
404 | this.bar("No test file found, aborting.", "RED_BAR"); | ||
405 | casper.exit(1); | ||
406 | } | ||
407 | var current = 0; | ||
408 | var interval = setInterval(function(self) { | ||
409 | if (self.running) { | ||
410 | return; | ||
411 | } | ||
412 | if (current === testFiles.length) { | ||
413 | self.renderResults(true); | ||
414 | clearInterval(interval); | ||
415 | } else { | ||
416 | self.runTest(testFiles[current]); | ||
417 | current++; | ||
418 | } | ||
419 | }, 100, this); | ||
420 | }; | ||
421 | |||
422 | /** | ||
423 | * Runs a test file | ||
424 | * | ||
425 | */ | ||
426 | this.runTest = function(testFile) { | ||
427 | this.bar('Test file: ' + testFile, 'INFO_BAR'); | ||
428 | this.running = true; | ||
429 | try { | ||
430 | this.exec(testFile); | ||
431 | } catch (e) { | ||
432 | // TODO: better formatting of aborted failing suite | ||
433 | // TODO: add exception trace (?) | ||
434 | this.error('FATAL: exception raised while runing test suite in ' + testFile + ': ' + e); | ||
435 | this.done(); | ||
436 | } | ||
437 | }; | ||
438 | |||
439 | /** | ||
440 | * Tests equality between the two passed arguments. | ||
441 | * | ||
442 | * @param Mixed v1 | ||
443 | * @param Mixed v2 | ||
444 | * @param Boolean | ||
445 | */ | ||
446 | this.testEquals = function(v1, v2) { | ||
447 | if (betterTypeOf(v1) !== betterTypeOf(v2)) { | ||
448 | return false; | ||
449 | } | ||
450 | if (isType(v1, "function")) { | ||
451 | return v1.toString() === v2.toString(); | ||
452 | } | ||
453 | if (v1 instanceof Object && v2 instanceof Object) { | ||
454 | if (Object.keys(v1).length !== Object.keys(v2).length) { | ||
455 | return false; | ||
456 | } | ||
457 | for (var k in v1) { | ||
458 | if (!this.testEquals(v1[k], v2[k])) { | ||
459 | return false; | ||
460 | } | ||
461 | } | ||
462 | return true; | ||
463 | } | ||
464 | return v1 === v2; | ||
465 | }; | ||
359 | }; | 466 | }; |
360 | })(phantom); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
467 | })(phantom); | ... | ... |
... | @@ -136,6 +136,36 @@ function createPage(casper) { | ... | @@ -136,6 +136,36 @@ function createPage(casper) { |
136 | } | 136 | } |
137 | 137 | ||
138 | /** | 138 | /** |
139 | * Dumps a JSON representation of passed value to the console. Used for | ||
140 | * debugging purpose only. | ||
141 | * | ||
142 | * @param Mixed value | ||
143 | */ | ||
144 | function dump(value) { | ||
145 | if (isType(value, "array")) { | ||
146 | value = value.map(function(prop) { | ||
147 | return isType(prop, "function") ? prop.toString().replace(/\s{2,}/, '') : prop; | ||
148 | }); | ||
149 | } | ||
150 | console.log(JSON.stringify(value, null, 4)); | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * Takes a string and append blank until the pad value is reached. | ||
155 | * | ||
156 | * @param String text | ||
157 | * @param Number pad Pad value (optional; default: 80) | ||
158 | * @return String | ||
159 | */ | ||
160 | function fillBlanks(text, pad) { | ||
161 | pad = pad || 80; | ||
162 | if (text.length < pad) { | ||
163 | text += new Array(pad - text.length + 1).join(' '); | ||
164 | } | ||
165 | return text; | ||
166 | } | ||
167 | |||
168 | /** | ||
139 | * Shorthands for checking if a value is of the given type. Can check for | 169 | * Shorthands for checking if a value is of the given type. Can check for |
140 | * arrays. | 170 | * arrays. |
141 | * | 171 | * | ... | ... |
1 | phantom.injectJs('casper.js'); | 1 | phantom.injectJs('casper.js'); |
2 | 2 | ||
3 | var fs = require('fs'); | ||
3 | var casper = new phantom.Casper({ | 4 | var casper = new phantom.Casper({ |
4 | faultTolerant: false, | 5 | faultTolerant: false, |
5 | verbose: true | 6 | verbose: true |
6 | }); | ||
7 | var fs = require('fs'); | ||
8 | var save = null; | ||
9 | |||
10 | phantom.args.forEach(function(arg) { | ||
11 | var debugMatch = /--loglevel=(\w+)/.exec(arg); | ||
12 | if (debugMatch) { | ||
13 | casper.options.logLevel = debugMatch[1]; | ||
14 | } | ||
15 | var saveMatch = /--save=(.*)(\s|)/.exec(arg); | ||
16 | if (saveMatch) { | ||
17 | save = saveMatch[1]; | ||
18 | } | ||
19 | }); | ||
20 | |||
21 | // phantom.Casper.Tester | ||
22 | casper.test.comment('Tester'); | ||
23 | casper.test.assert(casper.test.testEquals(null, null), 'Tester.testEquals() null equality'); | ||
24 | casper.test.assertNot(casper.test.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality'); | ||
25 | casper.test.assert(casper.test.testEquals("hi", "hi"), 'Tester.testEquals() string equality'); | ||
26 | casper.test.assertNot(casper.test.testEquals("hi", "ih"), 'Tester.testEquals() string inequality'); | ||
27 | casper.test.assert(casper.test.testEquals(5, 5), 'Tester.testEquals() number equality'); | ||
28 | casper.test.assert(casper.test.testEquals(5, 5.0), 'Tester.testEquals() cast number equality'); | ||
29 | casper.test.assertNot(casper.test.testEquals(5, 10), 'Tester.testEquals() number inequality'); | ||
30 | casper.test.assert(casper.test.testEquals([], []), 'Tester.testEquals() empty array equality'); | ||
31 | casper.test.assert(casper.test.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality'); | ||
32 | casper.test.assert(casper.test.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality'); | ||
33 | casper.test.assertNot(casper.test.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality'); | ||
34 | casper.test.assertNot(casper.test.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality'); | ||
35 | casper.test.assertNot(casper.test.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality'); | ||
36 | casper.test.assert(casper.test.testEquals({}, {}), 'Tester.testEquals() empty object equality'); | ||
37 | casper.test.assert(casper.test.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality'); | ||
38 | casper.test.assert(casper.test.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality'); | ||
39 | casper.test.assertNot(casper.test.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality'); | ||
40 | casper.test.assert(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:26}}), 'Tester.testEquals() complex object equality'); | ||
41 | casper.test.assertNot(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:27}}), 'Tester.testEquals() complex object inequality'); | ||
42 | casper.test.assert(casper.test.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality'); | ||
43 | casper.test.assertNot(casper.test.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality'); | ||
44 | |||
45 | // phantom.Casper.FunctionArgsInjector | ||
46 | casper.test.comment('FunctionArgsInjector'); | ||
47 | function createInjector(fn, values) { | ||
48 | return new phantom.Casper.FunctionArgsInjector(fn, values); | ||
49 | } | ||
50 | var testFn = function(a, b) { return a + b; }; | ||
51 | var injector = createInjector(testFn); | ||
52 | var extract = injector.extract(testFn); | ||
53 | casper.test.assertType(extract, "object", 'FunctionArgsInjector.extract() returns an object'); | ||
54 | casper.test.assertEquals(extract.name, null, 'FunctionArgsInjector.extract() process function name as expected'); | ||
55 | casper.test.assertEquals(extract.body, 'return a + b;', 'FunctionArgsInjector.extract() process function body as expected'); | ||
56 | casper.test.assertEquals(extract.args, ['a', 'b'], 'FunctionArgsInjector.extract() process function args as expected'); | ||
57 | var processed; | ||
58 | eval('processed = ' + injector.process({ a: 1, b: 2 })); | ||
59 | casper.test.assertEquals(processed(), 3, 'FunctionArgsInjector.process() proccessed the function correctly'); | ||
60 | |||
61 | // Casper#log() | ||
62 | casper.test.comment('logging'); | ||
63 | var oldLevel = casper.options.logLevel; | ||
64 | casper.options.logLevel = 'info'; | ||
65 | casper.options.verbose = false; | ||
66 | casper.log('foo', 'info'); | ||
67 | casper.test.assert(casper.result.log.some(function(e) { | ||
68 | return e.message === 'foo' && e.level === 'info'; | ||
69 | }), 'Casper.log() adds a log entry'); | ||
70 | casper.options.logLevel = oldLevel; | ||
71 | casper.options.verbose = true; | ||
72 | |||
73 | // Casper#start() | ||
74 | casper.test.comment('navigating'); | ||
75 | casper.start('tests/site/index.html', function(self) { | ||
76 | casper.test.comment('Casper.exists()'); | ||
77 | self.test.assert(self.exists('a') && !self.exists('chucknorriz'), 'Casper.exists() can check if an element exists'); | ||
78 | casper.test.comment('Casper.start()'); | ||
79 | self.test.assertTitle('CasperJS test index', 'Casper.start() casper can start itself an open an url'); | ||
80 | self.test.assertEval(function() { | ||
81 | return typeof(__utils__) === "object"; | ||
82 | }, 'Casper.start() injects ClientUtils instance within remote DOM'); | ||
83 | self.test.comment('fetching'); | ||
84 | self.test.assertEquals(self.fetchText('ul li'), 'onetwothree', 'Casper.fetchText() can retrieves text contents'); | ||
85 | self.test.comment('encoding'); | ||
86 | var image = self.base64encode('file://' + phantom.libraryPath + '/site/images/phantom.png'); | ||
87 | self.test.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents'); | ||
88 | self.test.comment('clicking'); | ||
89 | self.click('a[href="test.html"]'); | ||
90 | }); | ||
91 | |||
92 | casper.test.assertEquals(casper.steps.length, 2, 'Casper.start() can add a new navigation step'); | ||
93 | |||
94 | // Casper.viewport() | ||
95 | casper.test.comment('viewport'); | ||
96 | casper.viewport(1337, 999); | ||
97 | casper.test.assertEquals(casper.page.viewportSize.width, 1337, 'Casper.viewport() can change the width of page viewport'); | ||
98 | casper.test.assertEquals(casper.page.viewportSize.height, 999, 'Casper.viewport() can change the height of page viewport'); | ||
99 | casper.test.assertRaises(casper.viewport, ['a', 'b'], 'Casper.viewport() validates viewport size data'); | ||
100 | |||
101 | // Casper#then() | ||
102 | casper.test.comment('then'); | ||
103 | casper.then(function(self) { | ||
104 | self.test.assertTitle('CasperJS test target', 'Casper.click() casper can click on a text link and react when it is loaded 1/2'); | ||
105 | self.click('a[href="form.html"]'); | ||
106 | }); | ||
107 | |||
108 | casper.test.assert(casper.steps.length === 3, 'Casper.then() adds a new navigation step'); | ||
109 | |||
110 | // Casper#capture() | ||
111 | casper.test.comment('capturing'); | ||
112 | casper.viewport(300, 200); | ||
113 | var testFile = '/tmp/__casper_test_capture.png'; | ||
114 | if (fs.isFile(testFile)) { | ||
115 | fs.remove(testFile); | ||
116 | } | ||
117 | casper.capture(testFile); | ||
118 | casper.test.assert(fs.isFile(testFile), 'Casper.capture() captured a screenshot'); | ||
119 | fs.remove(testFile); | ||
120 | |||
121 | // Casper#evaluate() | ||
122 | casper.then(function(self) { | ||
123 | self.test.comment('Casper.evaluate()'); | ||
124 | var params = { | ||
125 | "boolean true": true, | ||
126 | "boolean false": false, | ||
127 | "int number": 42, | ||
128 | "float number": 1337.42, | ||
129 | "string": "plop! \"Ÿ£$\" 'no'", | ||
130 | "array": [1, 2, 3], | ||
131 | "object": {a: 1, b: 2} | ||
132 | }; | ||
133 | var casperParams = self.evaluate(function() { | ||
134 | return __casper_params__; | ||
135 | }, params); | ||
136 | self.test.assertType(casperParams, "object", 'Casper.evaluate() exposes parameters in a dedicated object'); | ||
137 | self.test.assertEquals(Object.keys(casperParams).length, 7, 'Casper.evaluate() object containing parameters has the correct length'); | ||
138 | for (var param in casperParams) { | ||
139 | self.test.assertEquals(JSON.stringify(casperParams[param]), JSON.stringify(params[param]), 'Casper.evaluate() can pass a ' + param); | ||
140 | self.test.assertEquals(typeof casperParams[param], typeof params[param], 'Casper.evaluate() preserves the ' + param + ' type'); | ||
141 | } | ||
142 | }); | 7 | }); |
143 | 8 | ||
144 | // Casper#fill() | 9 | var tests = []; |
145 | casper.then(function(self) { | 10 | if (phantom.args.length > 0) { |
146 | self.test.assertTitle('CasperJS test form', 'Casper.click() casper can click on a text link and react when it is loaded 2/2'); | 11 | // FIXME: leave room for other arguments than tests to be passed |
147 | self.test.comment('filling a form'); | 12 | tests = phantom.args.map(function(path) { |
148 | self.fill('form[action="result.html"]', { | 13 | return pathJoin(fs.workingDirectory, path); |
149 | email: 'chuck@norris.com', | ||
150 | content: 'Am watching thou', | ||
151 | check: true, | ||
152 | choice: 'no', | ||
153 | topic: 'bar', | ||
154 | file: phantom.libraryPath + '/README.md', | ||
155 | 'checklist[]': ['1', '3'] | ||
156 | }); | 14 | }); |
157 | self.test.assertEvalEquals(function() { | 15 | } else { |
158 | return document.querySelector('input[name="email"]').value; | 16 | tests = [pathJoin(casperLibPath, '..', 'tests', 'suites')]; |
159 | }, 'chuck@norris.com', 'Casper.fill() can fill an input[type=text] form field'); | 17 | } |
160 | self.test.assertEvalEquals(function() { | ||
161 | return document.querySelector('textarea[name="content"]').value; | ||
162 | }, 'Am watching thou', 'Casper.fill() can fill a textarea form field'); | ||
163 | self.test.assertEvalEquals(function() { | ||
164 | return document.querySelector('select[name="topic"]').value; | ||
165 | }, 'bar', 'Casper.fill() can pick a value from a select form field'); | ||
166 | self.test.assertEvalEquals(function() { | ||
167 | return document.querySelector('input[name="check"]').checked; | ||
168 | }, true, 'Casper.fill() can check a form checkbox'); | ||
169 | self.test.assertEvalEquals(function() { | ||
170 | return document.querySelector('input[name="choice"][value="no"]').checked; | ||
171 | }, true, 'Casper.fill() can check a form radio button 1/2'); | ||
172 | self.test.assertEvalEquals(function() { | ||
173 | return document.querySelector('input[name="choice"][value="yes"]').checked; | ||
174 | }, false, 'Casper.fill() can check a form radio button 2/2'); | ||
175 | self.test.assertEvalEquals(function() { | ||
176 | return document.querySelector('input[name="file"]').files.length === 1; | ||
177 | }, true, 'Casper.fill() can select a file to upload'); | ||
178 | self.test.assertEvalEquals(function() { | ||
179 | return (document.querySelector('input[name="checklist[]"][value="1"]').checked && | ||
180 | !document.querySelector('input[name="checklist[]"][value="2"]').checked && | ||
181 | document.querySelector('input[name="checklist[]"][value="3"]').checked); | ||
182 | }, true, 'Casper.fill() can fill a list of checkboxes'); | ||
183 | self.click('input[type="submit"]'); | ||
184 | }); | ||
185 | |||
186 | // Casper#click() | ||
187 | casper.then(function(self) { | ||
188 | self.test.assertTitle('CasperJS test form result', 'Casper.click() casper can click on a submit button'); | ||
189 | self.test.assertUrlMatch(/email=chuck@norris.com/, 'Casper.fill() input[type=email] field was submitted'); | ||
190 | self.test.assertUrlMatch(/content=Am\+watching\+thou/, 'Casper.fill() textarea field was submitted'); | ||
191 | self.test.assertUrlMatch(/check=on/, 'Casper.fill() input[type=checkbox] field was submitted'); | ||
192 | self.test.assertUrlMatch(/choice=no/, 'Casper.fill() input[type=radio] field was submitted'); | ||
193 | self.test.assertUrlMatch(/topic=bar/, 'Casper.fill() select field was submitted'); | ||
194 | }); | ||
195 | |||
196 | // Casper#thenClick() | ||
197 | casper.thenClick('body a', function(self) { | ||
198 | self.test.comment('Casper.thenClick()'); | ||
199 | self.test.assertTitle('CasperJS test index', 'Casper.thenClick() casper can add a step for clicking a link'); | ||
200 | }); | ||
201 | |||
202 | // Casper#each() | ||
203 | casper.test.comment('each'); | ||
204 | casper.each([1, 2, 3], function(self, item, i) { | ||
205 | self.test.assertEquals(i, item - 1, 'Casper.each() passes a contextualized index'); | ||
206 | }); | ||
207 | |||
208 | // Casper.XUnitExporter | ||
209 | casper.test.comment('phantom.Casper.XUnitExporter'); | ||
210 | xunit = new phantom.Casper.XUnitExporter(); | ||
211 | xunit.addSuccess('foo', 'bar'); | ||
212 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase'); | ||
213 | xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz'); | ||
214 | casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase'); | ||
215 | |||
216 | // Casper.ClientUtils.log() | ||
217 | casper.then(function(self) { | ||
218 | casper.test.comment('client utils log'); | ||
219 | var oldLevel = casper.options.logLevel; | ||
220 | casper.options.logLevel = 'debug'; | ||
221 | casper.options.verbose = false; | ||
222 | casper.evaluate(function() { | ||
223 | __utils__.log('debug message'); | ||
224 | __utils__.log('info message', 'info'); | ||
225 | }); | ||
226 | casper.test.assert(casper.result.log.some(function(e) { | ||
227 | return e.message === 'debug message' && e.level === 'debug' && e.space === 'remote'; | ||
228 | }), 'ClientUtils.log() adds a log entry'); | ||
229 | casper.test.assert(casper.result.log.some(function(e) { | ||
230 | return e.message === 'info message' && e.level === 'info' && e.space === 'remote'; | ||
231 | }), 'ClientUtils.log() adds a log entry at a given level'); | ||
232 | casper.options.logLevel = oldLevel; | ||
233 | casper.options.verbose = true; | ||
234 | }); | ||
235 | |||
236 | // Casper.wait() | ||
237 | var waitStart; | ||
238 | casper.then(function() { | ||
239 | waitStart = new Date().getTime(); | ||
240 | }).wait(1000, function(self) { | ||
241 | self.test.comment('wait'); | ||
242 | self.test.assert(new Date().getTime() - waitStart > 1000, 'Casper.wait() can wait for a given amount of time'); | ||
243 | // Casper.waitFor() | ||
244 | casper.thenOpen('tests/site/waitFor.html', function(self) { | ||
245 | casper.test.comment('waitFor'); | ||
246 | self.waitFor(function(self) { | ||
247 | return self.evaluate(function() { | ||
248 | return document.querySelectorAll('li').length === 4; | ||
249 | }); | ||
250 | }, function(self) { | ||
251 | self.test.pass('Casper.waitFor() can wait for something to happen'); | ||
252 | }, function(self) { | ||
253 | self.test.fail('Casper.waitFor() can wait for something to happen'); | ||
254 | }); | ||
255 | }); | ||
256 | }); | ||
257 | |||
258 | // Casper.getGlobal() | ||
259 | casper.thenOpen('tests/site/global.html', function(self) { | ||
260 | self.test.comment('Casper.getGlobal()'); | ||
261 | self.test.assertEquals(self.getGlobal('myGlobal'), 'awesome string', 'Casper.getGlobal() can retrieve a remote global variable'); | ||
262 | self.test.assertRaises(self.getGlobal, ['myUnencodableGlobal'], 'Casper.getGlobal() does not fail trying to encode an unencodable global'); | ||
263 | }); | ||
264 | |||
265 | // Casper.options.onStepComplete | ||
266 | casper.then(function(self) { | ||
267 | self.options.onStepComplete = function(self, stepResult) { | ||
268 | self.test.comment('Casper.options.onStepComplete()'); | ||
269 | self.test.assertEquals(stepResult, 'ok', 'Casper.options.onStepComplete() is called on step complete'); | ||
270 | self.options.onStepComplete = null; | ||
271 | }; | ||
272 | return 'ok'; | ||
273 | }); | ||
274 | |||
275 | // Casper.options.onResourceRequested & Casper.options.onResourceReceived | ||
276 | casper.then(function(self) { | ||
277 | self.options.onResourceReceived = function(self, resource) { | ||
278 | self.test.comment('Casper.options.onResourceReceived()'); | ||
279 | self.test.assertType(resource, 'object', 'Casper.options.onResourceReceived() retrieve a resource object'); | ||
280 | self.test.assert('status' in resource, 'Casper.options.onResourceReceived() retrieve a valid resource object'); | ||
281 | self.options.onResourceReceived = null; | ||
282 | }; | ||
283 | self.options.onResourceRequested = function(self, request) { | ||
284 | self.test.comment('Casper.options.onResourceRequested()'); | ||
285 | self.test.assertType(request, 'object', 'Casper.options.onResourceRequested() retrieve a request object'); | ||
286 | self.test.assert('method' in request, 'Casper.options.onResourceRequested() retrieve a valid request object'); | ||
287 | self.options.onResourceRequested = null; | ||
288 | }; | ||
289 | self.thenOpen('tests/site/page1.html'); | ||
290 | }); | ||
291 | |||
292 | // Casper.visible() | ||
293 | casper.thenOpen('tests/site/visible.html', function(self) { | ||
294 | self.test.comment('Casper.visible()'); | ||
295 | self.test.assert(self.visible('#img1'), 'Casper.visible() can detect if an element is visible'); | ||
296 | self.test.assert(!self.visible('#img2'), 'Casper.visible() can detect if an element is invisible'); | ||
297 | self.test.assert(!self.visible('#img3'), 'Casper.visible() can detect if an element is invisible'); | ||
298 | self.waitWhileVisible('#img1', function(self) { | ||
299 | self.test.comment('Casper.waitWhileVisible()'); | ||
300 | self.test.pass('Casper.waitWhileVisible() can wait while an element is visible'); | ||
301 | }, function(self) { | ||
302 | self.test.comment('Casper.waitWhileVisible()'); | ||
303 | self.test.fail('Casper.waitWhileVisible() can wait while an element is visible'); | ||
304 | }, 2000); | ||
305 | }); | ||
306 | |||
307 | // History | ||
308 | casper | ||
309 | .thenOpen('tests/site/page1.html') | ||
310 | .thenOpen('tests/site/page2.html') | ||
311 | .thenOpen('tests/site/page3.html') | ||
312 | .back() | ||
313 | .then(function(self) { | ||
314 | self.test.comment('navigating history backward'); | ||
315 | self.test.assertMatch(self.getCurrentUrl(), /tests\/site\/page2\.html$/, 'Casper.back() can go back an history step'); | ||
316 | }) | ||
317 | .forward() | ||
318 | .then(function(self) { | ||
319 | self.test.comment('navigating history forward'); | ||
320 | self.test.assertMatch(self.getCurrentUrl(), /tests\/site\/page3\.html$/, 'Casper.forward() can go forward an history step'); | ||
321 | }) | ||
322 | ; | ||
323 | |||
324 | // Casper.options.onAlert() | ||
325 | casper.then(function(self) { | ||
326 | self.options.onAlert = function(self, message) { | ||
327 | self.test.assertEquals(message, 'plop', 'Casper.options.onAlert() can intercept an alert message'); | ||
328 | }; | ||
329 | }); | ||
330 | casper.thenOpen('tests/site/alert.html').click('button', function(self) { | ||
331 | self.options.onAlert = null; | ||
332 | }); | ||
333 | 18 | ||
334 | // run suite | 19 | casper.test.runSuites.apply(casper.test, tests); |
335 | casper.run(function(self) { | ||
336 | casper.test.comment('history'); | ||
337 | casper.test.assert(self.history.length > 0, 'Casper.history contains urls'); | ||
338 | casper.test.assertMatch(self.history[0], /tests\/site\/index\.html$/, 'Casper.history has the correct first url'); | ||
339 | self.test.comment('logging, again'); | ||
340 | self.test.assertEquals(self.result.log.length, 3, 'Casper.log() logged messages'); | ||
341 | self.test.renderResults(true, 0, save); | ||
342 | }); | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
tests/suites/casper/capture.js
0 → 100644
1 | (function(t) { | ||
2 | var fs = require('fs'), testFile = '/tmp/__casper_test_capture.png'; | ||
3 | |||
4 | if (fs.exists(testFile) && fs.isFile(testFile)) { | ||
5 | fs.remove(testFile); | ||
6 | } | ||
7 | |||
8 | casper.start('tests/site/index.html', function(self) { | ||
9 | self.viewport(300, 200); | ||
10 | t.comment('Casper.capture()'); | ||
11 | self.capture(testFile); | ||
12 | t.assert(fs.isFile(testFile), 'Casper.capture() captured a screenshot'); | ||
13 | }); | ||
14 | |||
15 | try { | ||
16 | fs.remove(testFile); | ||
17 | } catch(e) {} | ||
18 | |||
19 | casper.run(function(self) { | ||
20 | t.done(); | ||
21 | }); | ||
22 | })(casper.test); |
tests/suites/casper/click.js
0 → 100644
1 | (function(t) { | ||
2 | t.comment('Casper.click()'); | ||
3 | |||
4 | casper.start('tests/site/index.html', function(self) { | ||
5 | self.click('a[href="test.html"]'); | ||
6 | }); | ||
7 | |||
8 | casper.then(function(self) { | ||
9 | t.assertTitle('CasperJS test target', 'Casper.click() can click on a link'); | ||
10 | }).thenClick('a', function(self) { | ||
11 | t.assertTitle('CasperJS test form', 'Casper.thenClick() can click on a link'); | ||
12 | }); | ||
13 | |||
14 | casper.run(function(self) { | ||
15 | t.done(); | ||
16 | }); | ||
17 | })(casper.test); |
tests/suites/casper/encode.js
0 → 100644
1 | (function(t) { | ||
2 | t.comment('Casper.base64encode()'); | ||
3 | |||
4 | casper.start('tests/site/index.html', function(self) { | ||
5 | var image = self.base64encode('file://' + phantom.libraryPath + '/site/images/phantom.png'); | ||
6 | t.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents'); | ||
7 | }); | ||
8 | |||
9 | casper.run(function(self) { | ||
10 | t.done(); | ||
11 | }); | ||
12 | })(casper.test); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
tests/suites/casper/evaluate.js
0 → 100644
1 | (function(t) { | ||
2 | t.comment('Casper.evaluate()'); | ||
3 | |||
4 | casper.start(); | ||
5 | |||
6 | var params = { | ||
7 | "boolean true": true, | ||
8 | "boolean false": false, | ||
9 | "int number": 42, | ||
10 | "float number": 1337.42, | ||
11 | "string": "plop! \"Ÿ£$\" 'no'", | ||
12 | "array": [1, 2, 3], | ||
13 | "object": {a: 1, b: 2} | ||
14 | }; | ||
15 | |||
16 | var casperParams = casper.evaluate(function() { | ||
17 | return __casper_params__; | ||
18 | }, params); | ||
19 | |||
20 | casper.test.assertType(casperParams, "object", 'Casper.evaluate() exposes parameters in a dedicated object'); | ||
21 | casper.test.assertEquals(Object.keys(casperParams).length, 7, 'Casper.evaluate() object containing parameters has the correct length'); | ||
22 | |||
23 | for (var param in casperParams) { | ||
24 | casper.test.assertEquals(JSON.stringify(casperParams[param]), JSON.stringify(params[param]), 'Casper.evaluate() can pass a ' + param); | ||
25 | casper.test.assertEquals(typeof casperParams[param], typeof params[param], 'Casper.evaluate() preserves the ' + param + ' type'); | ||
26 | } | ||
27 | |||
28 | t.done(); | ||
29 | })(casper.test); |
tests/suites/casper/exists.js
0 → 100644
tests/suites/casper/fetchtext.js
0 → 100644
tests/suites/casper/formfill.js
0 → 100644
1 | (function(t) { | ||
2 | casper.start('tests/site/form.html', function(self) { | ||
3 | t.comment('Casper.fill()'); | ||
4 | self.fill('form[action="result.html"]', { | ||
5 | email: 'chuck@norris.com', | ||
6 | content: 'Am watching thou', | ||
7 | check: true, | ||
8 | choice: 'no', | ||
9 | topic: 'bar', | ||
10 | file: phantom.libraryPath + '/README.md', | ||
11 | 'checklist[]': ['1', '3'] | ||
12 | }); | ||
13 | t.assertEvalEquals(function() { | ||
14 | return document.querySelector('input[name="email"]').value; | ||
15 | }, 'chuck@norris.com', 'Casper.fill() can fill an input[type=text] form field'); | ||
16 | t.assertEvalEquals(function() { | ||
17 | return document.querySelector('textarea[name="content"]').value; | ||
18 | }, 'Am watching thou', 'Casper.fill() can fill a textarea form field'); | ||
19 | t.assertEvalEquals(function() { | ||
20 | return document.querySelector('select[name="topic"]').value; | ||
21 | }, 'bar', 'Casper.fill() can pick a value from a select form field'); | ||
22 | t.assertEvalEquals(function() { | ||
23 | return document.querySelector('input[name="check"]').checked; | ||
24 | }, true, 'Casper.fill() can check a form checkbox'); | ||
25 | t.assertEvalEquals(function() { | ||
26 | return document.querySelector('input[name="choice"][value="no"]').checked; | ||
27 | }, true, 'Casper.fill() can check a form radio button 1/2'); | ||
28 | t.assertEvalEquals(function() { | ||
29 | return document.querySelector('input[name="choice"][value="yes"]').checked; | ||
30 | }, false, 'Casper.fill() can check a form radio button 2/2'); | ||
31 | t.assertEvalEquals(function() { | ||
32 | return document.querySelector('input[name="file"]').files.length === 1; | ||
33 | }, true, 'Casper.fill() can select a file to upload'); | ||
34 | t.assertEvalEquals(function() { | ||
35 | return (document.querySelector('input[name="checklist[]"][value="1"]').checked && | ||
36 | !document.querySelector('input[name="checklist[]"][value="2"]').checked && | ||
37 | document.querySelector('input[name="checklist[]"][value="3"]').checked); | ||
38 | }, true, 'Casper.fill() can fill a list of checkboxes'); | ||
39 | self.click('input[type="submit"]'); | ||
40 | }); | ||
41 | |||
42 | casper.then(function(self) { | ||
43 | t.comment('Form submitted'); | ||
44 | t.assertUrlMatch(/email=chuck@norris.com/, 'Casper.fill() input[type=email] field was submitted'); | ||
45 | t.assertUrlMatch(/content=Am\+watching\+thou/, 'Casper.fill() textarea field was submitted'); | ||
46 | t.assertUrlMatch(/check=on/, 'Casper.fill() input[type=checkbox] field was submitted'); | ||
47 | t.assertUrlMatch(/choice=no/, 'Casper.fill() input[type=radio] field was submitted'); | ||
48 | t.assertUrlMatch(/topic=bar/, 'Casper.fill() select field was submitted'); | ||
49 | }); | ||
50 | |||
51 | casper.run(function(self) { | ||
52 | t.done(); | ||
53 | }); | ||
54 | })(casper.test); |
tests/suites/casper/global.js
0 → 100644
1 | (function(t) { | ||
2 | casper.start('tests/site/global.html', function(self) { | ||
3 | t.comment('Casper.getGlobal()'); | ||
4 | t.assertEquals(self.getGlobal('myGlobal'), 'awesome string', 'Casper.getGlobal() can retrieve a remote global variable'); | ||
5 | t.assertRaises(self.getGlobal, ['myUnencodableGlobal'], 'Casper.getGlobal() does not fail trying to encode an unencodable global'); | ||
6 | }); | ||
7 | |||
8 | casper.run(function(self) { | ||
9 | t.done(); | ||
10 | }); | ||
11 | })(casper.test); |
tests/suites/casper/history.js
0 → 100644
1 | (function(t) { | ||
2 | casper.start('tests/site/page1.html'); | ||
3 | casper.thenOpen('tests/site/page2.html'); | ||
4 | casper.thenOpen('tests/site/page3.html'); | ||
5 | |||
6 | casper.back(); | ||
7 | casper.then(function(self) { | ||
8 | t.comment('navigating history backward'); | ||
9 | t.assertMatch(self.getCurrentUrl(), /tests\/site\/page2\.html$/, 'Casper.back() can go back an history step'); | ||
10 | }); | ||
11 | |||
12 | casper.forward(); | ||
13 | casper.then(function(self) { | ||
14 | t.comment('navigating history forward'); | ||
15 | t.assertMatch(self.getCurrentUrl(), /tests\/site\/page3\.html$/, 'Casper.forward() can go forward an history step'); | ||
16 | }); | ||
17 | |||
18 | casper.run(function(self) { | ||
19 | t.assert(self.history.length > 0, 'Casper.history contains urls'); | ||
20 | t.assertMatch(self.history[0], /tests\/site\/page1\.html$/, 'Casper.history has the correct first url'); | ||
21 | t.done(); | ||
22 | }); | ||
23 | })(casper.test); |
tests/suites/casper/hooks.js
0 → 100644
1 | (function(t) { | ||
2 | // Casper.options.onStepComplete | ||
3 | casper.start('tests/site/index.html', function(self) { | ||
4 | self.options.onStepComplete = function(self, stepResult) { | ||
5 | t.comment('Casper.options.onStepComplete()'); | ||
6 | t.assertEquals(stepResult, 'ok', 'Casper.options.onStepComplete() is called on step complete'); | ||
7 | self.options.onStepComplete = null; | ||
8 | }; | ||
9 | return 'ok'; | ||
10 | }); | ||
11 | |||
12 | // Casper.options.onResourceRequested & Casper.options.onResourceReceived | ||
13 | casper.then(function(self) { | ||
14 | self.options.onResourceReceived = function(self, resource) { | ||
15 | t.comment('Casper.options.onResourceReceived()'); | ||
16 | t.assertType(resource, 'object', 'Casper.options.onResourceReceived() retrieve a resource object'); | ||
17 | t.assert('status' in resource, 'Casper.options.onResourceReceived() retrieve a valid resource object'); | ||
18 | self.options.onResourceReceived = null; | ||
19 | }; | ||
20 | self.options.onResourceRequested = function(self, request) { | ||
21 | t.comment('Casper.options.onResourceRequested()'); | ||
22 | t.assertType(request, 'object', 'Casper.options.onResourceRequested() retrieve a request object'); | ||
23 | t.assert('method' in request, 'Casper.options.onResourceRequested() retrieve a valid request object'); | ||
24 | self.options.onResourceRequested = null; | ||
25 | }; | ||
26 | self.thenOpen('tests/site/page1.html'); | ||
27 | }); | ||
28 | |||
29 | // Casper.options.onAlert() | ||
30 | casper.then(function(self) { | ||
31 | self.options.onAlert = function(self, message) { | ||
32 | t.assertEquals(message, 'plop', 'Casper.options.onAlert() can intercept an alert message'); | ||
33 | }; | ||
34 | }).thenOpen('tests/site/alert.html').click('button', function(self) { | ||
35 | self.options.onAlert = null; | ||
36 | }); | ||
37 | |||
38 | casper.run(function(self) { | ||
39 | t.done(); | ||
40 | }); | ||
41 | })(casper.test); |
tests/suites/casper/logging.js
0 → 100644
1 | (function(t) { | ||
2 | casper.start('tests/site/index.html'); | ||
3 | |||
4 | var oldLevel = casper.options.logLevel; | ||
5 | |||
6 | casper.options.logLevel = 'info'; | ||
7 | casper.options.verbose = false; | ||
8 | |||
9 | t.comment('Casper.log()'); | ||
10 | casper.log('foo', 'info'); | ||
11 | t.assert(casper.result.log.some(function(e) { | ||
12 | return e.message === 'foo' && e.level === 'info'; | ||
13 | }), 'Casper.log() adds a log entry'); | ||
14 | |||
15 | casper.options.logLevel = oldLevel; | ||
16 | casper.options.verbose = true; | ||
17 | |||
18 | casper.then(function(self) { | ||
19 | var oldLevel = casper.options.logLevel; | ||
20 | casper.options.logLevel = 'debug'; | ||
21 | casper.options.verbose = false; | ||
22 | casper.evaluate(function() { | ||
23 | __utils__.log('debug message'); | ||
24 | __utils__.log('info message', 'info'); | ||
25 | }); | ||
26 | t.assert(casper.result.log.some(function(e) { | ||
27 | return e.message === 'debug message' && e.level === 'debug' && e.space === 'remote'; | ||
28 | }), 'ClientUtils.log() adds a log entry'); | ||
29 | t.assert(casper.result.log.some(function(e) { | ||
30 | return e.message === 'info message' && e.level === 'info' && e.space === 'remote'; | ||
31 | }), 'ClientUtils.log() adds a log entry at a given level'); | ||
32 | casper.options.logLevel = oldLevel; | ||
33 | casper.options.verbose = true; | ||
34 | }); | ||
35 | |||
36 | casper.run(function(self) { | ||
37 | t.assertEquals(self.result.log.length, 3, 'Casper.log() logged messages'); | ||
38 | t.done(); | ||
39 | }); | ||
40 | })(casper.test); |
tests/suites/casper/start.js
0 → 100644
1 | (function(t) { | ||
2 | t.comment('Casper.start()'); | ||
3 | |||
4 | casper.start('tests/site/index.html', function(self) { | ||
5 | t.pass('Casper.start() can chain a next step'); | ||
6 | t.assertTitle('CasperJS test index', 'Casper.start() opened the passed url'); | ||
7 | t.assertEval(function() { | ||
8 | return typeof(__utils__) === "object"; | ||
9 | }, 'Casper.start() injects ClientUtils instance within remote DOM'); | ||
10 | }); | ||
11 | |||
12 | t.assert(casper.started, 'Casper.start() started'); | ||
13 | |||
14 | casper.run(function(self) { | ||
15 | t.done(); | ||
16 | }); | ||
17 | })(casper.test); |
tests/suites/casper/steps.js
0 → 100644
1 | (function(t) { | ||
2 | t.comment('Casper.then()'); | ||
3 | |||
4 | casper.start('tests/site/index.html'); | ||
5 | |||
6 | var nsteps = casper.steps.length; | ||
7 | |||
8 | casper.then(function(self) { | ||
9 | t.assertTitle('CasperJS test index', 'Casper.then() added a new step'); | ||
10 | }); | ||
11 | |||
12 | t.assertEquals(casper.steps.length, nsteps + 1, 'Casper.then() can add a new step'); | ||
13 | |||
14 | t.comment('Casper.thenOpen()'); | ||
15 | |||
16 | casper.thenOpen('tests/site/test.html'); | ||
17 | |||
18 | t.assertEquals(casper.steps.length, nsteps + 2, 'Casper.thenOpen() can add a new step'); | ||
19 | |||
20 | casper.thenOpen('tests/site/test.html', function(self) { | ||
21 | t.assertTitle('CasperJS test target', 'Casper.thenOpen() opened a location and executed a step'); | ||
22 | }); | ||
23 | |||
24 | t.assertEquals(casper.steps.length, nsteps + 4, 'Casper.thenOpen() can add a new step for opening, plus another step'); | ||
25 | |||
26 | t.comment('Casper.each()'); | ||
27 | casper.each([1, 2, 3], function(self, item, i) { | ||
28 | self.test.assertEquals(i, item - 1, 'Casper.each() passes a contextualized index'); | ||
29 | }); | ||
30 | |||
31 | casper.run(function(self) { | ||
32 | t.done(); | ||
33 | }); | ||
34 | })(casper.test); |
tests/suites/casper/viewport.js
0 → 100644
1 | (function(t) { | ||
2 | t.comment('Casper.viewport()'); | ||
3 | |||
4 | casper.start(); | ||
5 | |||
6 | casper.viewport(1337, 999); | ||
7 | |||
8 | t.assertEquals(casper.page.viewportSize.width, 1337, 'Casper.viewport() can change the width of page viewport'); | ||
9 | t.assertEquals(casper.page.viewportSize.height, 999, 'Casper.viewport() can change the height of page viewport'); | ||
10 | t.assertRaises(casper.viewport, ['a', 'b'], 'Casper.viewport() validates viewport size data'); | ||
11 | |||
12 | t.done(); | ||
13 | })(casper.test); |
tests/suites/casper/visible.js
0 → 100644
1 | (function(t) { | ||
2 | casper.start('tests/site/visible.html', function(self) { | ||
3 | self.test.comment('Casper.visible()'); | ||
4 | self.test.assert(self.visible('#img1'), 'Casper.visible() can detect if an element is visible'); | ||
5 | self.test.assert(!self.visible('#img2'), 'Casper.visible() can detect if an element is invisible'); | ||
6 | self.test.assert(!self.visible('#img3'), 'Casper.visible() can detect if an element is invisible'); | ||
7 | self.waitWhileVisible('#img1', function(self) { | ||
8 | self.test.comment('Casper.waitWhileVisible()'); | ||
9 | self.test.pass('Casper.waitWhileVisible() can wait while an element is visible'); | ||
10 | }, function(self) { | ||
11 | self.test.comment('Casper.waitWhileVisible()'); | ||
12 | self.test.fail('Casper.waitWhileVisible() can wait while an element is visible'); | ||
13 | }, 2000); | ||
14 | }); | ||
15 | |||
16 | casper.run(function(self) { | ||
17 | t.done(); | ||
18 | }); | ||
19 | })(casper.test); |
tests/suites/casper/wait.js
0 → 100644
1 | (function(t) { | ||
2 | var waitStart; | ||
3 | |||
4 | casper.start('tests/site/index.html', function(self) { | ||
5 | waitStart = new Date().getTime(); | ||
6 | }); | ||
7 | |||
8 | casper.wait(1000, function(self) { | ||
9 | self.test.comment('Casper.wait()'); | ||
10 | self.test.assert(new Date().getTime() - waitStart > 1000, 'Casper.wait() can wait for a given amount of time'); | ||
11 | // Casper.waitFor() | ||
12 | casper.thenOpen('tests/site/waitFor.html', function(self) { | ||
13 | casper.test.comment('Casper.waitFor()'); | ||
14 | self.waitFor(function(self) { | ||
15 | return self.evaluate(function() { | ||
16 | return document.querySelectorAll('li').length === 4; | ||
17 | }); | ||
18 | }, function(self) { | ||
19 | self.test.pass('Casper.waitFor() can wait for something to happen'); | ||
20 | }, function(self) { | ||
21 | self.test.fail('Casper.waitFor() can wait for something to happen'); | ||
22 | }); | ||
23 | }); | ||
24 | }); | ||
25 | |||
26 | casper.run(function(self) { | ||
27 | t.done(); | ||
28 | }); | ||
29 | })(casper.test); |
tests/suites/injector.js
0 → 100644
1 | // phantom.Casper.FunctionArgsInjector | ||
2 | var t = casper.test; | ||
3 | |||
4 | function createInjector(fn, values) { | ||
5 | return new phantom.Casper.FunctionArgsInjector(fn, values); | ||
6 | } | ||
7 | |||
8 | var testFn = function(a, b) { return a + b; }; | ||
9 | var injector = createInjector(testFn); | ||
10 | var extract = injector.extract(testFn); | ||
11 | |||
12 | t.comment('FunctionArgsInjector.extract()'); | ||
13 | t.assertType(extract, "object", 'FunctionArgsInjector.extract() returns an object'); | ||
14 | t.assertEquals(extract.name, null, 'FunctionArgsInjector.extract() process function name as expected'); | ||
15 | t.assertEquals(extract.body, 'return a + b;', 'FunctionArgsInjector.extract() process function body as expected'); | ||
16 | t.assertEquals(extract.args, ['a', 'b'], 'FunctionArgsInjector.extract() process function args as expected'); | ||
17 | |||
18 | var processed; | ||
19 | eval('processed = ' + injector.process({ a: 1, b: 2 })); | ||
20 | |||
21 | t.assertEquals(processed(), 3, 'FunctionArgsInjector.process() proccessed the function correctly'); | ||
22 | |||
23 | t.done(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
tests/suites/tester.js
0 → 100644
1 | var t = casper.test; | ||
2 | |||
3 | t.comment('Tester.testEquals()'); | ||
4 | t.assert(casper.test.testEquals(null, null), 'Tester.testEquals() null equality'); | ||
5 | t.assertNot(casper.test.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality'); | ||
6 | t.assert(casper.test.testEquals("hi", "hi"), 'Tester.testEquals() string equality'); | ||
7 | t.assertNot(casper.test.testEquals("hi", "ih"), 'Tester.testEquals() string inequality'); | ||
8 | t.assert(casper.test.testEquals(5, 5), 'Tester.testEquals() number equality'); | ||
9 | t.assert(casper.test.testEquals(5, 5.0), 'Tester.testEquals() cast number equality'); | ||
10 | t.assertNot(casper.test.testEquals(5, 10), 'Tester.testEquals() number inequality'); | ||
11 | t.assert(casper.test.testEquals([], []), 'Tester.testEquals() empty array equality'); | ||
12 | t.assert(casper.test.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality'); | ||
13 | t.assert(casper.test.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality'); | ||
14 | t.assertNot(casper.test.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality'); | ||
15 | t.assertNot(casper.test.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality'); | ||
16 | t.assertNot(casper.test.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality'); | ||
17 | t.assert(casper.test.testEquals({}, {}), 'Tester.testEquals() empty object equality'); | ||
18 | t.assert(casper.test.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality'); | ||
19 | t.assert(casper.test.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality'); | ||
20 | t.assertNot(casper.test.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality'); | ||
21 | t.assert(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:26}}), 'Tester.testEquals() complex object equality'); | ||
22 | t.assertNot(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:27}}), 'Tester.testEquals() complex object inequality'); | ||
23 | t.assert(casper.test.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality'); | ||
24 | t.assertNot(casper.test.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality'); | ||
25 | |||
26 | t.done(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
tests/suites/xunit.js
0 → 100644
1 | var t = casper.test; | ||
2 | |||
3 | t.comment('phantom.Casper.XUnitExporter'); | ||
4 | |||
5 | xunit = new phantom.Casper.XUnitExporter(); | ||
6 | xunit.addSuccess('foo', 'bar'); | ||
7 | t.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase'); | ||
8 | xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz'); | ||
9 | t.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase'); | ||
10 | |||
11 | t.done(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment