Created
December 4, 2013 10:58
-
-
Save motin/7785773 to your computer and use it in GitHub Desktop.
Example casperjs script. Run from cli:
casperjs --params='{"url":"http://foobar.com/"}}' theprogram.js
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
{ | |
"author": "", | |
"name": "", | |
"version": "0.0.0", | |
"dependencies": { | |
"xtend": "> 0.0.0", | |
"utils": "> 0.0.0" | |
}, | |
"devDependencies": {} | |
} |
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 extend = require("xtend"); | |
function exitWithJsonResponse(response) { | |
console.log(JSON.stringify(response)); | |
casper.exit(); | |
} | |
var verbose = false; | |
var casper = require('casper').create({ | |
verbose: verbose, | |
logLevel: verbose ? "debug" : null, | |
onAlert: function(_casper, msg) { | |
var response = { | |
status: "error", | |
type: "alert", | |
msg: msg, | |
}; | |
jsonResponse(response); | |
}, | |
}); | |
if (verbose) { | |
var utils = require('utils'); | |
casper.echo("Casper CLI passed args:"); | |
require("utils").dump(casper.cli.args); | |
casper.echo("Casper CLI passed options:"); | |
require("utils").dump(casper.cli.options); | |
} | |
// Parameters can be sent as JSON in params cli option, or directly as cli options | |
var options = {}; | |
if (casper.cli.options.params !== undefined) { | |
options = extend(options, JSON.parse(casper.cli.options.params)); | |
casper.cli.options.params = null; | |
} | |
options = extend(options, casper.cli.options); | |
if (verbose) { | |
casper.echo("Options:"); | |
require("utils").dump(options); | |
} | |
// If necessary, set an ordinary user agent | |
casper.userAgent('User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31'); | |
// Set url from params | |
if (!params.url) { | |
var response = { | |
status: "error", | |
type: "param-missing", | |
msg: "Parameter 'url' needs to be supplied", | |
}; | |
exitWithJsonResponse(response); | |
} | |
var url = params.url; | |
casper.start(url, function() { | |
if (verbose) { | |
casper.download(url, 'url.html'); | |
//utils.dump(this); | |
casper.capture("screen.png"); | |
} | |
}); | |
function fillForm() { | |
casper.fill('form[name="username"]', params.username, false); | |
casper.fill('form[name="password"]', params.password, false); | |
if (verbose) { | |
casper.capture("form-filled-screen.png"); | |
} | |
} | |
function submitForm() { | |
casper.click('button[type="submit"]'); | |
} | |
// Non-working example for logging in | |
casper.then(function() { | |
fillForm(); | |
submitForm(); | |
}); | |
casper.then(function() { | |
casper.echo("End of script"); | |
casper.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment