wip revised code (based on coffee revision)
Showing
23 changed files
with
207 additions
and
161 deletions
... | @@ -18,4 +18,4 @@ dump(casper.cli.args); | ... | @@ -18,4 +18,4 @@ dump(casper.cli.args); |
18 | casper.echo("Casper CLI passed options:"); | 18 | casper.echo("Casper CLI passed options:"); |
19 | dump(casper.cli.options); | 19 | dump(casper.cli.options); |
20 | 20 | ||
21 | casper.exit(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
21 | casper.exit(); | ... | ... |
... | @@ -8,4 +8,4 @@ casper.start 'http://google.com/', -> | ... | @@ -8,4 +8,4 @@ casper.start 'http://google.com/', -> |
8 | # emitting a custom event | 8 | # emitting a custom event |
9 | @emit 'google.loaded', @getTitle() | 9 | @emit 'google.loaded', @getTitle() |
10 | 10 | ||
11 | casper.run() | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
11 | casper.run() | ... | ... |
... | @@ -2,12 +2,12 @@ var casper = require('casper').create(); | ... | @@ -2,12 +2,12 @@ var casper = require('casper').create(); |
2 | 2 | ||
3 | // listening to a custom event | 3 | // listening to a custom event |
4 | casper.on('google.loaded', function(title) { | 4 | casper.on('google.loaded', function(title) { |
5 | casper.echo('Google page title is ' + title); | 5 | casper.echo("Google page title is " + title); |
6 | }); | 6 | }); |
7 | 7 | ||
8 | casper.start('http://google.com/', function(self) { | 8 | casper.start('http://google.com/', function() { |
9 | // emitting a custom event | 9 | // emitting a custom event |
10 | self.emit('google.loaded', self.getTitle()); | 10 | this.emit('google.loaded', this.getTitle()); |
11 | }); | 11 | }); |
12 | 12 | ||
13 | casper.run(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
13 | casper.run(); | ... | ... |
1 | /** | 1 | /* |
2 | * A basic custom logging implementation. The idea is to (extremely) verbosely | 2 | A basic custom logging implementation. The idea is to (extremely) verbosely |
3 | * log every received resource. | 3 | log every received resource. |
4 | * | 4 | */ |
5 | */ | 5 | |
6 | var casper = require('casper').create({ | 6 | var casper = require('casper').create({ |
7 | /** | 7 | /** |
8 | * Every time a resource is received, a new log entry is added to the stack | 8 | * Every time a resource is received, a new log entry is added to the stack | ... | ... |
1 | ### download the google logo image as base64 ### | 1 | ### |
2 | Download the google logo image as base64 | ||
3 | ### | ||
2 | 4 | ||
3 | casper = require('casper').create verbose: true | 5 | casper = require('casper').create verbose: true |
4 | 6 | ||
5 | casper.start 'http://www.google.fr/', -> | 7 | casper.start 'http://www.google.fr/', -> |
6 | @echo @base64encode 'http://www.google.fr/images/srpr/logo3w.png' | 8 | @echo @base64encode 'http://www.google.fr/images/srpr/logo3w.png' |
7 | 9 | ||
8 | casper.run() | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
10 | casper.run() | ... | ... |
1 | // download the google logo image as base64 | 1 | /* |
2 | Download the google logo image as base64 | ||
3 | */ | ||
2 | 4 | ||
3 | var casper = require('casper').create({ | 5 | var casper = require('casper').create({ |
4 | verbose: true | 6 | verbose: true |
5 | }); | 7 | }); |
6 | 8 | ||
7 | casper.start('http://www.google.fr/', function(self) { | 9 | casper.start('http://www.google.fr/', function() { |
8 | self.echo(self.base64encode('http://www.google.fr/images/srpr/logo3w.png')); | 10 | this.echo(this.base64encode('http://www.google.fr/images/srpr/logo3w.png')); |
9 | }); | 11 | }); |
10 | 12 | ||
11 | casper.run(); | 13 | casper.run(); | ... | ... |
... | @@ -6,9 +6,11 @@ var links = [ | ... | @@ -6,9 +6,11 @@ var links = [ |
6 | 'http://bing.com/' | 6 | 'http://bing.com/' |
7 | ]; | 7 | ]; |
8 | 8 | ||
9 | casper.start().each(links, function(self, link) { | 9 | casper.start(); |
10 | self.thenOpen(link, function(self) { | 10 | |
11 | self.echo(self.getTitle() + ' - ' + link); | 11 | casper.each(links, function(self, link) { |
12 | this.thenOpen(link, function() { | ||
13 | this.echo("" + (this.getTitle()) + " - " + link); | ||
12 | }); | 14 | }); |
13 | }); | 15 | }); |
14 | 16 | ... | ... |
1 | getLinks = -> | 1 | getLinks = -> |
2 | links = document.querySelectorAll "h3.r a" | 2 | links = document.querySelectorAll "h3.r a" |
3 | Array::map.call links, (e) -> e.getAttribute "href" | 3 | Array::map.call links, (e) -> e.getAttribute "href" |
4 | 4 | ||
5 | links = [] | 5 | links = [] |
6 | casper = require('casper').create() | 6 | casper = require('casper').create() |
7 | 7 | ||
8 | casper.start "http://google.fr/", -> | 8 | casper.start "http://google.fr/", -> |
9 | # search for 'casperjs' from google form | 9 | # search for 'casperjs' from google form |
10 | @fill 'form[action="/search"]', q: "casperjs", true | 10 | @fill 'form[action="/search"]', q: "casperjs", true |
11 | 11 | ||
12 | casper.then -> | 12 | casper.then -> |
13 | # aggregate results for the 'casperjs' search | 13 | # aggregate results for the 'casperjs' search |
14 | links = @evaluate getLinks | 14 | links = @evaluate getLinks |
15 | # search for 'phantomjs' from google form | 15 | # search for 'phantomjs' from google form |
16 | @fill 'form[action="/search"]', q: "phantomjs", true | 16 | @fill 'form[action="/search"]', q: "phantomjs", true |
17 | 17 | ||
18 | casper.then -> | 18 | casper.then -> |
19 | # concat results for the 'phantomjs' search | 19 | # concat results for the 'phantomjs' search |
20 | links = links.concat @evaluate(getLinks) | 20 | links = links.concat @evaluate(getLinks) |
21 | 21 | ||
22 | casper.run -> | 22 | casper.run -> |
23 | # display results | ||
24 | @echo "#{links.length} links found:" | ||
25 | @echo " - " + links.join "\n - " | ||
26 | @exit() | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
23 | # display results | ||
24 | @echo "#{links.length} links found:" | ||
25 | @echo " - " + links.join "\n - " | ||
26 | @exit() | ... | ... |
1 | var links = []; | 1 | var getLinks = function() { |
2 | var casper = require('casper').create(); | 2 | var links; |
3 | 3 | links = document.querySelectorAll("h3.r a"); | |
4 | function getLinks() { | ||
5 | var links = document.querySelectorAll('h3.r a'); | ||
6 | return Array.prototype.map.call(links, function(e) { | 4 | return Array.prototype.map.call(links, function(e) { |
7 | return e.getAttribute('href'); | 5 | return e.getAttribute("href"); |
8 | }); | 6 | }); |
9 | } | 7 | }; |
8 | |||
9 | var links = []; | ||
10 | var casper = require('casper').create(); | ||
10 | 11 | ||
11 | casper.start('http://google.fr/', function(self) { | 12 | casper.start("http://google.fr/", function() { |
12 | // search for 'casperjs' from google form | 13 | // search for 'casperjs' from google form |
13 | self.fill('form[action="/search"]', { q: 'casperjs' }, true); | 14 | this.fill('form[action="/search"]', { |
15 | q: "casperjs" | ||
16 | }, true); | ||
14 | }); | 17 | }); |
15 | 18 | ||
16 | casper.then(function(self) { | 19 | casper.then(function() { |
17 | // aggregate results for the 'casperjs' search | 20 | // aggregate results for the 'casperjs' search |
18 | links = self.evaluate(getLinks); | 21 | links = this.evaluate(getLinks); |
19 | // now search for 'phantomjs' by fillin the form again | 22 | // search for 'phantomjs' from google form |
20 | self.fill('form[action="/search"]', { q: 'phantomjs' }, true); | 23 | this.fill('form[action="/search"]', { |
24 | q: "phantomjs" | ||
25 | }, true); | ||
21 | }); | 26 | }); |
22 | 27 | ||
23 | casper.then(function(self) { | 28 | casper.then(function() { |
24 | // aggregate results for the 'phantomjs' search | 29 | // concat results for the 'phantomjs' search |
25 | links = links.concat(self.evaluate(getLinks)); | 30 | links = links.concat(this.evaluate(getLinks)); |
26 | }); | 31 | }); |
27 | 32 | ||
28 | casper.run(function(self) { | 33 | casper.run(function() { |
29 | // echo results in some pretty fashion | 34 | // display results |
30 | self.echo(links.length + ' links found:'); | 35 | this.echo("" + links.length + " links found:"); |
31 | self.echo(' - ' + links.join('\n - ')).exit(); | 36 | this.echo(" - " + links.join("\n - ")); |
37 | this.exit(); | ||
32 | }); | 38 | }); | ... | ... |
... | @@ -3,11 +3,11 @@ Takes provided terms passed as arguments and query google for the number of | ... | @@ -3,11 +3,11 @@ Takes provided terms passed as arguments and query google for the number of |
3 | estimated results each have. | 3 | estimated results each have. |
4 | 4 | ||
5 | Usage: | 5 | Usage: |
6 | $ casperjs samples/googlematch.js nicolas chuck borris | 6 | $ casperjs googlematch.js nicolas chuck borris |
7 | nicolas: 69600000 | 7 | nicolas: 69600000 |
8 | chuck: 49500000 | 8 | chuck: 49500000 |
9 | borris: 2370000 | 9 | borris: 2370000 |
10 | winner is "nicolas" with 69600000 results | 10 | winner is "nicolas" with 69600000 results |
11 | ### | 11 | ### |
12 | 12 | ||
13 | casper = require('casper').create verbose: true | 13 | casper = require('casper').create verbose: true |
... | @@ -21,7 +21,7 @@ terms = casper.cli.args # terms are passed through command-line arguments | ... | @@ -21,7 +21,7 @@ terms = casper.cli.args # terms are passed through command-line arguments |
21 | 21 | ||
22 | if terms.length < 3 | 22 | if terms.length < 3 |
23 | casper | 23 | casper |
24 | .echo 'Usage: casperjs googlematch.js term1, term2 [, term3]...' | 24 | .echo("Usage: $ casperjs googlematch.js term1, term2 [, term3]...") |
25 | .exit(1) | 25 | .exit(1) |
26 | 26 | ||
27 | scores = [] | 27 | scores = [] | ... | ... |
1 | /** | 1 | /* |
2 | * Takes provided terms passed as arguments and query google for the number of | 2 | Takes provided terms passed as arguments and query google for the number of |
3 | * estimated results each have. | 3 | estimated results each have. |
4 | * | 4 | |
5 | * Usage: | 5 | Usage: |
6 | * $ casperjs samples/googlematch.js nicolas chuck borris | 6 | $ casperjs googlematch.js nicolas chuck borris |
7 | * nicolas: 69600000 | 7 | nicolas: 69600000 |
8 | * chuck: 49500000 | 8 | chuck: 49500000 |
9 | * borris: 2370000 | 9 | borris: 2370000 |
10 | * winner is "nicolas" with 69600000 results | 10 | winner is "nicolas" with 69600000 results |
11 | */ | 11 | */ |
12 | var casper = new require('casper').create({ | 12 | |
13 | var casper = require('casper').create({ | ||
13 | verbose: true | 14 | verbose: true |
14 | }), terms = casper.cli.args, scores = [], i = 0; | 15 | }); |
15 | 16 | ||
16 | casper.fetchScore = function() { | 17 | casper.fetchScore = function() { |
17 | return this.evaluate(function() { | 18 | return this.evaluate(function() { |
... | @@ -20,14 +21,18 @@ casper.fetchScore = function() { | ... | @@ -20,14 +21,18 @@ casper.fetchScore = function() { |
20 | }); | 21 | }); |
21 | }; | 22 | }; |
22 | 23 | ||
23 | if (terms.length < 2) { | 24 | var terms = casper.cli.args; |
25 | |||
26 | if (terms.length < 3) { | ||
24 | casper | 27 | casper |
25 | .echo('Usage: casperjs googlematch.js term1, term2 [, term3]...') | 28 | .echo("Usage: $ casperjs googlematch.js term1, term2 [, term3]...") |
26 | .exit(1) | 29 | .exit(1) |
27 | ; | 30 | ; |
28 | } | 31 | } |
29 | 32 | ||
30 | casper.echo('Let the match begin!'); | 33 | var scores = []; |
34 | |||
35 | casper.echo("Let the match begin between \"" + (terms.join('", "')) + "\"!"); | ||
31 | 36 | ||
32 | casper.start("http://google.fr/"); | 37 | casper.start("http://google.fr/"); |
33 | 38 | ||
... | @@ -46,7 +51,7 @@ casper.each(terms, function(self, term, i) { | ... | @@ -46,7 +51,7 @@ casper.each(terms, function(self, term, i) { |
46 | 51 | ||
47 | casper.run(function(self) { | 52 | casper.run(function(self) { |
48 | scores.sort(function(a, b) { | 53 | scores.sort(function(a, b) { |
49 | return b.score - a.score; | 54 | return b.score - a.score; |
50 | }); | 55 | }); |
51 | var winner = scores[0]; | 56 | var winner = scores[0]; |
52 | self.echo('winner is "' + winner.term + '" with ' + winner.score + ' results'); | 57 | self.echo('winner is "' + winner.term + '" with ' + winner.score + ' results'); | ... | ... |
1 | # Capture multiple pages of google search results | 1 | ### |
2 | # | 2 | Capture multiple pages of google search results |
3 | # usage: casperjs googlepagination.coffee my search terms | 3 | |
4 | # | 4 | usage: casperjs googlepagination.coffee my search terms |
5 | # (all arguments will be used as the query) | 5 | |
6 | (all arguments will be used as the query) | ||
7 | ### | ||
6 | 8 | ||
7 | casper = require('casper').create() | 9 | casper = require('casper').create() |
8 | currentPage = 1 | 10 | currentPage = 1 |
9 | 11 | ||
10 | if casper.cli.args.length == 0 | 12 | if casper.cli.args.length is 0 |
11 | casper | 13 | casper |
12 | .echo "usage: $ casperjs my search terms" | 14 | .echo("Usage: $ casperjs my search terms") |
13 | .exit(1) | 15 | .exit(1) |
14 | 16 | ||
15 | processPage = -> | 17 | processPage = -> | ... | ... |
1 | var casper, currentPage, processPage; | 1 | /* |
2 | Capture multiple pages of google search results | ||
2 | 3 | ||
3 | casper = require('casper').create(); | 4 | Usage: casperjs googlepagination.coffee my search terms |
4 | currentPage = 1; | 5 | |
6 | (all arguments will be used as the query) | ||
7 | */ | ||
8 | |||
9 | var casper = require('casper').create(); | ||
10 | |||
11 | var currentPage = 1; | ||
5 | 12 | ||
6 | if (casper.cli.args.length === 0) { | 13 | if (casper.cli.args.length === 0) { |
7 | casper | 14 | casper |
8 | .echo("usage: $ casperjs my search terms") | 15 | .echo("Usage: $ casperjs my search terms") |
9 | .exit(1) | 16 | .exit(1) |
10 | ; | 17 | ; |
11 | } | 18 | } |
12 | 19 | ||
13 | processPage = function() { | 20 | var processPage = function() { |
14 | var url; | 21 | var url; |
15 | this.echo("capturing page " + currentPage); | 22 | this.echo("capturing page " + currentPage); |
16 | this.capture("google-results-p" + currentPage + ".png"); | 23 | this.capture("google-results-p" + currentPage + ".png"); |
17 | if (currentPage >= 5) { | 24 | if (currentPage >= 5) { |
18 | return; | 25 | return; |
19 | } | 26 | } |
20 | if (this.exists("#pnnext")) { | 27 | if (this.exists("#pnnext")) { |
21 | currentPage++; | 28 | currentPage++; |
22 | this.echo("requesting next page: " + currentPage); | 29 | this.echo("requesting next page: " + currentPage); |
23 | url = this.getCurrentUrl(); | 30 | url = this.getCurrentUrl(); |
24 | return this.thenClick("#pnnext").then(function() { | 31 | return this.thenClick("#pnnext").then(function() { |
25 | return this.waitFor(function() { | 32 | return this.waitFor(function() { |
26 | return url !== this.getCurrentUrl(); | 33 | return url !== this.getCurrentUrl(); |
27 | }, processPage); | 34 | }, processPage); |
28 | }); | 35 | }); |
29 | } else { | 36 | } else { |
30 | return this.echo("that's all, folks."); | 37 | return this.echo("that's all, folks."); |
31 | } | 38 | } |
32 | }; | 39 | }; |
33 | 40 | ||
34 | casper.start('http://google.fr/', function() { | 41 | casper.start('http://google.fr/', function() { |
35 | return this.fill('form[action="/search"]', { | 42 | return this.fill('form[action="/search"]', { |
36 | q: casper.cli.args.join(' ') | 43 | q: casper.cli.args.join(' ') |
37 | }, true); | 44 | }, true); |
38 | }); | 45 | }); |
39 | 46 | ||
40 | casper.then(processPage); | 47 | casper.then(processPage); | ... | ... |
1 | casper = require('casper').create verbose: true, logLevel: 'debug' | 1 | casper = require('casper').create |
2 | verbose: true | ||
3 | logLevel: 'debug' | ||
2 | 4 | ||
3 | casper.log 'this is a debug message', 'debug' | 5 | casper.log 'this is a debug message', 'debug' |
4 | casper.log 'and an informative one', 'info' | 6 | casper.log 'and an informative one', 'info' | ... | ... |
... | @@ -7,4 +7,5 @@ casper.log('this is a debug message', 'debug'); | ... | @@ -7,4 +7,5 @@ casper.log('this is a debug message', 'debug'); |
7 | casper.log('and an informative one', 'info'); | 7 | casper.log('and an informative one', 'info'); |
8 | casper.log('and a warning', 'warning'); | 8 | casper.log('and a warning', 'warning'); |
9 | casper.log('and an error', 'error'); | 9 | casper.log('and an error', 'error'); |
10 | |||
10 | casper.exit(); | 11 | casper.exit(); | ... | ... |
1 | ### This script will capture a screenshot of a twitter account page | ||
2 | |||
3 | Usage $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]> | ||
4 | ### | 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 | |||
5 | casper = require('casper').create | 6 | casper = require('casper').create |
6 | viewportSize: | 7 | viewportSize: |
7 | width: 1024 | 8 | width: 1024 |
... | @@ -12,7 +13,7 @@ filename = casper.cli.get 1 | ... | @@ -12,7 +13,7 @@ filename = casper.cli.get 1 |
12 | 13 | ||
13 | if not twitterAccount or not filename or not /\.(png|jpg|pdf)$/i.test filename | 14 | if not twitterAccount or not filename or not /\.(png|jpg|pdf)$/i.test filename |
14 | casper | 15 | casper |
15 | .echo "Usage $ casperjs samples/screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>" | 16 | .echo("Usage: $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>") |
16 | .exit(1) | 17 | .exit(1) |
17 | 18 | ||
18 | casper.start "https://twitter.com/#!/#{twitterAccount}", -> | 19 | casper.start "https://twitter.com/#!/#{twitterAccount}", -> | ... | ... |
1 | /** | 1 | /* |
2 | * This script will capture a screenshot of a twitter account page | 2 | This script will capture a screenshot of a twitter account page |
3 | * | 3 | Usage: $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]> |
4 | * Usage $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]> | 4 | */ |
5 | */ | 5 | |
6 | var casper = require('casper').create({ | 6 | var casper = require('casper').create({ |
7 | viewportSize: { | 7 | viewportSize: { |
8 | width: 1024, | 8 | width: 1024, |
... | @@ -15,7 +15,7 @@ var filename = casper.cli.get(1); | ... | @@ -15,7 +15,7 @@ var filename = casper.cli.get(1); |
15 | 15 | ||
16 | if (!twitterAccount || !filename || !/\.(png|jpg|pdf)$/i.test(filename)) { | 16 | if (!twitterAccount || !filename || !/\.(png|jpg|pdf)$/i.test(filename)) { |
17 | casper | 17 | casper |
18 | .echo("Usage $ casperjs samples/screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>") | 18 | .echo("Usage: $ casperjs screenshot.coffee <twitter-account> <filename.[jpg|png|pdf]>") |
19 | .exit(1) | 19 | .exit(1) |
20 | ; | 20 | ; |
21 | } | 21 | } | ... | ... |
1 | ### | 1 | ### |
2 | This script will add a custom HTTP status code handler, here for 404 pages. | 2 | This script will add a custom HTTP status code handler, here for 404 pages. |
3 | ### | 3 | ### |
4 | |||
4 | casper = require('casper').create | 5 | casper = require('casper').create |
5 | httpStatusHandlers: | 6 | httpStatusHandlers: |
6 | 404: (self, resource) -> | 7 | 404: (self, resource) -> |
7 | @echo "Resource at #{resource.url} not found (404)", "COMMENT" | 8 | @echo "Resource at #{resource.url} not found (404)", "COMMENT" |
9 | verbose: true | ||
8 | 10 | ||
9 | casper.start 'http://www.google.com/plop', -> | 11 | casper.start 'http://www.google.com/plop', -> |
10 | @echo 'Done.' | 12 | @echo "Done." |
11 | @exit() | 13 | @exit() |
12 | 14 | ||
13 | casper.run() | 15 | casper.run() | ... | ... |
1 | /** | 1 | /* |
2 | * This script will add a custom HTTP status code handler, here for 404 pages. | 2 | This script will add a custom HTTP status code handler, here for 404 pages. |
3 | * | 3 | */ |
4 | */ | 4 | |
5 | var casper = require('casper').create({ | 5 | var casper = require('casper').create({ |
6 | httpStatusHandlers: { | 6 | httpStatusHandlers: { |
7 | 404: function(self, resource) { | 7 | 404: function(self, resource) { |
8 | self.echo('Resource at ' + resource.url + ' not found (404)', 'COMMENT'); | 8 | this.echo("Resource at " + resource.url + " not found (404)", "COMMENT"); |
9 | } | 9 | } |
10 | }, | 10 | }, |
11 | verbose: true | 11 | verbose: true |
12 | }); | 12 | }); |
13 | 13 | ||
14 | casper.start('http://www.google.com/plop', function(self) { | 14 | casper.start('http://www.google.com/plop', function() { |
15 | self.echo('Done.').exit(); | 15 | this.echo("Done."); |
16 | this.exit(); | ||
16 | }); | 17 | }); |
17 | 18 | ||
18 | casper.run(); | 19 | casper.run(); | ... | ... |
... | @@ -27,4 +27,5 @@ casper.each links, (self, link) -> | ... | @@ -27,4 +27,5 @@ casper.each links, (self, link) -> |
27 | else | 27 | else |
28 | @test.pass "#{@requestUrl} loaded in less than #{timeout}ms." | 28 | @test.pass "#{@requestUrl} loaded in less than #{timeout}ms." |
29 | 29 | ||
30 | casper.run -> @test.renderResults true | 30 | casper.run -> |
31 | @test.renderResults true | ... | ... |
1 | var failed = []; | 1 | var failed = []; |
2 | 2 | ||
3 | var casper = require('casper').create({ | 3 | var casper = require('casper').create({ |
4 | onStepTimeout: function(self) { | 4 | onStepTimeout: function() { |
5 | failed.push(self.requestUrl); | 5 | failed.push(this.requestUrl); |
6 | } | 6 | } |
7 | }); | 7 | }); |
8 | 8 | ||
... | @@ -15,9 +15,12 @@ var links = [ | ... | @@ -15,9 +15,12 @@ var links = [ |
15 | ]; | 15 | ]; |
16 | 16 | ||
17 | var timeout = ~~casper.cli.get(0); | 17 | var timeout = ~~casper.cli.get(0); |
18 | casper.options.stepTimeout = timeout > 0 ? timeout : 1000; | 18 | if (timeout < 1) { |
19 | timeout = 1000; | ||
20 | } | ||
21 | casper.options.stepTimeout = timeout; | ||
19 | 22 | ||
20 | casper.echo('Testing with timeout=' + casper.options.stepTimeout + 'ms.'); | 23 | casper.echo("Testing with timeout=" + casper.options.stepTimeout + "ms."); |
21 | 24 | ||
22 | casper.start(); | 25 | casper.start(); |
23 | 26 | ||
... | @@ -33,6 +36,6 @@ casper.each(links, function(self, link) { | ... | @@ -33,6 +36,6 @@ casper.each(links, function(self, link) { |
33 | }); | 36 | }); |
34 | }); | 37 | }); |
35 | 38 | ||
36 | casper.run(function(self) { | 39 | casper.run(function() { |
37 | self.test.renderResults(true); | 40 | this.test.renderResults(true); |
38 | }); | 41 | }); | ... | ... |
... | @@ -14,6 +14,7 @@ $ casperjs samples/timeout.js 2000 | ... | @@ -14,6 +14,7 @@ $ casperjs samples/timeout.js 2000 |
14 | Will google.com load in less than 2000ms? | 14 | Will google.com load in less than 2000ms? |
15 | YES! | 15 | YES! |
16 | ### | 16 | ### |
17 | |||
17 | casper = require('casper').create | 18 | casper = require('casper').create |
18 | onTimeout: -> | 19 | onTimeout: -> |
19 | @echo 'NOPE.', 'RED_BAR' | 20 | @echo 'NOPE.', 'RED_BAR' |
... | @@ -22,7 +23,7 @@ casper = require('casper').create | ... | @@ -22,7 +23,7 @@ casper = require('casper').create |
22 | timeout = ~~casper.cli.get 0 | 23 | timeout = ~~casper.cli.get 0 |
23 | if timeout < 1 | 24 | if timeout < 1 |
24 | casper | 25 | casper |
25 | .echo "You must pass a valid timeout value" | 26 | .echo("You must pass a valid timeout value") |
26 | .exit(1) | 27 | .exit(1) |
27 | 28 | ||
28 | casper.echo "Will google.com load in less than #{timeout}ms?" | 29 | casper.echo "Will google.com load in less than #{timeout}ms?" | ... | ... |
1 | /** | 1 | /* |
2 | * Just a silly game. | 2 | Just a silly game. |
3 | * | 3 | |
4 | * $ casperjs samples/timeout.js 500 | 4 | $ casperjs samples/timeout.js 500 |
5 | * Will google.com load in less than 500ms? | 5 | Will google.com load in less than 500ms? |
6 | * NOPE. | 6 | NOPE. |
7 | * $ casperjs samples/timeout.js 1000 | 7 | $ casperjs samples/timeout.js 1000 |
8 | * Will google.com load in less than 1000ms? | 8 | Will google.com load in less than 1000ms? |
9 | * NOPE. | 9 | NOPE. |
10 | * $ casperjs samples/timeout.js 1500 | 10 | $ casperjs samples/timeout.js 1500 |
11 | * Will google.com load in less than 1500ms? | 11 | Will google.com load in less than 1500ms? |
12 | * NOPE. | 12 | NOPE. |
13 | * $ casperjs samples/timeout.js 2000 | 13 | $ casperjs samples/timeout.js 2000 |
14 | * Will google.com load in less than 2000ms? | 14 | Will google.com load in less than 2000ms? |
15 | * YES! | 15 | YES! |
16 | */ | 16 | */ |
17 | |||
17 | var casper = require('casper').create({ | 18 | var casper = require('casper').create({ |
18 | onTimeout: function(self) { | 19 | onTimeout: function() { |
19 | self.echo('NOPE.', 'RED_BAR').exit(); | 20 | this |
21 | .echo('NOPE.', 'RED_BAR') | ||
22 | .exit() | ||
23 | ; | ||
20 | } | 24 | } |
21 | }); | 25 | }); |
22 | 26 | ||
23 | var timeout = ~~casper.cli.get(0); | 27 | var timeout = ~~casper.cli.get(0); |
28 | |||
24 | if (timeout < 1) { | 29 | if (timeout < 1) { |
25 | casper | 30 | casper |
26 | .echo('You must pass a valid timeout value') | 31 | .echo("You must pass a valid timeout value") |
27 | .exit(1) | 32 | .exit(1) |
28 | ; | 33 | ; |
29 | } | 34 | } |
30 | casper.echo('Will google.com load in less than ' + timeout + 'ms?'); | 35 | casper.echo("Will google.com load in less than " + timeout + "ms?"); |
31 | casper.options.timeout = timeout; | 36 | casper.options.timeout = timeout; |
32 | 37 | ||
33 | casper.start('http://www.google.com/', function(self) { | 38 | casper.start('http://www.google.com/', function() { |
34 | self.echo('YES!', 'GREEN_BAR').exit(); | 39 | this |
40 | .echo('YES!', 'GREEN_BAR') | ||
41 | .exit() | ||
42 | ; | ||
35 | }); | 43 | }); |
36 | 44 | ||
37 | casper.run(); | 45 | casper.run(); | ... | ... |
-
Please register or sign in to post a comment