casperjs 1.0.0.RC1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/CHANGELOG.md +289 -1
  2. data/CONTRIBUTING.md +93 -0
  3. data/CONTRIBUTORS.md +53 -0
  4. data/README.md +29 -1
  5. data/batchbin/casperjs.bat +5 -0
  6. data/bin/bootstrap.js +164 -114
  7. data/bin/casperjs +84 -0
  8. data/bin/usage.txt +2 -1
  9. data/casperjs.gemspec +7 -4
  10. data/modules/casper.js +661 -239
  11. data/modules/clientutils.js +240 -54
  12. data/modules/colorizer.js +2 -1
  13. data/modules/events.js +13 -1
  14. data/modules/http.js +70 -0
  15. data/modules/mouse.js +1 -2
  16. data/modules/pagestack.js +166 -0
  17. data/modules/tester.js +1013 -659
  18. data/modules/utils.js +519 -367
  19. data/modules/vendors/coffee-script.js +2 -2
  20. data/modules/xunit.js +56 -24
  21. data/package.json +2 -2
  22. data/rpm/casperjs.spec +203 -0
  23. data/rpm/mkrpm.sh +25 -0
  24. data/rubybin/casperjs +9 -4
  25. data/samples/bbcshots.coffee +11 -10
  26. data/samples/bbcshots.js +16 -15
  27. data/samples/cliplay.js +3 -0
  28. data/samples/customevents.js +3 -0
  29. data/samples/customlogging.coffee +17 -19
  30. data/samples/customlogging.js +26 -27
  31. data/samples/download.js +4 -1
  32. data/samples/dynamic.js +3 -0
  33. data/samples/each.js +3 -0
  34. data/samples/events.js +4 -2
  35. data/samples/extends.js +4 -1
  36. data/samples/googlelinks.coffee +4 -1
  37. data/samples/googlelinks.js +10 -3
  38. data/samples/googlematch.coffee +1 -1
  39. data/samples/googlematch.js +5 -2
  40. data/samples/googlepagination.js +4 -1
  41. data/samples/googletesting.js +3 -0
  42. data/samples/logcolor.js +3 -0
  43. data/samples/metaextract.js +3 -0
  44. data/samples/multirun.js +3 -0
  45. data/samples/screenshot.js +4 -1
  46. data/samples/statushandlers.js +4 -1
  47. data/samples/steptimeout.js +3 -0
  48. data/samples/timeout.js +4 -1
  49. data/samples/translate.coffee +23 -0
  50. data/samples/translate.js +30 -0
  51. data/tests/commands/mytest.js +14 -0
  52. data/tests/commands/script.js +3 -0
  53. data/tests/run.js +82 -37
  54. data/tests/sample_modules/config.json +1 -0
  55. data/tests/sample_modules/csmodule.coffee +1 -0
  56. data/tests/sample_modules/jsmodule.js +1 -0
  57. data/tests/selftest.js +57 -0
  58. data/tests/site/dummy.js +1 -0
  59. data/tests/site/field-array.html +14 -0
  60. data/tests/site/frame1.html +10 -0
  61. data/tests/site/frame2.html +11 -0
  62. data/tests/site/frame3.html +11 -0
  63. data/tests/site/frames.html +12 -0
  64. data/tests/site/global.html +6 -1
  65. data/tests/site/includes/include1.js +6 -0
  66. data/tests/site/includes/include2.js +6 -0
  67. data/tests/site/index.html +3 -0
  68. data/tests/site/popup.html +19 -0
  69. data/tests/site/resources.html +7 -8
  70. data/tests/site/urls.html +14 -0
  71. data/tests/suites/casper/agent.js +3 -1
  72. data/tests/suites/casper/alert.js +14 -0
  73. data/tests/suites/casper/auth.js +24 -0
  74. data/tests/suites/casper/capture.js +12 -12
  75. data/tests/suites/casper/click.js +11 -1
  76. data/tests/suites/casper/confirm.js +24 -16
  77. data/tests/suites/casper/debug.js +10 -0
  78. data/tests/suites/casper/elementattribute.js +3 -1
  79. data/tests/suites/casper/encode.js +6 -2
  80. data/tests/suites/casper/evaluate.js +78 -18
  81. data/tests/suites/casper/events.js +3 -1
  82. data/tests/suites/casper/exists.js +3 -1
  83. data/tests/suites/casper/fetchtext.js +3 -1
  84. data/tests/suites/casper/flow.coffee +1 -1
  85. data/tests/suites/casper/formfill.js +39 -4
  86. data/tests/suites/casper/frames.js +43 -0
  87. data/tests/suites/casper/global.js +9 -3
  88. data/tests/suites/casper/headers.js +41 -0
  89. data/tests/suites/casper/history.js +3 -1
  90. data/tests/suites/casper/hooks.js +3 -1
  91. data/tests/suites/casper/keys.js +15 -0
  92. data/tests/suites/casper/location.js +21 -0
  93. data/tests/suites/casper/logging.js +3 -1
  94. data/tests/suites/casper/mouseevents.js +3 -1
  95. data/tests/suites/casper/onerror.js +3 -1
  96. data/tests/suites/casper/open.js +63 -1
  97. data/tests/suites/casper/popup.js +86 -0
  98. data/tests/suites/casper/prompt.js +11 -15
  99. data/tests/suites/casper/request.js +36 -0
  100. data/tests/suites/casper/resources.coffee +5 -5
  101. data/tests/suites/casper/scripts.js +32 -0
  102. data/tests/suites/casper/start.js +3 -1
  103. data/tests/suites/casper/steps.js +4 -2
  104. data/tests/suites/casper/urls.js +21 -0
  105. data/tests/suites/casper/viewport.js +3 -1
  106. data/tests/suites/casper/visible.js +3 -1
  107. data/tests/suites/casper/wait.js +22 -12
  108. data/tests/suites/casper/xpath.js +3 -1
  109. data/tests/suites/cli.js +3 -1
  110. data/tests/suites/clientutils.js +57 -3
  111. data/tests/suites/coffee.coffee +1 -1
  112. data/tests/suites/fs.js +3 -1
  113. data/tests/suites/http_status.js +19 -3
  114. data/tests/suites/popup.js +33 -0
  115. data/tests/suites/require.js +31 -0
  116. data/tests/suites/tester.js +134 -29
  117. data/tests/suites/utils.js +108 -8
  118. data/tests/suites/xunit.js +37 -6
  119. metadata +49 -15
  120. data/modules/injector.js +0 -93
  121. data/tests/suites/injector.js +0 -64
