From 71c7e2c7d65ebcf02f9c9dbfd0775e7a42dfa157 Mon Sep 17 00:00:00 2001 From: Nicolas Perriault Date: Sat, 6 Apr 2013 10:34:09 +0200 Subject: [PATCH] fixed googlepagination code sample --- samples/googlepagination.coffee | 33 ++++++++++++++++------------- samples/googlepagination.js | 37 +++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/samples/googlepagination.coffee b/samples/googlepagination.coffee index 3cf7190ad..39b3967f1 100644 --- a/samples/googlepagination.coffee +++ b/samples/googlepagination.coffee @@ -6,7 +6,11 @@ Usage: $ casperjs googlepagination.coffee my search terms (all arguments will be used as the query) ### -casper = require("casper").create() +casper = require("casper").create( + waitTimeout: 1000 + pageSettings: + userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20130404 Firefox/23.0" +) currentPage = 1 if casper.cli.args.length is 0 @@ -14,27 +18,28 @@ if casper.cli.args.length is 0 .echo("Usage: $ casperjs googlepagination.coffee my search terms") .exit(1) +terminate = -> + @echo("that's all, folks.").exit(); + processPage = -> @echo "capturing page #{currentPage}" @capture "google-results-p#{currentPage}.png" # don't go too far down the rabbit hole - return if currentPage >= 5 - - if @exists "#pnnext" - currentPage++ - @echo "requesting next page: #{currentPage}" - url = @getCurrentUrl() - @thenClick("#pnnext").then -> - @waitFor (-> - url isnt @getCurrentUrl() - ), processPage - else - @echo "that's all, folks." + if currentPage >= 5 || !@exists "#pnnext" + return terminate.call casper + + currentPage++ + @echo "requesting next page: #{currentPage}" + url = @getCurrentUrl() + @thenClick("#pnnext").then -> + @waitFor (-> + url isnt @getCurrentUrl() + ), processPage, terminate casper.start "http://google.fr/", -> @fill 'form[action="/search"]', q: casper.cli.args.join(" "), true -casper.then processPage +casper.waitForSelector "#pnnext", processPage, terminate casper.run() diff --git a/samples/googlepagination.js b/samples/googlepagination.js index eab641469..a320d87a4 100644 --- a/samples/googlepagination.js +++ b/samples/googlepagination.js @@ -9,7 +9,12 @@ * (all arguments will be used as the query) */ -var casper = require("casper").create(); +var casper = require("casper").create({ + waitTimeout: 1000, + pageSettings: { + userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:23.0) Gecko/20130404 Firefox/23.0" + } +}); var currentPage = 1; if (casper.cli.args.length === 0) { @@ -19,28 +24,28 @@ if (casper.cli.args.length === 0) { ; } +var terminate = function() { + this.echo("that's all, folks.").exit(); +}; + var processPage = function() { var url; this.echo("capturing page " + currentPage); this.capture("google-results-p" + currentPage + ".png"); // don't go too far down the rabbit hole - if (currentPage >= 5) { - return; + if (currentPage >= 5 || !this.exists("#pnnext")) { + return terminate.call(casper); } - if (this.exists("#pnnext")) { - currentPage++; - this.echo("requesting next page: " + currentPage); - url = this.getCurrentUrl(); - this.thenClick("#pnnext").then(function() { - this.waitFor(function() { - return url !== this.getCurrentUrl(); - }, processPage); - }); - } else { - this.echo("that's all, folks."); - } + currentPage++; + this.echo("requesting next page: " + currentPage); + url = this.getCurrentUrl(); + this.thenClick("#pnnext").then(function() { + this.waitFor(function() { + return url !== this.getCurrentUrl(); + }, processPage, terminate); + }); }; casper.start("http://google.fr/", function() { @@ -49,6 +54,6 @@ casper.start("http://google.fr/", function() { }, true); }); -casper.then(processPage); +casper.waitForSelector('#pnnext', processPage, terminate); casper.run();