Created
January 4, 2016 03:45
-
-
Save maesa/06b3c46e7bde275f147b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var casper = require('casper').create({ | |
verbose: true, | |
logLevel: 'info', | |
pageSettings: { | |
loadImages: false, | |
loadPlugins: false, | |
userAgent: 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36' | |
} | |
}); | |
var yourEmail = 'your@email.com'; | |
var yourPassword = 'yourpassword'; | |
casper.start('https://www.packtpub.com/packt/offers/free-learning', function(){ | |
if (this.visible("div#account-bar-logged-in")) { | |
claimYourFreeeBook(); | |
} else { | |
this.click("a.login-popup"); | |
this.fillSelectors("form#packt-user-login-form", { | |
'input[name="email"]' : yourEmail, | |
'input[name="password"]' : yourPassword | |
}, true); | |
this.waitForSelector("div#account-bar-logged-in", claimYourFreeeBook, stopScript); | |
} | |
}); | |
casper.on("remote.message", function(msg) { | |
this.echo("Console: " + msg); | |
}); | |
casper.on('step.error', function(err) { | |
this.die("Step has failed: " + err); | |
}) | |
casper.on("page.error", function(msg, trace) { | |
this.echo("Error: " + msg, "ERROR"); | |
}); | |
casper.on('complete.error', function(err) { | |
this.die("Complete callback has failed: " + err); | |
}); | |
casper.run(); | |
function claimYourFreeeBook() { | |
casper.waitForSelector("div.free-ebook a", function() { | |
this.click("div.free-ebook a"); | |
}, stopScript); | |
} | |
function stopScript() { | |
console.log("Exiting..."); | |
casper.exit(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment