Commit 71c7e2c7 71c7e2c7d65ebcf02f9c9dbfd0775e7a42dfa157 by Nicolas Perriault

fixed googlepagination code sample

1 parent 81f8744b
...@@ -6,7 +6,11 @@ Usage: $ casperjs googlepagination.coffee my search terms ...@@ -6,7 +6,11 @@ Usage: $ casperjs googlepagination.coffee my search terms
6 (all arguments will be used as the query) 6 (all arguments will be used as the query)
7 ### 7 ###
8 8
9 casper = require("casper").create() 9 casper = require("casper").create(
10 waitTimeout: 1000
11 pageSettings:
12 userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20130404 Firefox/23.0"
13 )
10 currentPage = 1 14 currentPage = 1
11 15
12 if casper.cli.args.length is 0 16 if casper.cli.args.length is 0
...@@ -14,27 +18,28 @@ if casper.cli.args.length is 0 ...@@ -14,27 +18,28 @@ if casper.cli.args.length is 0
14 .echo("Usage: $ casperjs googlepagination.coffee my search terms") 18 .echo("Usage: $ casperjs googlepagination.coffee my search terms")
15 .exit(1) 19 .exit(1)
16 20
21 terminate = ->
22 @echo("that's all, folks.").exit();
23
17 processPage = -> 24 processPage = ->
18 @echo "capturing page #{currentPage}" 25 @echo "capturing page #{currentPage}"
19 @capture "google-results-p#{currentPage}.png" 26 @capture "google-results-p#{currentPage}.png"
20 27
21 # don't go too far down the rabbit hole 28 # don't go too far down the rabbit hole
22 return if currentPage >= 5 29 if currentPage >= 5 || !@exists "#pnnext"
23 30 return terminate.call casper
24 if @exists "#pnnext" 31
25 currentPage++ 32 currentPage++
26 @echo "requesting next page: #{currentPage}" 33 @echo "requesting next page: #{currentPage}"
27 url = @getCurrentUrl() 34 url = @getCurrentUrl()
28 @thenClick("#pnnext").then -> 35 @thenClick("#pnnext").then ->
29 @waitFor (-> 36 @waitFor (->
30 url isnt @getCurrentUrl() 37 url isnt @getCurrentUrl()
31 ), processPage 38 ), processPage, terminate
32 else
33 @echo "that's all, folks."
34 39
35 casper.start "http://google.fr/", -> 40 casper.start "http://google.fr/", ->
36 @fill 'form[action="/search"]', q: casper.cli.args.join(" "), true 41 @fill 'form[action="/search"]', q: casper.cli.args.join(" "), true
37 42
38 casper.then processPage 43 casper.waitForSelector "#pnnext", processPage, terminate
39 44
40 casper.run() 45 casper.run()
......
...@@ -9,7 +9,12 @@ ...@@ -9,7 +9,12 @@
9 * (all arguments will be used as the query) 9 * (all arguments will be used as the query)
10 */ 10 */
11 11
12 var casper = require("casper").create(); 12 var casper = require("casper").create({
13 waitTimeout: 1000,
14 pageSettings: {
15 userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20130404 Firefox/23.0"
16 }
17 });
13 var currentPage = 1; 18 var currentPage = 1;
14 19
15 if (casper.cli.args.length === 0) { 20 if (casper.cli.args.length === 0) {
...@@ -19,28 +24,28 @@ if (casper.cli.args.length === 0) { ...@@ -19,28 +24,28 @@ if (casper.cli.args.length === 0) {
19 ; 24 ;
20 } 25 }
21 26
27 var terminate = function() {
28 this.echo("that's all, folks.").exit();
29 };
30
22 var processPage = function() { 31 var processPage = function() {
23 var url; 32 var url;
24 this.echo("capturing page " + currentPage); 33 this.echo("capturing page " + currentPage);
25 this.capture("google-results-p" + currentPage + ".png"); 34 this.capture("google-results-p" + currentPage + ".png");
26 35
27 // don't go too far down the rabbit hole 36 // don't go too far down the rabbit hole
28 if (currentPage >= 5) { 37 if (currentPage >= 5 || !this.exists("#pnnext")) {
29 return; 38 return terminate.call(casper);
30 } 39 }
31 40
32 if (this.exists("#pnnext")) { 41 currentPage++;
33 currentPage++; 42 this.echo("requesting next page: " + currentPage);
34 this.echo("requesting next page: " + currentPage); 43 url = this.getCurrentUrl();
35 url = this.getCurrentUrl(); 44 this.thenClick("#pnnext").then(function() {
36 this.thenClick("#pnnext").then(function() { 45 this.waitFor(function() {
37 this.waitFor(function() { 46 return url !== this.getCurrentUrl();
38 return url !== this.getCurrentUrl(); 47 }, processPage, terminate);
39 }, processPage); 48 });
40 });
41 } else {
42 this.echo("that's all, folks.");
43 }
44 }; 49 };
45 50
46 casper.start("http://google.fr/", function() { 51 casper.start("http://google.fr/", function() {
...@@ -49,6 +54,6 @@ casper.start("http://google.fr/", function() { ...@@ -49,6 +54,6 @@ casper.start("http://google.fr/", function() {
49 }, true); 54 }, true);
50 }); 55 });
51 56
52 casper.then(processPage); 57 casper.waitForSelector('#pnnext', processPage, terminate);
53 58
54 casper.run(); 59 casper.run();
......