1/6/2017 - 11:06 AM

Cheatsheet for CasperJS

Cheatsheet: CasperJS, PhantomJS & Spooky. More:

// PhantomJS Cheatsheet

$ brew update && brew install phantomjs  // install PhantomJS with brew


var page = require('webpage').create();'', function() {});
page.evaluate(function() { return document.title; });

page.onConsoleMessage = function(msg) { console.log(msg); };
\!h page.onResourceRequested = function(request) { console.log('Request ' + JSON.stringify(request, undefined, 4)); };
page.onResourceReceived = function(response) {  console.log('Receive ' + JSON.stringify(response, undefined, 4)); };
page.onError = function(msg, trace) { console.log(msg); ... }

page.includeJs("", function() {} );

// CasperJS Module Cheatsheet

$ brew install casperjs --devel  // install CasperJS with brew

var casper = require('casper').create({
  clientScripts: [
  exitOnError: true,
  httpStatusHandlers: { },
  logLevel: "error",
  onAlert: null,
  onDie: null,
  onError: null,
  onLoadError: null,
  onPageInitialized: null,
  onResourceReceived: null,
  onResourceRequested: null,
  onStepComplete: null,
  onStepTimeout: null,
  onTimeout: null,
  onWaitTimeout: null,
  page: null,
  pageSettings: {
    javascriptEnabled: true,
    loadImages: false,
    loadPlugins: false,
    localToRemoteUrlAccessEnabled: false,
    userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5)",
    userName: null,
    password: null,
    XSSAuditingEnabled: false,
  remoteScripts: [ ],
  safeLogs: [ ],
  stepTimeout: null,
  timeout: null,
  verbose: true,
  viewportSize: null,
  retryTimeout: 100,
  waitTimeout: 5000,

casper.options.waitTimeout = 1000;  // alter an option at runtime

casper.cli.has(); casper.cli.get(); casper.cli.drop();

casper.exit([int status]);
caper.forward(); onComplete[, int time]);
casper.start(String url[, Function then]);
casper.then(Function then);
casper.thenBypass(Number nb);
casper.thenBypassIf(Mixed condition, Number nb);
casper.thenBypassUnless(Mixed condition, Number nb);
casper.thenClick(String selector[, Function then]);
casper.thenEvaluate(Function fn[, arg1[, arg2[, ...]]]);
casper.thenOpen(String location[, mixed options]);
casper.thenOpenAndEvaluate(String location[, Function then[, arg1[, arg2[, ...]]]);

casper.base64encode(String url[, String method, Object data]);
casper.bypass(Numbr nb); selector);
casper.clickLabel(String label[, String tag]);

casper.capture(String targetFilepath, Object clipRect);
casper.captureBase64(String format[, Mixed area]);
casper.captureSelector(String targetFile, String selector);

casper.clear();  // clears the current page execution environment context
casper.debugHTML([String selector, Boolean outer]);
casper.die(String message[, int status]); url, String target[, String method, Object data]);

casper.each(Array array, Function fn);
casper.eachThen(String message[, String style]);

casper.echo(String message[, String style]);
casper.evaluate(Function fn[, arg1[, arg2[, …]]]);
casper.evaluateOrDie(Function fn[, String message]);
casper.exists(String selector);
casper.fetchText(String selector);

casper.log(String message[, String level, String space]);

casper.fill(String selector, Object values[, Boolean submit]);  // fields are referenced by name attribute
casper.fillSelectors(String selector, Object values[, Boolean submit]);  // fields are referenced by css3 selectors
casper.fillXPath(String selector, Object values[, Boolean submit]);  // fields are referenced by XPath selectors

casper.getElement(s)Attribute(String selector, String attribute);
casper.getElement(s)Bounds(String selector);
casper.getElement(s)Info(String selector);
casper.getFormValues(String selector);
casper.getGlobal(String name);
casper.getHTML([String selector, Boolean outer]);

casper.mouseEvent(String type, String selector);  // mouseup, mousedown, click, mousemove, mouseover, mouseout location, Object Settings);  // settings: method, data, headers
casper.reload([Function then];
casper.repeat(int times, Function then);
casper.resourceExists(Mixed test);
casper.sendKeys(Selector selector, String keys[, Object options]);  // options: keepFocus, modifiers
casper.setHttpAuth(String username, String password);
casper.status(Boolean asString);

casper.userAgent(String agent);
casper.viewport(Number width, Number height[, Function then]);
casper.visible(String selector);

casper.wait(Number timeout[, Function then]);
casper.waitFor(Function testFx[, Function then, Function onTimeout, Number timeout]);
casper.waitForPopup(String|RegExp urlPattern[, Function then, Function onTimeout, Number timeout]);
casper.waitForResource(Function testFx[, Function then, Function onTimeout, Number timeout]);
casper.waitForUrl(String|RegExp url[, Function then, Function onTimeout, Number timeout]);
casper.waitForSelector(String selector[, Function then, Function onTimeout, Number timeout]);
casper.waitWhileSelector(String selector[, Function then, Function onTimeout, Number timeout]);
casper.waitForSelectorTextChange(String selectors[, Function then, Function onTimeout, Number timeout]);
casper.waitForText(String text[, Function then, Function onTimeout, Number timeout]);
casper.waitUntilVisible(String selector[, Function then, Function onTimeout, Number timeout]);
casper.waitWhileVisible(String selector[, Function then, Function onTimeout, Number timeout]);
casper.warn(String message);
casper.withFrame(String|Number frameInfo, Function then);
casper.withPopup(Mixed popupInfo, Function then);
casper.zoom(Number factor);

casper.on(String event, Function then);
casper.emit(String event);

// Events referenced: back, capture.saved, click, complete.error, die, downloaded.file, error, exit, fill, forward,
// http.auth, http.status.[code], load.started, load.failed, load.finished, log,, mouse.down, mouse.move,
// mouse.up, navigation.requested, open, page.created, page.error, page.initialized, page.resource.received, page.resource.requested,
// popup.created, popup.loaded, popup.close, popup.created, remote.alert, remote.callback, remote.message, resource.received, resource.requested,
// run.complete, run.start, starting, started, step.added, step.complete, step.created, step.error, step.start, step.timeout, timeout, url.changed,
// viewport.changed, wait.done, wait.start, waitFor.timeout

casper.setFilter(String event, Function then);

// Filters referenced: capture.target_filename, echo.message, log.message, open.location, page.confirm, page.prompt

// CasperJS Utils Module Cheatsheet

var utils = require('utils');

utils.fillBlanks(text, pad);
utils.getPropertyPath(Object obj, String path);
utils.inherits(ctor, superCtor);

utils.isType(what, type);
utils.mergeObjects(origin, add);
utils.node(name, attributes);

// Spooky Cheatsheet (Spooky is used to control CasperJS from the Node environment)

var spooky = new Spooky({
  child: {
    'transport': "http",
    'proxy': '',  // when you want to switch a proxy (kind of thing I like with Spooky)
    'proxy-auth': 'username:password'
  casper: {
    logLevel: "debug",  // can also be set to "info", "warning" or "error"
    verbose: false
    pageSettings: {
      userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36'
}, function() {
  // The magic begins here.

// Passing values between spooky environment (NodeJS) to CasperJS.
spooky.then([{ x: x }, function () { console.log('x:', x); }]); // function tuples
spooky.thenEvaluate(function (x) { console.log('x:', x); }, { x: x }); // argument hashes