revamped many samples
Showing
12 changed files
with
133 additions
and
112 deletions
... | @@ -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 | this.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(); | ... | ... |
... | @@ -31,6 +31,6 @@ var casper = require('casper').create({ | ... | @@ -31,6 +31,6 @@ var casper = require('casper').create({ |
31 | casper.logLevels = ['verbose'].concat(casper.logLevels); | 31 | casper.logLevels = ['verbose'].concat(casper.logLevels); |
32 | 32 | ||
33 | // test our new logger with google | 33 | // test our new logger with google |
34 | casper.start('http://www.google.com/').run(function(self) { | 34 | casper.start('http://www.google.com/').run(function() { |
35 | self.exit(); | 35 | this.exit(); |
36 | }); | 36 | }); | ... | ... |
... | @@ -4,8 +4,8 @@ var casper = require('casper').create({ | ... | @@ -4,8 +4,8 @@ var casper = require('casper').create({ |
4 | verbose: true | 4 | verbose: true |
5 | }); | 5 | }); |
6 | 6 | ||
7 | casper.start('http://www.google.fr/', function(self) { | 7 | casper.start('http://www.google.fr/', function() { |
8 | self.echo(self.base64encode('http://www.google.fr/images/srpr/logo3w.png')); | 8 | this.echo(self.base64encode('http://www.google.fr/images/srpr/logo3w.png')); |
9 | }); | 9 | }); |
10 | 10 | ||
11 | casper.run(); | 11 | casper.run(); | ... | ... |
... | @@ -4,6 +4,7 @@ if (phantom.casperArgs.args.length !== 1) { | ... | @@ -4,6 +4,7 @@ if (phantom.casperArgs.args.length !== 1) { |
4 | } | 4 | } |
5 | 5 | ||
6 | var casper = require('casper').create({ | 6 | var casper = require('casper').create({ |
7 | logLevel: "debug", | ||
7 | verbose: true | 8 | verbose: true |
8 | }); | 9 | }); |
9 | 10 | ||
... | @@ -30,38 +31,38 @@ var links = [ | ... | @@ -30,38 +31,38 @@ var links = [ |
30 | ]; | 31 | ]; |
31 | 32 | ||
32 | // Just opens the page and prints the title | 33 | // Just opens the page and prints the title |
33 | var start = function(self, link) { | 34 | var start = function(link) { |
34 | self.start(link, function(self) { | 35 | this.start(link, function() { |
35 | self.echo('Page title: ' + self.getTitle()); | 36 | this.echo('Page title: ' + this.getTitle()); |
36 | }); | 37 | }); |
37 | }; | 38 | }; |
38 | 39 | ||
39 | // Get the links, and add them to the links array | 40 | // Get the links, and add them to the links array |
40 | // (It could be done all in one step, but it is intentionally splitted) | 41 | // (It could be done all in one step, but it is intentionally splitted) |
41 | var addLinks = function(link) { | 42 | var addLinks = function(link) { |
42 | this.then(function(self) { | 43 | this.then(function() { |
43 | var found = self.evaluate(searchLinks); | 44 | var found = this.evaluate(searchLinks); |
44 | self.echo(found.length + " links found on " + link); | 45 | this.echo(found.length + " links found on " + link); |
45 | links = links.concat(found); | 46 | links = links.concat(found); |
46 | }); | 47 | }); |
47 | }; | 48 | }; |
48 | 49 | ||
49 | casper.start().then(function(self) { | 50 | casper.start().then(function() { |
50 | self.echo('Starting'); | 51 | this.echo('Starting'); |
51 | }); | 52 | }); |
52 | 53 | ||
53 | var currentLink = 0; | 54 | var currentLink = 0; |
54 | 55 | ||
55 | // As long as it has a next link, and is under the maximum limit, will keep running | 56 | // As long as it has a next link, and is under the maximum limit, will keep running |
56 | function check(self) { | 57 | function check() { |
57 | if (links[currentLink] && currentLink < upTo) { | 58 | if (links[currentLink] && currentLink < upTo) { |
58 | self.echo('--- Link ' + currentLink + ' ---'); | 59 | this.echo('--- Link ' + currentLink + ' ---'); |
59 | start(self, links[currentLink]); | 60 | start.call(this, links[currentLink]); |
60 | addLinks.call(self, links[currentLink]); | 61 | addLinks.call(this, links[currentLink]); |
61 | currentLink++; | 62 | currentLink++; |
62 | self.run(check); | 63 | this.run(check); |
63 | } else { | 64 | } else { |
64 | self.echo('All done.').exit(); | 65 | this.echo('All done.').exit(); |
65 | } | 66 | } |
66 | } | 67 | } |
67 | 68 | ... | ... |
... | @@ -8,25 +8,25 @@ function getLinks() { | ... | @@ -8,25 +8,25 @@ function getLinks() { |
8 | }); | 8 | }); |
9 | } | 9 | } |
10 | 10 | ||
11 | casper.start('http://google.fr/', function(self) { | 11 | casper.start('http://google.fr/', function() { |
12 | // search for 'casperjs' from google form | 12 | // search for 'casperjs' from google form |
13 | self.fill('form[action="/search"]', { q: 'casperjs' }, true); | 13 | this.fill('form[action="/search"]', { q: 'casperjs' }, true); |
14 | }); | 14 | }); |
15 | 15 | ||
16 | casper.then(function(self) { | 16 | casper.then(function() { |
17 | // aggregate results for the 'casperjs' search | 17 | // aggregate results for the 'casperjs' search |
18 | links = self.evaluate(getLinks); | 18 | links = this.evaluate(getLinks); |
19 | // now search for 'phantomjs' by fillin the form again | 19 | // now search for 'phantomjs' by fillin the form again |
20 | self.fill('form[action="/search"]', { q: 'phantomjs' }, true); | 20 | this.fill('form[action="/search"]', { q: 'phantomjs' }, true); |
21 | }); | 21 | }); |
22 | 22 | ||
23 | casper.then(function(self) { | 23 | casper.then(function() { |
24 | // aggregate results for the 'phantomjs' search | 24 | // aggregate results for the 'phantomjs' search |
25 | links = links.concat(self.evaluate(getLinks)); | 25 | links = links.concat(this.evaluate(getLinks)); |
26 | }); | 26 | }); |
27 | 27 | ||
28 | casper.run(function(self) { | 28 | casper.run(function() { |
29 | // echo results in some pretty fashion | 29 | // echo results in some pretty fashion |
30 | self.echo(links.length + ' links found:'); | 30 | this.echo(links.length + ' links found:'); |
31 | self.echo(' - ' + links.join('\n - ')).exit(); | 31 | this.echo(' - ' + links.join('\n - ')).exit(); |
32 | }); | 32 | }); | ... | ... |
... | @@ -35,7 +35,7 @@ casper.each terms, (self, term) -> | ... | @@ -35,7 +35,7 @@ casper.each terms, (self, term) -> |
35 | @then -> | 35 | @then -> |
36 | score = @fetchScore() | 36 | score = @fetchScore() |
37 | scores.push term: term, score: score | 37 | scores.push term: term, score: score |
38 | self.echo "#{term}: #{score}" | 38 | @echo "#{term}: #{score}" |
39 | 39 | ||
40 | casper.run -> | 40 | casper.run -> |
41 | winner = scores[0] | 41 | winner = scores[0] | ... | ... |
... | @@ -16,7 +16,7 @@ var casper = new require('casper').create({ | ... | @@ -16,7 +16,7 @@ var casper = new require('casper').create({ |
16 | casper.fetchScore = function() { | 16 | casper.fetchScore = function() { |
17 | return this.evaluate(function() { | 17 | return this.evaluate(function() { |
18 | var result = document.querySelector('#resultStats').innerText; | 18 | var result = document.querySelector('#resultStats').innerText; |
19 | return parseInt(/Environ ([0-9\s]{1,}).*/.exec(result)[1].replace(/\s/g, '')); | 19 | return parseInt(/Environ ([0-9\s]{1,}).*/.exec(result)[1].replace(/\s/g, ''), 10); |
20 | }); | 20 | }); |
21 | }; | 21 | }; |
22 | 22 | ||
... | @@ -31,24 +31,31 @@ casper.echo('Let the match begin!'); | ... | @@ -31,24 +31,31 @@ casper.echo('Let the match begin!'); |
31 | 31 | ||
32 | casper.start("http://google.fr/"); | 32 | casper.start("http://google.fr/"); |
33 | 33 | ||
34 | casper.each(terms, function(self, term, i) { | 34 | casper.each(terms, function(casper, term, i) { |
35 | self.then(function(self) { | 35 | this.echo('Fecthing score for ' + term); |
36 | self.fill('form[action="/search"]', { q: term }, true); | 36 | this.then(function() { |
37 | }).then(function(self) { | 37 | this.fill('form[action="/search"]', { q: term }, true); |
38 | var score = self.fetchScore(); | 38 | }); |
39 | this.then(function() { | ||
40 | var score = this.fetchScore(); | ||
39 | scores.push({ | 41 | scores.push({ |
40 | term: term, | 42 | term: term, |
41 | score: score | 43 | score: score |
42 | }); | 44 | }); |
43 | self.echo(term + ': ' + score); | 45 | this.echo(term + ': ' + score); |
44 | }); | 46 | }); |
45 | }); | 47 | }); |
46 | 48 | ||
47 | casper.run(function(self) { | 49 | casper.run(function() { |
50 | if (scores.length === 0) { | ||
51 | this.echo('No result found').exit(); | ||
52 | } | ||
48 | var winner = scores[0]; | 53 | var winner = scores[0]; |
49 | for (var i = 0, len = scores.length; i < len; i++) | 54 | scores.forEach(function(score) { |
50 | if (scores[i].score > winner.score) | 55 | if (score.score > winner.score) { |
51 | winner = scores[i]; | 56 | winner = score.score; |
52 | self.echo('winner is "' + winner.term + '" with ' + winner.score + ' results'); | 57 | } |
53 | self.exit(); | 58 | }); |
59 | this.echo('winner is "' + winner.term + '" with ' + winner.score + ' results'); | ||
60 | this.exit(); | ||
54 | }); | 61 | }); | ... | ... |
... | @@ -2,22 +2,22 @@ var casper = require('casper').create({ | ... | @@ -2,22 +2,22 @@ var casper = require('casper').create({ |
2 | logLevel: "debug" | 2 | logLevel: "debug" |
3 | }); | 3 | }); |
4 | 4 | ||
5 | casper.start('http://www.google.fr/', function(self) { | 5 | casper.start('http://www.google.fr/', function() { |
6 | self.test.assertTitle('Google', 'google homepage title is the one expected'); | 6 | this.test.assertTitle('Google', 'google homepage title is the one expected'); |
7 | self.test.assertExists('form[action="/search"]', 'main form is found'); | 7 | this.test.assertExists('form[action="/search"]', 'main form is found'); |
8 | self.fill('form[action="/search"]', { | 8 | this.fill('form[action="/search"]', { |
9 | q: 'foo' | 9 | q: 'foo' |
10 | }, true); | 10 | }, true); |
11 | }); | 11 | }); |
12 | 12 | ||
13 | casper.then(function(self) { | 13 | casper.then(function() { |
14 | self.test.assertTitle('foo - Recherche Google', 'google title is ok'); | 14 | this.test.assertTitle('foo - Recherche Google', 'google title is ok'); |
15 | self.test.assertUrlMatch(/q=foo/, 'search term has been submitted'); | 15 | this.test.assertUrlMatch(/q=foo/, 'search term has been submitted'); |
16 | self.test.assertEval(function() { | 16 | this.test.assertEval(function() { |
17 | return __utils__.findAll('h3.r').length >= 10; | 17 | return __utils__.findAll('h3.r').length >= 10; |
18 | }, 'google search for "foo" retrieves 10 or more results'); | 18 | }, 'google search for "foo" retrieves 10 or more results'); |
19 | }); | 19 | }); |
20 | 20 | ||
21 | casper.run(function(self) { | 21 | casper.run(function() { |
22 | self.test.renderResults(true); | 22 | this.test.renderResults(true); |
23 | }); | 23 | }); | ... | ... |
... | @@ -7,45 +7,45 @@ function countLinks() { | ... | @@ -7,45 +7,45 @@ function countLinks() { |
7 | } | 7 | } |
8 | 8 | ||
9 | var suites = [ | 9 | var suites = [ |
10 | function(self) { | 10 | function() { |
11 | self.echo('Suite 1'); | 11 | this.echo('Suite 1'); |
12 | self.start('http://google.com/', function(self) { | 12 | this.start('http://google.com/', function() { |
13 | self.echo('Page title: ' + self.getTitle()); | 13 | this.echo('Page title: ' + this.getTitle()); |
14 | }).then(function(self) { | 14 | }).then(function() { |
15 | self.echo(self.evaluate(countLinks) + ' links'); | 15 | this.echo(this.evaluate(countLinks) + ' links'); |
16 | }); | 16 | }); |
17 | }, | 17 | }, |
18 | function(self) { | 18 | function() { |
19 | self.echo('Suite 2'); | 19 | this.echo('Suite 2'); |
20 | self.start('http://yahoo.com/', function(self) { | 20 | this.start('http://yahoo.com/', function() { |
21 | self.echo('Page title: ' + self.getTitle()); | 21 | this.echo('Page title: ' + this.getTitle()); |
22 | }).then(function(self) { | 22 | }).then(function() { |
23 | self.echo(self.evaluate(countLinks) + ' links'); | 23 | this.echo(this.evaluate(countLinks) + ' links'); |
24 | }); | 24 | }); |
25 | }, | 25 | }, |
26 | function(self) { | 26 | function() { |
27 | self.echo('Suite 3'); | 27 | this.echo('Suite 3'); |
28 | self.start('http://bing.com/', function(self) { | 28 | this.start('http://bing.com/', function() { |
29 | self.echo('Page title: ' + self.getTitle()); | 29 | this.echo('Page title: ' + this.getTitle()); |
30 | }).then(function(self) { | 30 | }).then(function() { |
31 | self.echo(self.evaluate(countLinks) + ' links'); | 31 | this.echo(this.evaluate(countLinks) + ' links'); |
32 | }); | 32 | }); |
33 | } | 33 | } |
34 | ]; | 34 | ]; |
35 | 35 | ||
36 | casper.start().then(function(self) { | 36 | casper.start().then(function() { |
37 | self.echo('Starting'); | 37 | this.echo('Starting'); |
38 | }); | 38 | }); |
39 | 39 | ||
40 | var currentSuite = 0; | 40 | var currentSuite = 0; |
41 | 41 | ||
42 | function check(self) { | 42 | function check() { |
43 | if (suites[currentSuite]) { | 43 | if (suites[currentSuite]) { |
44 | suites[currentSuite](casper); | 44 | suites[currentSuite].call(this); |
45 | currentSuite++; | 45 | currentSuite++; |
46 | casper.run(check); | 46 | casper.run(check); |
47 | } else { | 47 | } else { |
48 | self.echo('All done.').exit(); | 48 | this.echo('All done.').exit(); |
49 | } | 49 | } |
50 | } | 50 | } |
51 | 51 | ... | ... |
1 | failed = []; | 1 | failed = [] |
2 | 2 | start = null | |
3 | casper = require('casper').create | ||
4 | onStepTimeout: -> failed.push @requestUrl | ||
5 | |||
6 | links = [ | 3 | links = [ |
7 | 'http://google.com/' | 4 | 'http://google.com/' |
8 | 'http://akei.com/' | 5 | 'http://akei.com/' |
... | @@ -11,20 +8,29 @@ links = [ | ... | @@ -11,20 +8,29 @@ links = [ |
11 | 'http://cdiscount.fr/' | 8 | 'http://cdiscount.fr/' |
12 | ] | 9 | ] |
13 | 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 | |||
14 | timeout = ~~casper.cli.get(0) | 19 | timeout = ~~casper.cli.get(0) |
15 | timeout = 1000 if timeout < 1 | 20 | timeout = 1000 if timeout < 1 |
16 | casper.options.stepTimeout = timeout | 21 | casper.options.stepTimeout = timeout |
17 | 22 | ||
18 | casper.echo "Testing with timeout=#{casper.options.stepTimeout}ms." | 23 | casper.echo "Testing with timeout=#{timeout}ms, please be patient." |
19 | 24 | ||
20 | casper.start() | 25 | casper.start() |
21 | 26 | ||
22 | casper.each links, (self, link) -> | 27 | casper.each links, (self, link) -> |
23 | @test.comment "Adding #{link} to test suite" | 28 | @then -> |
24 | @thenOpen link, -> | 29 | @test.comment "Loading #{link}" |
25 | if @requestUrl in failed | 30 | start = new Date() |
26 | @test.fail "#{@requestUrl} loaded in less than #{timeout}ms." | 31 | @open link |
27 | else | 32 | @then -> |
33 | if @requestUrl not in failed | ||
28 | @test.pass "#{@requestUrl} loaded in less than #{timeout}ms." | 34 | @test.pass "#{@requestUrl} loaded in less than #{timeout}ms." |
29 | 35 | ||
30 | casper.run -> @test.renderResults true | 36 | casper.run -> @test.renderResults true | ... | ... |
1 | var failed = []; | 1 | var failed = []; |
2 | 2 | var start = null; | |
3 | var casper = require('casper').create({ | ||
4 | onStepTimeout: function(self) { | ||
5 | failed.push(self.requestUrl); | ||
6 | } | ||
7 | }); | ||
8 | |||
9 | var links = [ | 3 | var links = [ |
10 | 'http://google.com/', | 4 | 'http://google.com/', |
11 | 'http://akei.com/', | 5 | 'http://akei.com/', |
... | @@ -14,25 +8,38 @@ var links = [ | ... | @@ -14,25 +8,38 @@ var links = [ |
14 | 'http://cdiscount.fr/' | 8 | 'http://cdiscount.fr/' |
15 | ]; | 9 | ]; |
16 | 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 | |||
17 | var timeout = ~~casper.cli.get(0); | 22 | var timeout = ~~casper.cli.get(0); |
18 | casper.options.stepTimeout = timeout > 0 ? timeout : 1000; | 23 | casper.options.stepTimeout = timeout > 0 ? timeout : 1000; |
19 | 24 | ||
20 | casper.echo('Testing with timeout=' + casper.options.stepTimeout + 'ms.'); | 25 | casper.echo('Testing with timeout=' + casper.options.stepTimeout + 'ms, please be patient.'); |
21 | 26 | ||
22 | casper.start(); | 27 | casper.start(); |
23 | 28 | ||
24 | casper.each(links, function(self, link) { | 29 | casper.each(links, function(casper, link) { |
25 | self.test.comment('Adding ' + link + ' to test suite'); | 30 | this.then(function() { |
26 | self.thenOpen(link, function(self) { | 31 | this.test.comment('Loading ' + link); |
27 | var testStatus = self.test.pass; | 32 | start = new Date(); |
28 | if (failed.indexOf(self.requestUrl) > -1) { | 33 | this.open(link); |
29 | self.test.fail(self.requestUrl); | 34 | }); |
30 | } else { | 35 | this.then(function() { |
31 | self.test.pass(self.requestUrl); | 36 | var message = this.requestUrl + " loads in less than " + timeout + "ms."; |
37 | if (failed.indexOf(this.requestUrl) === -1) { | ||
38 | this.test.pass(message); | ||
32 | } | 39 | } |
33 | }); | 40 | }); |
34 | }); | 41 | }); |
35 | 42 | ||
36 | casper.run(function(self) { | 43 | casper.run(function() { |
37 | self.test.renderResults(true); | 44 | this.test.renderResults(true); |
38 | }); | 45 | }); | ... | ... |
... | @@ -15,8 +15,8 @@ | ... | @@ -15,8 +15,8 @@ |
15 | * YES! | 15 | * YES! |
16 | */ | 16 | */ |
17 | var casper = require('casper').create({ | 17 | var casper = require('casper').create({ |
18 | onTimeout: function(self) { | 18 | onTimeout: function() { |
19 | self.echo('NOPE.', 'RED_BAR').exit(); | 19 | this.echo('NOPE.', 'RED_BAR').exit(); |
20 | } | 20 | } |
21 | }); | 21 | }); |
22 | 22 | ||
... | @@ -30,8 +30,8 @@ if (timeout < 1) { | ... | @@ -30,8 +30,8 @@ if (timeout < 1) { |
30 | casper.echo('Will google.com load in less than ' + timeout + 'ms?'); | 30 | casper.echo('Will google.com load in less than ' + timeout + 'ms?'); |
31 | casper.options.timeout = timeout; | 31 | casper.options.timeout = timeout; |
32 | 32 | ||
33 | casper.start('http://www.google.com/', function(self) { | 33 | casper.start('http://www.google.com/', function() { |
34 | self.echo('YES!', 'GREEN_BAR').exit(); | 34 | this.echo('YES!', 'GREEN_BAR').exit(); |
35 | }); | 35 | }); |
36 | 36 | ||
37 | casper.run(); | 37 | casper.run(); | ... | ... |
-
Please register or sign in to post a comment