@@ -0,0 +1 @@
1
+ {"ok": true}
@@ -0,0 +1 @@
1
+ exports.ok = true
@@ -0,0 +1 @@
1
+ exports.ok = true;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * CasperJS local HTTP test server
3
+ */
4
+
5
+ /*global phantom casper require*/
6
+
7
+ var colorizer = require('colorizer').create('Colorizer');
8
+ var fs = require('fs');
9
+ var utils = require('utils');
10
+ var server = require('webserver').create();
11
+ var service;
12
+ var testServerPort = 54321;
13
+
14
+ function info(message) {
15
+ "use strict";
16
+ console.log(colorizer.colorize('INFO', 'INFO_BAR') + ' ' + message);
17
+ }
18
+
19
+ service = server.listen(testServerPort, function(request, response) {
20
+ "use strict";
21
+ var requestPath = request.url;
22
+ if (requestPath.indexOf('?') !== -1) {
23
+ requestPath = request.url.split('?')[0];
24
+ }
25
+ var pageFile = fs.pathJoin(phantom.casperPath, requestPath);
26
+ if (!fs.exists(pageFile) || !fs.isFile(pageFile)) {
27
+ response.statusCode = 404;
28
+ console.log(utils.format('Test server url not found: %s (file: %s)', request.url, pageFile), "warning");
29
+ response.write("404 - NOT FOUND");
30
+ } else {
31
+ var headers = {};
32
+ if (/js$/.test(pageFile)) {
33
+ headers['Content-Type'] = "application/javascript";
34
+ }
35
+ response.writeHead(200, headers);
36
+ response.write(fs.read(pageFile));
37
+ }
38
+ response.close();
39
+ });
40
+
41
+ // overriding Casper.open to prefix all test urls
42
+ casper.setFilter('open.location', function(location) {
43
+ "use strict";
44
+ if (/^file/.test(location)) {
45
+ return location;
46
+ }
47
+ if (!/^http/.test(location)) {
48
+ return utils.format('http://localhost:%d/%s', testServerPort, location);
49
+ }
50
+ return location;
51
+ });
52
+
53
+ // test suites completion listener
54
+ casper.test.on('tests.complete', function() {
55
+ "use strict";
56
+ server.close();
57
+ });
@@ -0,0 +1 @@
1
+ document.write('foo');
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
+ <title>CasperJS test form</title>
6
+ </head>
7
+ <body>
8
+ <form action="result.html" enctype="multipart/form-data">
9
+ <input type="text" name="foo[bar]">
10
+ <input type="text" name="foo[baz]">
11
+ <input type="submit" name="submit" value="submit">
12
+ </form>
13
+ </body>
14
+ </html>
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <title>CasperJS frame 1</title>
6
+ </head>
7
+ <body id="f1">
8
+ <h1>This is frame 1.</h1>
9
+ </body>
10
+ </html>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <title>CasperJS frame 2</title>
6
+ </head>
7
+ <body id="f2">
8
+ <h1>This is frame 2.</h1>
9
+ <p><a href="frame3.html" target="frame2">frame 3</a></p>
10
+ </body>
11
+ </html>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <title>CasperJS frame 3</title>
6
+ </head>
7
+ <body id="f3">
8
+ <h1>This is frame 3.</h1>
9
+ <p><a href="frame2.html">frame 2</a></p>
10
+ </body>
11
+ </html>
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
+ <title>CasperJS test frames</title>
6
+ </head>
7
+ <frameset cols="50%,50%%">
8
+ <frame src="frame1.html" name="frame1">
9
+ <frame src="frame2.html" name="frame2">
10
+ </frameset>
11
+ </html>
12
+
@@ -4,6 +4,11 @@
4
4
  <script type="text/javascript">
5
5
  var myGlobal = 'awesome string';
6
6
  var myUnencodableGlobal = document;
7
+ var myObject = {
8
+ foo: {
9
+ bar: 'baz'
10
+ }
11
+ };
7
12
  </script>
8
13
  </body>
9
- </html>
14
+ </html>
@@ -0,0 +1,6 @@
1
+ (function() {
2
+ var elem = document.createElement('div');
3
+ elem.setAttribute('id', 'include1');
4
+ elem.appendChild(document.createTextNode('include1'));
5
+ document.querySelector('body').appendChild(elem);
6
+ })();
@@ -0,0 +1,6 @@
1
+ (function() {
2
+ var elem = document.createElement('div');
3
+ elem.setAttribute('id', 'include2');
4
+ elem.appendChild(document.createTextNode('include2'));
5
+ document.querySelector('body').appendChild(elem);
6
+ })();
@@ -13,5 +13,8 @@
13
13
  <li>two</li>
14
14
  <li>three</li>
15
15
  </ul>
16
+ <input type="text" name="dummy_name" value="dummy_value" />
17
+ <h1>Title</h1>
18
+ <p id="hidden" style="display:none">I'm hidden.</p>
16
19
  </body>
17
20
  </html>
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <title>CasperJS test popup</title>
6
+ </head>
7
+ <a href="." class="close">close</a>
8
+ <body>
9
+ <a href="/tests/site/form.html" target="_blank">new window</a>
10
+ <script>
11
+ var w = window.open("http://localhost:54321/tests/site/index.html",
12
+ "popup", "menubar=no, status=no, scrollbars=no, menubar=no, width=400, height=300");
13
+ document.querySelector('a').onclick = function onclick(evt) {
14
+ evt.preventDefault();
15
+ w.close();
16
+ };
17
+ </script>
18
+ </body>
19
+ </html>
@@ -3,14 +3,13 @@
3
3
  <head>
