Skip to content

Commit

Permalink
fixed googlepagination code sample
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed Apr 6, 2013
1 parent 81f8744 commit 71c7e2c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
33 changes: 19 additions & 14 deletions samples/googlepagination.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,40 @@ 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
casper
.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()
37 changes: 21 additions & 16 deletions samples/googlepagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand All @@ -49,6 +54,6 @@ casper.start("http://google.fr/", function() {
}, true);
});

casper.then(processPage);
casper.waitForSelector('#pnnext', processPage, terminate);

casper.run();

0 comments on commit 71c7e2c

Please sign in to comment.