casperjs 1.0.0.RC1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/CHANGELOG.md +179 -0
  2. data/LICENSE.md +19 -0
  3. data/README.md +30 -0
  4. data/bin/bootstrap.js +292 -0
  5. data/bin/usage.txt +10 -0
  6. data/casperjs.gemspec +21 -0
  7. data/modules/casper.js +1679 -0
  8. data/modules/cli.js +138 -0
  9. data/modules/clientutils.js +595 -0
  10. data/modules/colorizer.js +129 -0
  11. data/modules/events.js +247 -0
  12. data/modules/injector.js +93 -0
  13. data/modules/mouse.js +110 -0
  14. data/modules/querystring.js +187 -0
  15. data/modules/tester.js +807 -0
  16. data/modules/utils.js +429 -0
  17. data/modules/vendors/coffee-script.js +8 -0
  18. data/modules/xunit.js +123 -0
  19. data/package.json +35 -0
  20. data/rubybin/casperjs +57 -0
  21. data/samples/bbcshots.coffee +64 -0
  22. data/samples/bbcshots.js +80 -0
  23. data/samples/cliplay.coffee +19 -0
  24. data/samples/cliplay.js +21 -0
  25. data/samples/customevents.coffee +11 -0
  26. data/samples/customevents.js +13 -0
  27. data/samples/customlogging.coffee +33 -0
  28. data/samples/customlogging.js +42 -0
  29. data/samples/download.coffee +10 -0
  30. data/samples/download.js +11 -0
  31. data/samples/dynamic.coffee +60 -0
  32. data/samples/dynamic.js +65 -0
  33. data/samples/each.coffee +14 -0
  34. data/samples/each.js +17 -0
  35. data/samples/events.coffee +34 -0
  36. data/samples/events.js +41 -0
  37. data/samples/extends.coffee +29 -0
  38. data/samples/extends.js +37 -0
  39. data/samples/googlelinks.coffee +27 -0
  40. data/samples/googlelinks.js +33 -0
  41. data/samples/googlematch.coffee +47 -0
  42. data/samples/googlematch.js +65 -0
  43. data/samples/googlepagination.coffee +40 -0
  44. data/samples/googlepagination.js +51 -0
  45. data/samples/googletesting.coffee +17 -0
  46. data/samples/googletesting.js +23 -0
  47. data/samples/logcolor.coffee +10 -0
  48. data/samples/logcolor.js +11 -0
  49. data/samples/metaextract.coffee +23 -0
  50. data/samples/metaextract.js +29 -0
  51. data/samples/multirun.coffee +37 -0
  52. data/samples/multirun.js +56 -0
  53. data/samples/screenshot.coffee +28 -0
  54. data/samples/screenshot.js +33 -0
  55. data/samples/statushandlers.coffee +15 -0
  56. data/samples/statushandlers.js +19 -0
  57. data/samples/steptimeout.coffee +37 -0
  58. data/samples/steptimeout.js +45 -0
  59. data/samples/timeout.coffee +39 -0
  60. data/samples/timeout.js +47 -0
  61. data/tests/run.js +76 -0
  62. data/tests/site/alert.html +10 -0
  63. data/tests/site/click.html +40 -0
  64. data/tests/site/confirm.html +12 -0
  65. data/tests/site/elementattribute.html +6 -0
  66. data/tests/site/error.html +10 -0
  67. data/tests/site/form.html +26 -0
  68. data/tests/site/global.html +9 -0
  69. data/tests/site/images/phantom.png +0 -0
  70. data/tests/site/index.html +17 -0
  71. data/tests/site/mouse-events.html +47 -0
  72. data/tests/site/multiple-forms.html +16 -0
  73. data/tests/site/page1.html +8 -0
  74. data/tests/site/page2.html +8 -0
  75. data/tests/site/page3.html +8 -0
  76. data/tests/site/prompt.html +12 -0
  77. data/tests/site/resources.html +16 -0
  78. data/tests/site/result.html +11 -0
  79. data/tests/site/test.html +10 -0
  80. data/tests/site/visible.html +17 -0
  81. data/tests/site/waitFor.html +22 -0
  82. data/tests/suites/casper/agent.js +24 -0
  83. data/tests/suites/casper/capture.js +31 -0
  84. data/tests/suites/casper/click.js +61 -0
  85. data/tests/suites/casper/confirm.js +21 -0
  86. data/tests/suites/casper/elementattribute.js +8 -0
  87. data/tests/suites/casper/encode.js +20 -0
  88. data/tests/suites/casper/evaluate.js +27 -0
  89. data/tests/suites/casper/events.js +38 -0
  90. data/tests/suites/casper/exists.js +9 -0
  91. data/tests/suites/casper/fetchtext.js +9 -0
  92. data/tests/suites/casper/flow.coffee +38 -0
  93. data/tests/suites/casper/formfill.js +69 -0
  94. data/tests/suites/casper/global.js +9 -0
  95. data/tests/suites/casper/history.js +21 -0
  96. data/tests/suites/casper/hooks.js +41 -0
  97. data/tests/suites/casper/logging.js +38 -0
  98. data/tests/suites/casper/mouseevents.js +27 -0
  99. data/tests/suites/casper/onerror.js +19 -0
  100. data/tests/suites/casper/open.js +73 -0
  101. data/tests/suites/casper/prompt.js +17 -0
  102. data/tests/suites/casper/resources.coffee +24 -0
  103. data/tests/suites/casper/start.js +15 -0
  104. data/tests/suites/casper/steps.js +32 -0
  105. data/tests/suites/casper/viewport.js +11 -0
  106. data/tests/suites/casper/visible.js +17 -0
  107. data/tests/suites/casper/wait.js +27 -0
  108. data/tests/suites/casper/xpath.js +32 -0
  109. data/tests/suites/cli.js +125 -0
  110. data/tests/suites/clientutils.js +84 -0
  111. data/tests/suites/coffee.coffee +19 -0
  112. data/tests/suites/fs.js +36 -0
  113. data/tests/suites/http_status.js +28 -0
  114. data/tests/suites/injector.js +64 -0
  115. data/tests/suites/tester.js +121 -0
  116. data/tests/suites/utils.js +209 -0
  117. data/tests/suites/xunit.js +16 -0
  118. data/tests/testdir/01_a/abc.js +0 -0
  119. data/tests/testdir/01_a/def.js +0 -0
  120. data/tests/testdir/02_b/abc.js +0 -0
  121. data/tests/testdir/03_a.js +0 -0
  122. data/tests/testdir/03_b.js +0 -0
  123. data/tests/testdir/04/01_init.js +0 -0
  124. data/tests/testdir/04/02_do.js +0 -0
  125. metadata +192 -0