4
4
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
5
  <title>CasperJS test resource</title>
6
- <script>
7
- setTimeout(function () {
8
- var path = "images/phantom.png?" + new Date();
9
- document.querySelector("img").setAttribute("src", path);
10
- }, 1000);
11
- </script>
12
6
  </head>
13
7
  <body>
14
- <img width="55" height="55" border="1">
8
+ <script id="loader"></script>
9
+ <script>
10
+ setTimeout(function () {
11
+ document.querySelector("#loader").setAttribute("src", "dummy.js");
12
+ }, 1000);
13
+ </script>
15
14
  </body>
16
- </html>
15
+ </html>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <title>CasperJS url tests</title>
6
+ </head>
7
+ <body>
8
+ <ul>
9
+ <li><a href="?test=Forlì">raw unicode</a></li>
10
+ <li><a href="?test=Forl%EC">escaped</a></li>
11
+ <li><a href="?test=Forl%C3%AC">uri encoded</a></li>
12
+ </ul>
13
+ </body>
14
+ </html>
@@ -1,3 +1,5 @@
1
+ /*global casper*/
2
+ /*jshint strict:false*/
1
3
  function testUA(ua, match) {
2
4
  casper.test.assertMatch(
3
5
  ua, match, 'Default user agent matches ' + match
@@ -20,5 +22,5 @@ casper.thenOpen('tests/site/index.html');
20
22
 
21
23
  casper.run(function() {
22
24
  this.removeListener('resource.requested', fetchUA);
23
- this.test.done();
25
+ this.test.done(3);
24
26
  });
@@ -0,0 +1,14 @@
1
+ /*global casper*/
2
+ /*jshint strict:false*/
3
+
4
+ var ok = false;
5
+
6
+ casper.on('remote.alert', function(message) {
7
+ ok = message === 'plop';
8
+ });
9
+
10
+ casper.start('tests/site/alert.html').run(function() {
11
+ this.test.assert(ok, 'alert event has been intercepted');
12
+ this.removeAllListeners('remote.alert');
13
+ this.test.done(1);
14
+ });
@@ -0,0 +1,24 @@
1
+ /*global casper*/
2
+ /*jshint strict:false maxstatements:99*/
3
+
4
+ casper.start('tests/site/index.html');
5
+
6
+ casper.configureHttpAuth('http://localhost/');
7
+ casper.test.assertEquals(casper.page.settings.userName, undefined);
8
+ casper.test.assertEquals(casper.page.settings.password, undefined);
9
+
10
+ casper.configureHttpAuth('http://niko:plop@localhost/');
11
+ casper.test.assertEquals(casper.page.settings.userName, 'niko');
12
+ casper.test.assertEquals(casper.page.settings.password, 'plop');
13
+
14
+ casper.configureHttpAuth('http://localhost/', {username: 'john', password: 'doe'});
15
+ casper.test.assertEquals(casper.page.settings.userName, 'john');
16
+ casper.test.assertEquals(casper.page.settings.password, 'doe');
17
+
18
+ casper.configureHttpAuth('http://niko:plop@localhost/', {username: 'john', password: 'doe'});
19
+ casper.test.assertEquals(casper.page.settings.userName, 'niko');
20
+ casper.test.assertEquals(casper.page.settings.password, 'plop');
21
+
22
+ casper.run(function() {
23
+ this.test.done(8);
24
+ });
@@ -1,3 +1,5 @@
1
+ /*global casper*/
2
+ /*jshint strict:false*/
1
3
  var fs = require('fs'), testFile = '/tmp/__casper_test_capture.png';
2
4
 
3
5
  if (fs.exists(testFile) && fs.isFile(testFile)) {
@@ -11,21 +13,19 @@ casper.start('tests/site/index.html', function() {
11
13
  this.test.assert(fs.isFile(testFile), 'Casper.capture() captured a screenshot');
12
14
  });
13
15
 
14
- if (phantom.version.major === 1 && phantom.version.minor >= 6) {
15
- casper.thenOpen('tests/site/index.html', function() {
16
- this.test.comment('Casper.captureBase64()');
17
- this.test.assert(this.captureBase64('png').length > 0,
18
- 'Casper.captureBase64() rendered a page capture as base64');
19
- this.test.assert(this.captureBase64('png', 'ul').length > 0,
20
- 'Casper.captureBase64() rendered a capture from a selector as base64');
21
- this.test.assert(this.captureBase64('png', {top: 0, left: 0, width: 30, height: 30}).length > 0,
22
- 'Casper.captureBase64() rendered a capture from a clipRect as base64');
23
- });
24
- }
16
+ casper.thenOpen('tests/site/index.html', function() {
17
+ this.test.comment('Casper.captureBase64()');
18
+ this.test.assert(this.captureBase64('png').length > 0,
19
+ 'Casper.captureBase64() rendered a page capture as base64');
20
+ this.test.assert(this.captureBase64('png', 'ul').length > 0,
21
+ 'Casper.captureBase64() rendered a capture from a selector as base64');
22
+ this.test.assert(this.captureBase64('png', {top: 0, left: 0, width: 30, height: 30}).length > 0,
23
+ 'Casper.captureBase64() rendered a capture from a clipRect as base64');
24
+ });
25
25
 
26
26
  casper.run(function() {
27
27
  try {
28
28
  fs.remove(testFile);
29
29
  } catch(e) {}
30
- this.test.done();
30
+ this.test.done(4);
31
31
  });
@@ -1,3 +1,5 @@
1
+ /*global casper*/
2
+ /*jshint strict:false*/
1
3
  casper.start('tests/site/index.html', function() {
2
4
  this.click('a[href="test.html"]');
3
5
  });
@@ -56,6 +58,14 @@ casper.then(function() {
56
58
  this.test.assertEquals(results.testmove, [200, 100], 'Mouse.move() has moved to the specified position');
57
59
  });
58
60
 
61
+ // element focus on click
62
+ casper.then(function() {
63
+ this.page.content = '<form><input type="text" name="foo"></form>'
64
+ this.click('form input[name=foo]')
65
+ this.page.sendEvent('keypress', 'bar');
66
+ this.test.assertEquals(this.getFormValues('form')['foo'], 'bar', 'Casper.click() sets the focus on clicked element');
67
+ });
68
+
59
69
  casper.run(function() {
60
- this.test.done();
70
+ this.test.done(22);
61
71
  });
@@ -1,21 +1,29 @@
1
- // skip this test for phantom versions < 1.5
2
- if (phantom.version.major === 1 && phantom.version.minor < 6) {
3
- casper.test.comment('Skipped tests, PhantomJS 1.6 required');
4
- casper.test.done();
5
- } else {
6
- var received;
1
+ /*global casper*/
2
+ /*jshint strict:false*/
3
+ var received;
7
4
 
5
+ casper.setFilter('page.confirm', function(message) {
6
+ received = message;
7
+ return true;
8
+ });
9
+
10
+ casper.start('tests/site/confirm.html', function() {
11
+ this.test.assert(this.getGlobal('confirmed'), 'confirmation dialog accepted');
12
+ });
13
+
14
+ casper.then(function() {
15
+ //remove the page.confirm event filter so we can add a new one
16
+ casper.removeAllFilters('page.confirm')
8
17
  casper.setFilter('page.confirm', function(message) {
9
- received = message;
10
- return true;
18
+ return false;
11
19
  });
20
+ });
12
21
 
13
- casper.start('tests/site/confirm.html', function() {
14
- this.test.assert(this.getGlobal('confirmed'), 'confirmation received');
15
- });
22
+ casper.thenOpen('/tests/site/confirm.html', function() {
23
+ this.test.assertNot(this.getGlobal('confirmed'), 'confirmation dialog canceled');
24
+ });
16
25
 
17
- casper.run(function() {
18
- this.test.assertEquals(received, 'are you sure?', 'confirmation message is ok');
19
- this.test.done();
20
- });
21
- }
26
+ casper.run(function() {
27
+ this.test.assertEquals(received, 'are you sure?', 'confirmation message is ok');
28
+ this.test.done(3);
29
+ });
@@ -0,0 +1,10 @@
1
+ /*global casper*/
2
+ /*jshint strict:false*/
3
+ casper.start('tests/site/index.html', function() {
4
+ this.test.assertEquals(this.getHTML('ul li'), 'one', 'Casper.getHTML() retrieves inner HTML by default');
5
+ this.test.assertEquals(this.getHTML('ul li', true), '<li>one</li>', 'Casper.getHTML() can retrieve outer HTML');
6
+ });
7
+
8
+ casper.run(function() {
9
+ casper.test.done(2);
10
+ });
@@ -1,8 +1,10 @@
1
+ /*global casper*/
2
+ /*jshint strict:false*/
1
3
  casper.start('tests/site/elementattribute.html', function() {
2
4
  this.test.comment('Casper.getElementAttribute()');
3
5
  this.test.assertEquals(this.getElementAttribute('.testo','data-stuff'), 'beautiful string', 'Casper.getElementAttribute() works as intended');
4
6
  });
5
7
 
6
8
  casper.run(function() {
7
- this.test.done();
9
+ this.test.done(1);
8
10
  });
@@ -1,6 +1,10 @@
1
+ /*global casper*/
2
+ /*jshint strict:false*/
1
3
  var fs = require('fs');
2
4
 
3
- casper.start('tests/site/index.html', function() {
5
+ // FIXME: we're using local url scheme until https://github.com/ariya/phantomjs/pull/288 is
6
+ // possibly merged
7
+ casper.start('file://' + phantom.casperPath + '/tests/site/index.html', function() {
4
8
  var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png';
5
9
  var image = this.base64encode(imageUrl);
6
10
 
@@ -16,5 +20,5 @@ casper.start('tests/site/index.html', function() {
16
20
  });
17
21
 
18
22
  casper.run(function() {
19
- this.test.done();
23
+ this.test.done(2);
20
24
  });
@@ -1,27 +1,87 @@
1
+ /*global casper*/
2
+ /*jshint strict:false maxparams:99*/
1
3
  casper.test.comment('Casper.evaluate()');
2
4
 
3
5
  casper.start();
4
6
 
5
- var params = {
6
- "boolean true": true,
7
- "boolean false": false,
8
- "int number": 42,
9
- "float number": 1337.42,
10
- "string": "plop! \"Ÿ£$\" 'no'",
11
- "array": [1, 2, 3],
12
- "object": {a: 1, b: 2}
7
+ var context = {
8
+ "_boolean_true": true,
9
+ "_boolean_false": false,
10
+ "_int_number": 42,
11
+ "_float_number": 1337.42,
12
+ "_string": "plop! \"Ÿ£$\" 'no'",
13
+ "_array": [1, 2, 3],
14
+ "_object": {a: 1, b: 2},
15
+ "_function": function(){console.log('ok');}
13
16
  };
14
17
 
15
- var casperParams = casper.evaluate(function() {
16
- return __casper_params__;
17
- }, params);
18
+ var result = casper.evaluate(function(_boolean_true,
19
+ _boolean_false,
20
+ _int_number,
21
+ _float_number,
22
+ _string,
23
+ _array,
24
+ _object,
25
+ _function) {
26
+ return [].map.call(arguments, function(arg) {
27
+ return typeof(arg);
28
+ });
29
+ }, context);
18
30
 
19
- casper.test.assertType(casperParams, "object", 'Casper.evaluate() exposes parameters in a dedicated object');
20
- casper.test.assertEquals(Object.keys(casperParams).length, 7, 'Casper.evaluate() object containing parameters has the correct length');
31
+ casper.test.assertEquals(result.toString(),
32
+ ['boolean', 'boolean', 'number', 'number', 'string', 'object', 'object', 'function'].toString(),
33
+ 'Casper.evaluate() handles passed argument context correcly');
21
34
 
22
- for (var param in casperParams) {
23
- casper.test.assertEquals(JSON.stringify(casperParams[param]), JSON.stringify(params[param]), 'Casper.evaluate() can pass a ' + param);
24
- casper.test.assertEquals(typeof casperParams[param], typeof params[param], 'Casper.evaluate() preserves the ' + param + ' type');
25
- }
35
+ // no context
36
+ casper.test.assertEquals(casper.evaluate(function() {
37
+ return 42;
38
+ }), 42, 'Casper.evaluate() handles evaluation with no context passed');
26
39
 
27
- casper.test.done();
40
+ // object context (previous casperjs versions compatibility mode)
41
+ casper.test.assertEquals(casper.evaluate(function(a) {
42
+ return [a];
43
+ }, {a: "foo"}), ["foo"], 'Casper.evaluate() accepts an object as arguments context');
44
+ casper.test.assertEquals(casper.evaluate(function(a, b) {
45
+ return [a, b];
46
+ }, {a: "foo", b: "bar"}), ["foo", "bar"], 'Casper.evaluate() accepts an object as arguments context');
47
+ casper.test.assertEquals(casper.evaluate(function(a, b, c) {
48
+ return [a, b, c];
49
+ }, {a: "foo", b: "bar", c: "baz"}), ["foo", "bar", "baz"], 'Casper.evaluate() accepts an object as arguments context');
50
+
51
+ // array context
52
+ casper.test.assertEquals(casper.evaluate(function(a) {
53
+ return [a];
54
+ }, ["foo"]), ["foo"], 'Casper.evaluate() accepts an array as arguments context');
55
+ casper.test.assertEquals(casper.evaluate(function(a, b) {
56
+ return [a, b];
57
+ }, ["foo", "bar"]), ["foo", "bar"], 'Casper.evaluate() accepts an array as arguments context');
58
+ casper.test.assertEquals(casper.evaluate(function(a, b, c) {
59
+ return [a, b, c];
60
+ }, ["foo", "bar", "baz"]), ["foo", "bar", "baz"], 'Casper.evaluate() accepts an array as arguments context');
61
+
62
+ // natural arguments context (phantomjs equivalent)
63
+ casper.test.assertEquals(casper.evaluate(function(a) {
64
+ return [a];
65
+ }, "foo"), ["foo"], 'Casper.evaluate() accepts natural arguments context');
66
+ casper.test.assertEquals(casper.evaluate(function(a, b) {
67
+ return [a, b];
68
+ }, "foo", "bar"), ["foo", "bar"], 'Casper.evaluate() accepts natural arguments context');
69
+ casper.test.assertEquals(casper.evaluate(function(a, b, c) {
70
+ return [a, b, c];
71
+ }, "foo", "bar", "baz"), ["foo", "bar", "baz"], 'Casper.evaluate() accepts natural arguments context');
72
+
73
+ casper.start().thenEvaluate(function(a, b) {
74
+ window.a = a
75
+ window.b = b;
76
+ }, "foo", "bar");
77
+
78
+ casper.then(function() {
79
+ this.test.comment('Casper.thenEvaluate()');
80
+ this.test.assertEquals(this.getGlobal('a'), "foo", "Casper.thenEvaluate() sets args");
81
+ this.test.assertEquals(this.getGlobal('b'), "bar",
82
+ "Casper.thenEvaluate() sets args the same way evaluate() does");
83
+ });
84
+
85
+ casper.run(function() {
86
+ this.test.done(13);
87
+ });