Skip to content
Maciej Brencz edited this page Mar 2, 2014 · 9 revisions

phantomas can be used as CommonJS module from within your nodejs code:

Install

npm install phantomas --save

This will install phantomas from npm repository and add a dependency to your project package.json file.

Example

#!/usr/bin/env node
var phantomas = require('phantomas');

console.log(phantomas); // { [Function: phantomas] path: '...', version: '0.10.1' }

phantomas('http://example.com', {"analyze-css": true}, function(err, res) {
        console.log([
                'phantomas results',
                err, // null or exit code from phantomas process
                res // JSON formatted results
        ]);
});

Now, run the code:

node example.js

Run in debug mode:

DEBUG=phantomas* node example.js

API

Take a look at the example script

var phantomas = require('phantomas'),
  task;

console.log(phantomas.version); // 0.12.1
console.log(phantomas.metadata.metrics); // metrics metadata - issue #224

task = phantomas(url, options, function(err, res) {
  // err: exit code
  // res: parsed JSON or raw data (TAP / CSV / XML)
});

console.log(task.pid); // process ID

// Streams handling
task.stdout.pipe(process.stdout);
task.stderr.pipe(process.stderr);

// Events handling
task.on('progress', function(progress) {
  // reports page loading progress
});

task.on('milestone', function(milestone) {
  // reports page loading milestone - first byte received, onDOMReady, window.onload
});

task.on('log', function(msg) {
  // emitted on every log message sent by phantomas
});

task.on('results', function(results) {
  // results object with metrics values, offenders, asserts data
});

task.on('error', function(exitCode) {
  // reports phantomas exit code (if not zero)
});
Clone this wiki locally