Last active
April 8, 2017 04:24
-
-
Save mblarsen/73a1c44436340331fa8031e76fc8bcc0 to your computer and use it in GitHub Desktop.
Creating a Facebook App programmatically using CasperJS
This file contains hidden or 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
// | |
// run: casperjs --engine=slimerjs create-facebook-app.js | |
// | |
const casper = require('casper').create({ | |
verbose: true, | |
loglevel: 'debug', | |
}) | |
/* captures every step */ | |
let counter = 1; | |
function capture(suffix = null) { | |
suffix = suffix === null ? '' : '-' + suffix | |
casper.capture('step-' + counter + suffix + '.png') | |
counter++ | |
} | |
/* wraps casper functions in promises */ | |
function promisefy(name) { | |
const _org = casper[name] | |
casper[name] = function () { | |
const args = arguments | |
return new Promise(function (resolve, reject) { | |
_org.bind(casper)(...args) | |
casper.then(function () { | |
capture(_org.name) | |
resolve() | |
}) | |
}) | |
} | |
} | |
/* wrap these */ | |
[ | |
'clickLabel', | |
'fillSelectors', | |
'click', | |
'wait', | |
'waitForResource', | |
'waitForSelector', | |
].forEach(promisefy) | |
/* run promises sequentially */ | |
Promise.serial = funcs => { | |
return funcs.reduce((resolved, func) => { | |
return resolved.then(func) | |
}, Promise.resolve([])) | |
} | |
casper.start('https://developers.facebook.com/') | |
casper.viewport(1680, 1024) | |
casper.then(function () { | |
Promise.serial([ | |
casper.clickLabel.bind(null, 'Log In', 'a'), | |
casper.fillSelectors.bind(null, 'form#login_form', { | |
'input[name=email]': 'your email', | |
'input[name=pass]': 'your password', | |
}, false), | |
casper.click.bind(null, 'button#loginbutton'), | |
casper.click.bind(null, 'a[href="/apps/"]'), | |
casper.clickLabel.bind(null, 'Add a New App', 'em'), | |
casper.waitForSelector.bind(null, 'form[action="/apps/async/create/"]'), | |
casper.fillSelectors.bind(null, 'form[action="/apps/async/create/"]', { | |
'input[name=basic_name]': 'app name', | |
'input[name=basic_email]': 'your email', | |
}, false), | |
casper.clickLabel.bind(null, 'Choose a Category', 'span'), | |
casper.clickLabel.bind(null, 'Business', 'span'), | |
casper.clickLabel.bind(null, 'Create App ID', 'button'), | |
casper.waitForResource.bind(null, /captcha\/tfbimage\.php/), | |
casper.wait.bind(null, 1500), | |
casper.captureSelector.bind(null, 'captcha.png', '.img[alt^="Hit reload"]'), | |
]) | |
// TODO set up casper server | |
// TODO send captured captcha to user | |
// TODO with answer continue creation of app | |
}) | |
casper.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment