javascript Cheatsheet:CasperJS,PhantomJS&Spooky。更多:http://journal.gentlenode.com/casperjs-2-chea

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Cheatsheet:CasperJS,PhantomJS&Spooky。更多:http://journal.gentlenode.com/casperjs-2-chea相关的知识,希望对你有一定的参考价值。

// PhantomJS Cheatsheet

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

phantom.exit();

var page = require('webpage').create();

page.open('http://example.com', function() {});
page.evaluate(function() { return document.title; });
page.render('example.png');

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("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {} );


// CasperJS Module Cheatsheet

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

var casper = require('casper').create({
  clientScripts: [
    'includes/jquery.js',
    'includes/underscore.js'
  ],
  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.args
casper.cli.options
casper.cli.has(); casper.cli.get(); casper.cli.drop();

casper.back(), 
casper.exit([int status]);
caper.forward();
casper.run(fn 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);

casper.click(String 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.debugPage();
casper.die(String message[, int status]);

casper.download(String 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.getCurrentUrl();
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.getPageContent();
casper.getTitle();

casper.mouseEvent(String type, String selector);  // mouseup, mousedown, click, mousemove, mouseover, mouseout

casper.open(String 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.toString();
casper.userAgent(String agent);
casper.viewport(Number width, Number height[, Function then]);
casper.visible(String selector);

casper.unwait();
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.click, 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.betterTypeOf(input);
utils.dump(value);
utils.fileExt(file);
utils.fillBlanks(text, pad);
utils.format(f);
utils.getPropertyPath(Object obj, String path);
utils.inherits(ctor, superCtor);

utils.isArray(value);
utils.isCasperObject(value);
utils.isClipRect(value);
utils.isFalsy(subject);
utils.isFunction(value);
utils.isJsFile(file);
utils.isNull(value);
utils.isNumber(value);
utils.isObject(value);
utils.isString(value);
utils.isTruthy(subject);
utils.isType(what, type);
utils.isUndefined(value);
utils.isWebPage(what);
utils.mergeObjects(origin, add);
utils.node(name, attributes);
utils.serialize(value);
utils.unique(array);


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

var spooky = new Spooky({
  child: {
    'transport': "http",
    'proxy': '192.128.101.42:9001',  // 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


以上是关于javascript Cheatsheet:CasperJS,PhantomJS&Spooky。更多:http://journal.gentlenode.com/casperjs-2-chea的主要内容,如果未能解决你的问题,请参考以下文章

javascript 一个正则表达式cheatsheet

javascript React Redux cheatsheet

javascript NodeJS Cheatsheet

markdown Vanilla JavaScript快速参考/ Cheatsheet

markdown Vanilla JavaScript快速参考/ Cheatsheet

markdown Vanilla JavaScript快速参考/ Cheatsheet