@@ -0,0 +1,84 @@
1
+ var fs = require('fs');
2
+ var x = require('casper').selectXPath;
3
+
4
+ function fakeDocument(html) {
5
+ window.document.body.innerHTML = html;
6
+ }
7
+
8
+ (function(casper) {
9
+ casper.test.comment('ClientUtils.encode()');
10
+ var clientutils = require('clientutils').create();
11
+ var testCases = {
12
+ 'an empty string': '',
13
+ 'a word': 'plop',
14
+ 'a null char': 'a\u0000',
15
+ 'an utf8 string': 'ÀÁÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
16
+ 'song lyrics': ("Voilà l'été, j'aperçois le soleil\n" +
17
+ "Les nuages filent et le ciel s'éclaircit\n" +
18
+ "Et dans ma tête qui bourdonnent?\n" +
19
+ "Les abeilles!"),
20
+ 'a file contents': fs.read(phantom.casperPath + '/tests/site/alert.html')
21
+ };
22
+ for (var what in testCases) {
23
+ var source = testCases[what];
24
+ var encoded = clientutils.encode(source);
25
+ casper.test.assertEquals(clientutils.decode(encoded), source, 'ClientUtils.encode() encodes and decodes ' + what);
26
+ }
27
+ })(casper);
28
+
29
+ (function(casper) {
30
+ casper.test.comment('ClientUtils.exists()');
31
+ var clientutils = require('clientutils').create();
32
+ fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>');
33
+ casper.test.assert(clientutils.exists('ul'), 'ClientUtils.exists() checks that an element exist');
34
+ casper.test.assertNot(clientutils.exists('ol'), 'ClientUtils.exists() checks that an element exist');
35
+ casper.test.assert(clientutils.exists('ul.foo li'), 'ClientUtils.exists() checks that an element exist');
36
+ // xpath
37
+ casper.test.assert(clientutils.exists(x('//ul')), 'ClientUtils.exists() checks that an element exist using XPath');
38
+ casper.test.assertNot(clientutils.exists(x('//ol')), 'ClientUtils.exists() checks that an element exist using XPath');
39
+ fakeDocument(null);
40
+ })(casper);
41
+
42
+ (function(casper) {
43
+ casper.test.comment('ClientUtils.findAll()');
44
+ var clientutils = require('clientutils').create();
45
+ fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>');
46
+ casper.test.assertType(clientutils.findAll('li'), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements');
47
+ casper.test.assertEquals(clientutils.findAll('li').length, 2, 'ClientUtils.findAll() can find matching DOM elements');
48
+ casper.test.assertType(clientutils.findAll('ol'), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements');
49
+ casper.test.assertEquals(clientutils.findAll('ol').length, 0, 'ClientUtils.findAll() can find matching DOM elements');
50
+ // scoped
51
+ var scope = clientutils.findOne('ul');
52
+ casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope');
53
+ fakeDocument(null);
54
+ })(casper);
55
+
56
+ (function(casper) {
57
+ casper.test.comment('ClientUtils.findOne()');
58
+ var clientutils = require('clientutils').create();
59
+ fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>');
60
+ casper.test.assertType(clientutils.findOne('ul'), 'htmlulistelement', 'ClientUtils.findOne() can find a matching DOM element');
61
+ casper.test.assertNot(clientutils.findOne('ol'), 'ClientUtils.findOne() can find a matching DOM element');
62
+ // scoped
63
+ var scope = clientutils.findOne('ul');
64
+ casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope');
65
+ casper.test.assertEquals(clientutils.findAll('li', scope).length, 2, 'ClientUtils.findAll() can find matching DOM elements within a given scope');
66
+ fakeDocument(null);
67
+ })(casper);
68
+
69
+ (function(casper) {
70
+ casper.test.comment('ClientUtils.processSelector()');
71
+ var clientutils = require('clientutils').create();
72
+ // CSS3 selector
73
+ var cssSelector = clientutils.processSelector('html body > ul.foo li');
74
+ casper.test.assertType(cssSelector, 'object', 'ClientUtils.processSelector() can process a CSS3 selector');
75
+ casper.test.assertEquals(cssSelector.type, 'css', 'ClientUtils.processSelector() can process a CSS3 selector');
76
+ casper.test.assertEquals(cssSelector.path, 'html body > ul.foo li', 'ClientUtils.processSelector() can process a CSS3 selector');
77
+ // XPath selector
78
+ var xpathSelector = clientutils.processSelector(x('//li[text()="blah"]'));
79
+ casper.test.assertType(xpathSelector, 'object', 'ClientUtils.processSelector() can process a XPath selector');
80
+ casper.test.assertEquals(xpathSelector.type, 'xpath', 'ClientUtils.processSelector() can process a XPath selector');
81
+ casper.test.assertEquals(xpathSelector.path, '//li[text()="blah"]', 'ClientUtils.processSelector() can process a XPath selector');
82
+ })(casper);
83
+
84
+ casper.test.done();
@@ -0,0 +1,19 @@
1
+ "A small subset of the run.js written in coffeescript"
2
+
3
+ steps = 0
4
+
5
+ casper.options.onStepComplete = -> steps++
6
+
7
+ casper.start "tests/site/index.html", ->
8
+ @test.assertTitle "CasperJS test index", "Casper.start() casper can start itself an open an url"
9
+ @test.assertEquals @fetchText("ul li"), "onetwothree", "Casper.fetchText() can retrieves text contents"
10
+ @click "a[href=\"test.html\"]"
11
+
12
+ casper.then ->
13
+ @test.assertTitle "CasperJS test target", "Casper.click() casper can click on a text link"
14
+ @click "a[href=\"form.html\"]"
15
+
16
+ casper.run ->
17
+ @test.assertEquals steps, 3, "Casper.options.onStepComplete() is called on step complete"
18
+ @options.onStepComplete = null
19
+ @test.done()
@@ -0,0 +1,36 @@
1
+ var fs = require('fs'), t = casper.test;
2
+
3
+ // Testing added methods
4
+ (function() {
5
+ t.comment('fs.dirname()');
6
+ var tests = {
7
+ '/local/plop/foo.js': '/local/plop',
8
+ 'local/plop/foo.js': 'local/plop',
9
+ './local/plop/foo.js': './local/plop',
10
+ 'c:\\local\\plop\\foo.js': 'c:/local/plop',
11
+ 'D:\\local\\plop\\foo.js': 'D:/local/plop',
12
+ 'D:\\local\\plop\\': 'D:/local/plop',
13
+ 'c:\\': 'c:',
14
+ 'c:': 'c:'
15
+ };
16
+ for (var testCase in tests) {
17
+ t.assertEquals(fs.dirname(testCase), tests[testCase], 'fs.dirname() does its job for ' + testCase);
18
+ }
19
+ })();
20
+
21
+ (function() {
22
+ t.comment('fs.dirname()');
23
+ var tests = {
24
+ '/': false,
25
+ '/local/plop/foo.js': false,
26
+ 'D:\\local\\plop\\': true,
27
+ 'c:\\': true,
28
+ 'c:': true,
29
+ '\\\\Server\\Plop': true
30
+ };
31
+ for (var testCase in tests) {
32
+ t.assertEquals(fs.isWindows(testCase), tests[testCase], 'fs.isWindows() does its job for ' + testCase);
33
+ }
34
+ })();
35
+
36
+ t.done();
@@ -0,0 +1,28 @@
1
+ var server = require('webserver').create();
2
+ var service = server.listen(8090, function (request, response) {
3
+ var code = parseInt(/^\/(\d+)$/.exec(request.url)[1], 10);
4
+ response.statusCode = code;
5
+ response.write("");
6
+ response.close();
7
+ });
8
+
9
+ codes = [100, 101, 102, 118, 200, 201, 202, 203, 204, 205, 206, 207, 210,
10
+ 300, 301, 302, 303, 304, 305, 307, 310,
11
+ 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413,
12
+ 414, 415, 416, 417, 418, 422, 423, 424, 425, 426, 449, 450,
13
+ 500, 501, 502, 503, 504, 505, 507, 509];
14
+
15
+ casper.start('http://google.com').each(codes, function(self, code) {
16
+ if (code === 100) {
17
+ // HTTP 100 is CONTINUE, so don't expect a terminated response
18
+ return;
19
+ }
20
+ this.thenOpen('http://localhost:8090/' + code, function() {
21
+ this.test.assertHttpStatus(code);
22
+ });
23
+ });
24
+
25
+ casper.run(function() {
26
+ server.close();
27
+ this.test.done();
28
+ });
@@ -0,0 +1,64 @@
1
+ var t = casper.test;
2
+ var createInjector = function(fn, values) {
3
+ return require('injector').create(fn, values);
4
+ };
5
+ var testFn = function(a, b) { return a + b; };
6
+ var injector = createInjector(testFn);
7
+ var extract = injector.extract(testFn);
8
+
9
+ t.comment('FunctionArgsInjector.extract()');
10
+ t.assertType(extract, "object", 'FunctionArgsInjector.extract() returns an object');
11
+ t.assertEquals(extract.name, null, 'FunctionArgsInjector.extract() process function name as expected');
12
+ t.assertEquals(extract.body, 'return a + b;', 'FunctionArgsInjector.extract() process function body as expected');
13
+ t.assertEquals(extract.args, ['a', 'b'], 'FunctionArgsInjector.extract() process function args as expected');
14
+
15
+ function Plop(foo, bar) {
16
+ return 'foo: ' + foo +', bar: ' + bar;
17
+ }
18
+ function Plip() { return 'plop'; }
19
+ function foo_bar(boz) {}
20
+ var gni = function ($bubu_bibi, __popo__) {};
21
+ var gno = function ( arg1, /*plop*/ arg2 ) { };
22
+ function issue129(term) {
23
+ // see issue #129
24
+ return term;
25
+ // see issue #129
26
+ }
27
+ t.assertEquals(injector.extract(Plop), {
28
+ name: 'Plop',
29
+ args: ['foo', 'bar'],
30
+ body: "return 'foo: ' + foo +', bar: ' + bar;"
31
+ }, 'FunctionArgsInjector.extract() handles named functions with arguments and body');
32
+ t.assertEquals(injector.extract(Plip), {
33
+ name: 'Plip',
34
+ args: [],
35
+ body: "return 'plop';"
36
+ }, 'FunctionArgsInjector.extract() handles functions with no arguments');
37
+ t.assertEquals(injector.extract(foo_bar), {
38
+ name: 'foo_bar',
39
+ args: ['boz'],
40
+ body: ""
41
+ }, 'FunctionArgsInjector.extract() handles functions with no body');
42
+ t.assertEquals(injector.extract(gni), {
43
+ name: null,
44
+ args: ['$bubu_bibi', '__popo__'],
45
+ body: ""
46
+ }, 'FunctionArgsInjector.extract() handles anonymous functions with complex args passed');
47
+ t.assertEquals(injector.extract(gno), {
48
+ name: null,
49
+ args: ['arg1', 'arg2'],
50
+ body: ""
51
+ }, 'FunctionArgsInjector.extract() handles can filter comments in function args');
52
+
53
+ t.comment('FunctionArgsInjector.process()');
54
+ var processed;
55
+ eval('processed = ' + injector.process({ a: 1, b: 2 }));
56
+
57
+ t.assertType(processed, "function", 'FunctionArgsInjector.process() processed a function');
58
+ t.assertEquals(processed(), 3, 'FunctionArgsInjector.process() processed the function correctly');
59
+
60
+ // Issue #129
61
+ var fnIssue129 = createInjector(issue129).process({term: 'fixed'});
62
+ t.assertEquals(fnIssue129('fixed'), 'fixed', 'FunctionArgsInjector.process() has issue #129 fixed');
63
+
64
+ t.done();
@@ -0,0 +1,121 @@
1
+ var fs = require('fs');
2
+
3
+ var t = casper.test;
4
+
5
+ casper.start();
6
+
7
+ t.comment('Tester.testEquals()');
8
+ t.assert(t.testEquals(null, null), 'Tester.testEquals() null equality');
9
+ t.assertNot(t.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality');
10
+ t.assert(t.testEquals("hi", "hi"), 'Tester.testEquals() string equality');
11
+ t.assertNot(t.testEquals("hi", "ih"), 'Tester.testEquals() string inequality');
12
+ t.assert(t.testEquals(5, 5), 'Tester.testEquals() number equality');
13
+ t.assertNot(t.testEquals("5", 5), 'Tester.testEquals() number equality without implicit cast');
14
+ t.assert(t.testEquals(5, 5.0), 'Tester.testEquals() number equality with cast');
15
+ t.assertNot(t.testEquals(5, 10), 'Tester.testEquals() number inequality');
16
+ t.assert(t.testEquals([], []), 'Tester.testEquals() empty array equality');
17
+ t.assert(t.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality');
18
+ t.assert(t.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality');
19
+ t.assertNot(t.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality');
20
+ t.assertNot(t.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality');
21
+ t.assertNot(t.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality');
22
+ t.assert(t.testEquals({}, {}), 'Tester.testEquals() empty object equality');
23
+ t.assert(t.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality');
24
+ t.assert(t.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality');
25
+ t.assertNot(t.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality');
26
+ t.assert(t.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');
27
+ t.assertNot(t.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');
28
+ t.assert(t.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality');
29
+ t.assertNot(t.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality');
30
+
31
+ t.comment('Tester.sortFiles()');
32
+ var testDirRoot = fs.pathJoin(phantom.casperPath, 'tests', 'testdir');
33
+ var files = t.findTestFiles(testDirRoot);
34
+ var expected = [
35
+ "01_a/abc.js",
36
+ "01_a/def.js",
37
+ "02_b/abc.js",
38
+ "03_a.js",
39
+ "03_b.js",
40
+ "04/01_init.js",
41
+ "04/02_do.js"
42
+ ].map(function(entry) {
43
+ return fs.pathJoin.apply(fs, [testDirRoot].concat(entry.split('/')));
44
+ });
45
+ t.assertEquals(files, expected, 'findTestFiles() find test files and sort them');
46
+
47
+ casper.thenOpen('tests/site/index.html', function() {
48
+ t.comment('Tester.assertTextExists()');
49
+ t.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text');
50
+ });
51
+
52
+ casper.then(function() {
53
+ t.comment('Tester.assert()');
54
+ t.assert(true, 'Tester.assert() works as expected');
55
+
56
+ t.comment('Tester.assertNot()');
57
+ t.assertNot(false, 'Tester.assertNot() works as expected');
58
+
59
+ t.comment('Tester.assertEquals()');
60
+ t.assertEquals(true, true, 'Tester.assertEquals() works as expected');
61
+
62
+ t.comment('Tester.assertNotEquals()');
63
+ t.assertNotEquals(true, false, 'Tester.assertNotEquals() works as expected');
64
+
65
+ t.comment('Tester.assertEval()');
66
+ t.assertEval(function() {
67
+ return true;
68
+ }, 'Tester.assertEval() works as expected');
69
+
70
+ t.comment('Tester.assertEvalEquals()');
71
+ t.assertEvalEquals(function() {
72
+ return 42;
73
+ }, 42, 'Tester.assertEvalEquals() works as expected');
74
+
75
+ t.comment('Tester.assertExists()');
76
+ t.assertExists('body', 'Tester.assertExists() works as expected');
77
+
78
+ t.comment('Tester.assertDoesntExist()');
79
+ t.assertDoesntExist('foobar', 'Tester.assertDoesntExist() works as expected');
80
+
81
+ t.comment('Tester.assertHttpStatus()');
82
+ // using file:// protocol, HTTP status is always null
83
+ t.assertHttpStatus(null, 'Tester.assertHttpStatus() works as expected');
84
+
85
+ t.comment('Tester.assertMatch()');
86
+ t.assertMatch("the lazy dog", /lazy/, 'Tester.assertMatch() works as expected');
87
+
88
+ t.comment('Tester.assertRaises()');
89
+ t.assertRaises(function() {
90
+ throw new Error('plop');
91
+ }, [], 'Tester.assertRaises() works as expected');
92
+
93
+ t.comment('Tester.assertResourceExists()');
94
+ t.assertResourceExists(/index\.html/, 'Tester.assertResourceExists() works as expected');
95
+
96
+ t.comment('Tester.assertTitle()');
97
+ t.assertTitle('CasperJS test index', 'Tester.assertTitle() works as expected');
98
+
99
+ t.comment('Tester.assertTitleMatch()');
100
+ t.assertTitleMatch(/test index/, 'Tester.assertTitleMatch() works as expected');
101
+
102
+ t.comment('Tester.assertType()');
103
+ t.assertType("plop", "string", "Tester.assertType() works as expected");
104
+
105
+ t.comment('Tester.assertUrlMatch()');
106
+ t.assertUrlMatch(/index\.html$/, "Tester.assertUrlMatch() works as expected");
107
+ });
108
+
109
+ casper.then(function() {
110
+ t.comment('Tester.getFailures()');
111
+ t.assertEquals(typeof t.getFailures().length, "number", "Tester.getFailures() works as expected");
112
+
113
+ var passCount = t.getPasses().length;
114
+ t.comment('Tester.getPasses()');
115
+ t.assertEquals(1, 1, "Rogue assertEquals pass case");
116
+ t.assertEquals(t.getPasses().length, passCount + 1, "Tester.getPasses() works as expected");
117
+ });
118
+
119
+ casper.run(function() {
120
+ t.done();
121
+ });
@@ -0,0 +1,209 @@
1
+ var utils = require('utils'), t = casper.test, x = require('casper').selectXPath;
2
+
3
+ t.comment('fileExt()');
4
+ (function() {
5
+ var testCases = {
6
+ 'foo.ext': 'ext',
7
+ 'FOO.EXT': 'ext',
8
+ 'a.ext': 'ext',
9
+ '.ext': 'ext',
10
+ 'toto.': '',
11
+ ' plop.ext ': 'ext'
12
+ };
13
+
14
+ for (var testCase in testCases) {
15
+ t.assertEquals(utils.fileExt(testCase), testCases[testCase], 'fileExt() extract file extension');
16
+ }
17
+ })();
18
+
19
+ t.comment('fillBlanks()');
20
+ (function() {
21
+ testCases = {
22
+ 'foo': 'foo ',
23
+ ' foo bar ': ' foo bar ',
24
+ ' foo bar ': ' foo bar '
25
+ };
26
+
27
+ for (var testCase in testCases) {
28
+ t.assertEquals(utils.fillBlanks(testCase, 10), testCases[testCase], 'fillBlanks() fills blanks');
29
+ }
30
+ })();
31
+
32
+ t.comment('getPropertyPath()');
33
+ (function() {
34
+ testCases = [
35
+ {
36
+ input: utils.getPropertyPath({}, 'a.b.c'),
37
+ output: undefined
38
+ },
39
+ {
40
+ input: utils.getPropertyPath([1, 2, 3], 'a.b.c'),
41
+ output: undefined
42
+ },
43
+ {
44
+ input: utils.getPropertyPath({ a: { b: { c: 1 } }, c: 2 }, 'a.b.c'),
45
+ output: 1
46
+ },
47
+ {
48
+ input: utils.getPropertyPath({ a: { b: { c: 1 } }, c: 2 }, 'a.b.x'),
49
+ output: undefined
50
+ },
51
+ {
52
+ input: utils.getPropertyPath({ a: { b: { c: 1 } }, c: 2 }, 'a.b'),
53
+ output: { c: 1 }
54
+ },
55
+ {
56
+ input: utils.getPropertyPath({ 'a-b': { 'c-d': 1} }, 'a-b.c-d'),
57
+ output: 1
58
+ },
59
+ {
60
+ input: utils.getPropertyPath({ 'a.b': { 'c.d': 1} }, 'a.b.c.d'),
61
+ output: undefined
62
+ }
63
+ ];
64
+ testCases.forEach(function(testCase) {
65
+ t.assertEquals(testCase.input, testCase.output, 'getPropertyPath() gets a property using a path');
66
+ });
67
+ })();
68
+
69
+ t.comment('isArray()');
70
+ (function() {
71
+ t.assertEquals(utils.isArray([]), true, 'isArray() checks for an Array');
72
+ t.assertEquals(utils.isArray({}), false, 'isArray() checks for an Array');
73
+ t.assertEquals(utils.isArray("foo"), false, 'isArray() checks for an Array');
74
+ })();
75
+
76
+ t.comment('isClipRect()');
77
+ (function() {
78
+ testCases = [
79
+ [{}, false],
80
+ [{top: 2}, false],
81
+ [{top: 2, left: 2, width: 2, height: 2}, true],
82
+ [{top: 2, left: 2, height: 2, width: 2}, true],
83
+ [{top: 2, left: 2, width: 2, height: new Date()}, false]
84
+ ];
85
+
86
+ testCases.forEach(function(testCase) {
87
+ t.assertEquals(utils.isClipRect(testCase[0]), testCase[1], 'isClipRect() checks for a ClipRect');
88
+ });
89
+ })();
90
+
91
+ t.comment('isObject()');
92
+ (function() {
93
+ t.assertEquals(utils.isObject({}), true, 'isObject() checks for an Object');
94
+ t.assertEquals(utils.isObject([]), true, 'isObject() checks for an Object');
95
+ t.assertEquals(utils.isObject(1), false, 'isObject() checks for an Object');
96
+ t.assertEquals(utils.isObject("1"), false, 'isObject() checks for an Object');
97
+ t.assertEquals(utils.isObject(function(){}), false, 'isObject() checks for an Object');
98
+ t.assertEquals(utils.isObject(new Function('return {};')()), true, 'isObject() checks for an Object');
99
+ t.assertEquals(utils.isObject(require('webpage').create()), true, 'isObject() checks for an Object');
100
+ t.assertEquals(utils.isObject(null), false, 'isObject() checks for an Object');
101
+ })();
102
+
103
+ t.comment('isValidSelector()');
104
+ (function() {
105
+ t.assertEquals(utils.isValidSelector({}), false, 'isValidSelector() checks for a valid selector');
106
+ t.assertEquals(utils.isValidSelector(""), false, 'isValidSelector() checks for a valid selector');
107
+ t.assertEquals(utils.isValidSelector("a"), true, 'isValidSelector() checks for a valid selector');
108
+ t.assertEquals(utils.isValidSelector('div#plop form[name="form"] input[type="submit"]'), true, 'isValidSelector() checks for a valid selector');
109
+ t.assertEquals(utils.isValidSelector(x('//a')), true, 'isValidSelector() checks for a valid selector');
110
+ t.assertEquals(utils.isValidSelector({
111
+ type: "css",
112
+ path: 'div#plop form[name="form"] input[type="submit"]'
113
+ }), true, 'isValidSelector() checks for a valid selector');
114
+ t.assertEquals(utils.isValidSelector({
115
+ type: "xpath",
116
+ path: '//a'
117
+ }), true, 'isValidSelector() checks for a valid selector');
118
+ t.assertEquals(utils.isValidSelector({
119
+ type: "css"
120
+ }), false, 'isValidSelector() checks for a valid selector');
121
+ t.assertEquals(utils.isValidSelector({
122
+ type: "xpath"
123
+ }), false, 'isValidSelector() checks for a valid selector');
124
+ t.assertEquals(utils.isValidSelector({
125
+ type: "css3",
126
+ path: "a"
127
+ }), false, 'isValidSelector() checks for a valid selector');
128
+ })();
129
+
130
+ t.comment('isWebPage()');
131
+ (function() {
132
+ var pageModule = require('webpage');
133
+ t.assertEquals(utils.isWebPage(pageModule), false, 'isWebPage() checks for a WebPage instance');
134
+ t.assertEquals(utils.isWebPage(pageModule.create()), true, 'isWebPage() checks for a WebPage instance');
135
+ t.assertEquals(utils.isWebPage(null), false, 'isWebPage() checks for a WebPage instance');
136
+ })();
137
+
138
+ t.comment('isJsFile()');
139
+ (function() {
140
+ testCases = {
141
+ '': false,
142
+ 'toto.png': false,
143
+ 'plop': false,
144
+ 'gniii.coffee': true,
145
+ 'script.js': true
146
+ };
147
+
148
+ for (var testCase in testCases) {
149
+ t.assertEquals(utils.isJsFile(testCase), testCases[testCase], 'isJsFile() checks for js file');
150
+ }
151
+ })();
152
+
153
+ t.comment('mergeObjects()');
154
+ (function() {
155
+ testCases = [
156
+ {
157
+ obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2}
158
+ },
159
+ {
160
+ obj1: {}, obj2: {a: 1}, merged: {a: 1}
161
+ },
162
+ {
163
+ obj1: {a: 1}, obj2: {}, merged: {a: 1}
164
+ },
165
+ {
166
+ obj1: {a: 1}, obj2: {a: 2}, merged: {a: 2}
167
+ },
168
+ {
169
+ obj1: {x: 0, double: function(){return this.x*2;}},
170
+ obj2: {triple: function(){return this.x*3;}},
171
+ merged: {
172
+ x: 0,
173
+ double: function(){return this.x*2;},
174
+ triple: function(){return this.x*3;}
175
+ }
176
+ }
177
+ ];
178
+
179
+ testCases.forEach(function(testCase) {
180
+ t.assertEquals(utils.mergeObjects(testCase.obj1, testCase.obj2), testCase.merged, 'mergeObjects() can merge objects');
181
+ });
182
+ })();
183
+
184
+ t.comment('unique()');
185
+ (function() {
186
+ testCases = [
187
+ {
188
+ input: [1,2,3],
189
+ output: [1,2,3]
190
+ },
191
+ {
192
+ input: [1,2,3,2,1],
193
+ output: [1,2,3]
194
+ },
195
+ {
196
+ input: ["foo", "bar", "foo"],
197
+ output: ["foo", "bar"]
198
+ },
199
+ {
200
+ input: [],
201
+ output: []
202
+ }
203
+ ];
204
+ testCases.forEach(function(testCase) {
205
+ t.assertEquals(utils.unique(testCase.input), testCase.output, 'unique() computes unique values of an array');
206
+ });
207
+ })();
208
+
209
+ t.done();