Commit 71c7e2c7 71c7e2c7d65ebcf02f9c9dbfd0775e7a42dfa157 by Nicolas Perriault

fixed googlepagination code sample

1 parent 81f8744b
......@@ -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 currentPage >= 5 || !@exists "#pnnext"
return terminate.call casper
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."
), 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()
......
......@@ -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);
}, processPage, terminate);
});
} else {
this.echo("that's all, folks.");
}
};
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();
......