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,51 @@
1
+ /*
2
+ * Capture multiple pages of google search results
3
+ *
4
+ * Usage: $ casperjs googlepagination.coffee my search terms
5
+ *
6
+ * (all arguments will be used as the query)
7
+ */
8
+
9
+ var casper = require("casper").create();
10
+ var currentPage = 1;
11
+
12
+ if (casper.cli.args.length === 0) {
13
+ casper
14
+ .echo("Usage: $ casperjs googlepagination.js my search terms")
15
+ .exit(1)
16
+ ;
17
+ }
18
+
19
+ var processPage = function() {
20
+ var url;
21
+ this.echo("capturing page " + currentPage);
22
+ this.capture("google-results-p" + currentPage + ".png");
23
+
24
+ // don't go too far down the rabbit hole
25
+ if (currentPage >= 5) {
26
+ return;
27
+ }
28
+
29
+ if (this.exists("#pnnext")) {
30
+ currentPage++;
31
+ this.echo("requesting next page: " + currentPage);
32
+ url = this.getCurrentUrl();
33
+ this.thenClick("#pnnext").then(function() {
34
+ this.waitFor(function() {
35
+ return url !== this.getCurrentUrl();
36
+ }, processPage);
37
+ });
38
+ } else {
39
+ this.echo("that's all, folks.");
40
+ }
41
+ };
42
+
43
+ casper.start("http://google.fr/", function() {
44
+ this.fill('form[action="/search"]', {
45
+ q: casper.cli.args.join(" ")
46
+ }, true);
47
+ });
48
+
49
+ casper.then(processPage);
50
+
51
+ casper.run();
@@ -0,0 +1,17 @@
1
+ casper = require("casper").create
2
+ logLevel: "debug"
3
+
4
+ casper.start "http://www.google.fr/", ->
5
+ @test.assertTitle "Google", "google homepage title is the one expected"
6
+ @test.assertExists 'form[action="/search"]', "main form is found"
7
+ @fill 'form[action="/search"]', q: "foo", true
8
+
9
+ casper.then ->
10
+ @test.assertTitle "foo - Recherche Google", "google title is ok"
11
+ @test.assertUrlMatch /q=foo/, "search term has been submitted"
12
+ @test.assertEval (->
13
+ __utils__.findAll("h3.r").length >= 10
14
+ ), "google search for \"foo\" retrieves 10 or more results"
15
+
16
+ casper.run ->
17
+ @test.renderResults true
@@ -0,0 +1,23 @@
1
+ var casper = require("casper").create({
2
+ logLevel: "debug"
3
+ });
4
+
5
+ casper.start("http://www.google.fr/", function() {
6
+ this.test.assertTitle("Google", "google homepage title is the one expected");
7
+ this.test.assertExists('form[action="/search"]', "main form is found");
8
+ this.fill('form[action="/search"]', {
9
+ q: "foo"
10
+ }, true);
11
+ });
12
+
13
+ casper.then(function() {
14
+ this.test.assertTitle("foo - Recherche Google", "google title is ok");
15
+ this.test.assertUrlMatch(/q=foo/, "search term has been submitted");
16
+ this.test.assertEval((function() {
17
+ return __utils__.findAll("h3.r").length >= 10;
18
+ }), "google search for \"foo\" retrieves 10 or more results");
19
+ });
20
+
21
+ casper.run(function() {
22
+ this.test.renderResults(true);
23
+ });
@@ -0,0 +1,10 @@
1
+ casper = require("casper").create
2
+ verbose: true
3
+ logLevel: "debug"
4
+
5
+ casper.log "this is a debug message", "debug"
6
+ casper.log "and an informative one", "info"
7
+ casper.log "and a warning", "warning"
8
+ casper.log "and an error", "error"
9
+
10
+ casper.exit()
@@ -0,0 +1,11 @@
1
+ var casper = require("casper").create({
2
+ verbose: true,
3
+ logLevel: "debug"
4
+ });
5
+
6
+ casper.log("this is a debug message", "debug");
7
+ casper.log("and an informative one", "info");
8
+ casper.log("and a warning", "warning");
9
+ casper.log("and an error", "error");
10
+
11
+ casper.exit();
@@ -0,0 +1,23 @@
1
+ casper = require("casper").create()
2
+ url = casper.cli.get 0
3
+ metas = []
4
+
5
+ if not url
6
+ casper
7
+ .echo("Usage: $ casperjs metaextract.coffee <url>")
8
+ .exit 1
9
+
10
+ casper.start url, ->
11
+ metas = @evaluate ->
12
+ metas = []
13
+ castarray = (arr) -> [].slice.call(arr)
14
+ for elem in castarray document.querySelectorAll "meta"
15
+ meta = {}
16
+ for attr in castarray elem.attributes
17
+ meta[attr.name] = attr.value
18
+ metas.push meta
19
+ metas
20
+
21
+ casper.run ->
22
+ require("utils").dump metas
23
+ this.exit()
@@ -0,0 +1,29 @@
1
+ var casper = require("casper").create();
2
+ var url = casper.cli.get(0);
3
+ var metas = [];
4
+
5
+ if (!url) {
6
+ casper
7
+ .echo("Usage: $ casperjs metaextract.js <url>")
8
+ .exit(1)
9
+ ;
10
+ }
11
+
12
+ casper.start(url, function() {
13
+ metas = this.evaluate(function() {
14
+ var metas = [];
15
+ [].forEach.call(document.querySelectorAll("meta"), function(elem) {
16
+ var meta = {};
17
+ [].slice.call(elem.attributes).forEach(function(attr) {
18
+ meta[attr.name] = attr.value;
19
+ });
20
+ metas.push(meta);
21
+ });
22
+ return metas;
23
+ });
24
+ });
25
+
26
+ casper.run(function() {
27
+ require("utils").dump(metas);
28
+ this.exit();
29
+ });
@@ -0,0 +1,37 @@
1
+ casper = require("casper").create verbose: true
2
+
3
+ countLinks = ->
4
+ document.querySelectorAll('a').length
5
+
6
+ suites = [
7
+ ->
8
+ @echo "Suite 1"
9
+ @start "http://google.com/", -> @echo "Page title: #{@getTitle()}"
10
+ @then -> @echo "#{@evaluate(countLinks)} links"
11
+ ->
12
+ @echo "Suite 2"
13
+ @start "http://yahoo.com/", -> @echo "Page title: #{@getTitle()}"
14
+ @then -> @echo "#{@evaluate(countLinks)} links"
15
+ ->
16
+ @echo "Suite 3"
17
+ @start "http://bing.com/", -> @echo "Page title: #{@getTitle()}"
18
+ @then -> @echo "#{@evaluate(countLinks)} links"
19
+ ]
20
+
21
+ casper.start()
22
+
23
+ casper.then ->
24
+ @echo("Starting")
25
+
26
+ currentSuite = 0;
27
+
28
+ check = ->
29
+ if suites[currentSuite]
30
+ suites[currentSuite].call @
31
+ currentSuite++;
32
+ casper.run check
33
+ else
34
+ @echo "All done."
35
+ @exit()
36
+
37
+ casper.run check
@@ -0,0 +1,56 @@
1
+ var casper = require("casper").create({
2
+ verbose: true
3
+ });
4
+
5
+ var countLinks = function() {
6
+ return document.querySelectorAll('a').length;
7
+ };
8
+
9
+ var suites = [
10
+ function() {
11
+ this.echo("Suite 1");
12
+ this.start("http://google.com/", function() {
13
+ this.echo("Page title: " + (this.getTitle()));
14
+ });
15
+ this.then(function() {
16
+ this.echo((this.evaluate(countLinks)) + " links");
17
+ });
18
+ }, function() {
19
+ this.echo("Suite 2");
20
+ this.start("http://yahoo.com/", function() {
21
+ this.echo("Page title: " + (this.getTitle()));
22
+ });
23
+ this.then(function() {
24
+ this.echo((this.evaluate(countLinks)) + " links");
25
+ });
26
+ }, function() {
27
+ this.echo("Suite 3");
28
+ this.start("http://bing.com/", function() {
29
+ this.echo("Page title: " + (this.getTitle()));
30
+ });
31
+ this.then(function() {
32
+ this.echo((this.evaluate(countLinks)) + " links");
33
+ });
34
+ }
35
+ ];
36
+
37
+ casper.start();
38
+
39
+ casper.then(function() {
40
+ this.echo("Starting");
41
+ });
42
+
43
+ var currentSuite = 0;
44
+
45
+ var check = function() {
46
+ if (suites[currentSuite]) {
47
+ suites[currentSuite].call(this);
48
+ currentSuite++;
49
+ casper.run(check);
50
+ } else {
51
+ this.echo("All done.");
52
+ this.exit();
53
+ }
54
+ };
55
+
56
+ casper.run(check);
@@ -0,0 +1,28 @@
1
+ ###
2
+ This script will capture a screenshot of a twitter account page
3
+ Usage: $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>
4
+ ###
5
+
6
+ casper = require("casper").create
7
+ viewportSize:
8
+ width: 1024
9
+ height: 768
10
+
11
+ twitterAccount = casper.cli.get 0
12
+ filename = casper.cli.get 1
13
+
14
+ if not twitterAccount or not filename or not /\.(png|jpg|pdf)$/i.test filename
15
+ casper
16
+ .echo("Usage: $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>")
17
+ .exit(1)
18
+
19
+ casper.start "https://twitter.com/#!/#{twitterAccount}", ->
20
+ @waitForSelector ".tweet-row", (->
21
+ @captureSelector filename, "html"
22
+ @echo "Saved screenshot of #{@getCurrentUrl()} to #{filename}"
23
+ ), (->
24
+ @die("Timeout reached. Fail whale?")
25
+ @exit()
26
+ ), 12000
27
+
28
+ casper.run()
@@ -0,0 +1,33 @@
1
+ /*
2
+ * This script will capture a screenshot of a twitter account page
3
+ * Usage: $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>
4
+ */
5
+
6
+ var casper = require("casper").create({
7
+ viewportSize: {
8
+ width: 1024,
9
+ height: 768
10
+ }
11
+ });
12
+
13
+ var twitterAccount = casper.cli.get(0);
14
+ var filename = casper.cli.get(1);
15
+
16
+ if (!twitterAccount || !filename || !/\.(png|jpg|pdf)$/i.test(filename)) {
17
+ casper
18
+ .echo("Usage: $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>")
19
+ .exit(1)
20
+ ;
21
+ }
22
+
23
+ casper.start("https://twitter.com/#!/" + twitterAccount, function() {
24
+ this.waitForSelector(".tweet-row", (function() {
25
+ this.captureSelector(filename, "html");
26
+ this.echo("Saved screenshot of " + (this.getCurrentUrl()) + " to " + filename);
27
+ }), (function() {
28
+ this.die("Timeout reached. Fail whale?");
29
+ this.exit();
30
+ }), 12000);
31
+ });
32
+
33
+ casper.run();
@@ -0,0 +1,15 @@
1
+ ###
2
+ This script will add a custom HTTP status code handler, here for 404 pages.
3
+ ###
4
+
5
+ casper = require("casper").create
6
+ httpStatusHandlers:
7
+ 404: (self, resource) ->
8
+ @echo "Resource at #{resource.url} not found (404)", "COMMENT"
9
+ verbose: true
10
+
11
+ casper.start "http://www.google.com/plop", ->
12
+ @echo "Done."
13
+ @exit()
14
+
15
+ casper.run()
@@ -0,0 +1,19 @@
1
+ /*
2
+ * This script will add a custom HTTP status code handler, here for 404 pages.
3
+ */
4
+
5
+ var casper = require("casper").create({
6
+ httpStatusHandlers: {
7
+ 404: function(self, resource) {
8
+ this.echo("Resource at " + resource.url + " not found (404)", "COMMENT");
9
+ }
10
+ },
11
+ verbose: true
12
+ });
13
+
14
+ casper.start("http://www.google.com/plop", function() {
15
+ this.echo("Done.");
16
+ this.exit();
17
+ });
18
+
19
+ casper.run();
@@ -0,0 +1,37 @@
1
+ failed = []
2
+ start = null
3
+ links = [
4
+ "http://google.com/"
5
+ "http://akei.com/"
6
+ "http://lemonde.fr/"
7
+ "http://liberation.fr/"
8
+ "http://cdiscount.fr/"
9
+ ]
10
+
11
+ casper = require("casper").create
12
+ onStepTimeout: ->
13
+ failed.push @requestUrl
14
+ @test.fail "#{@requestUrl} loads in less than #{timeout}ms."
15
+
16
+ casper.on "load.finished", ->
17
+ @echo "#{@requestUrl} loaded in #{new Date() - start}ms", "PARAMETER"
18
+
19
+ timeout = ~~casper.cli.get(0)
20
+ timeout = 1000 if timeout < 1
21
+ casper.options.stepTimeout = timeout
22
+
23
+ casper.echo "Testing with timeout=#{timeout}ms, please be patient."
24
+
25
+ casper.start()
26
+
27
+ casper.each links, (self, link) ->
28
+ @then ->
29
+ @test.comment "Loading #{link}"
30
+ start = new Date()
31
+ @open link
32
+ @then ->
33
+ if @requestUrl not in failed
34
+ @test.pass "#{@requestUrl} loaded in less than #{timeout}ms."
35
+
36
+ casper.run ->
37
+ @test.renderResults true
@@ -0,0 +1,45 @@
1
+ var failed = [];
2
+ var start = null;
3
+ var links = [
4
+ "http://google.com/'",
5
+ "http://akei.com/'",
6
+ "http://lemonde.fr/'",
7
+ "http://liberation.fr/'",
8
+ "http://cdiscount.fr/"
9
+ ];
10
+
11
+ var casper = require("casper").create({
12
+ onStepTimeout: function() {
13
+ failed.push(this.requestUrl);
14
+ this.test.fail(this.requestUrl + " loads in less than " + timeout + "ms.");
15
+ }
16
+ });
17
+
18
+ casper.on("load.finished", function() {
19
+ this.echo(this.requestUrl + " loaded in " + (new Date() - start) + "ms", "PARAMETER");
20
+ });
21
+
22
+ var timeout = ~~casper.cli.get(0);
23
+ casper.options.stepTimeout = timeout > 0 ? timeout : 1000;
24
+
25
+ casper.echo("Testing with timeout=" + casper.options.stepTimeout + "ms, please be patient.");
26
+
27
+ casper.start();
28
+
29
+ casper.each(links, function(casper, link) {
30
+ this.then(function() {
31
+ this.test.comment("Loading " + link);
32
+ start = new Date();
33
+ this.open(link);
34
+ });
35
+ this.then(function() {
36
+ var message = this.requestUrl + " loads in less than " + timeout + "ms.";
37
+ if (failed.indexOf(this.requestUrl) === -1) {
38
+ this.test.pass(message);
39
+ }
40
+ });
41
+ });
42
+
43
+ casper.run(function() {
44
+ this.test.renderResults(true);
45
+ });