Giter VIP home page Giter VIP logo

truffler's Introduction

Truffler

Access web pages programmatically with PhantomJS, for running tests or scraping information.

NPM version Node.js version support Build status Dependencies MIT licensed

var truffler = require('truffler');

var test = truffler(function(browser, page, done) {
    // the test function to run in PhantomJS
    done();
});

test.run('http://www.nature.com/', function(error, results) {
    console.log(results);
});

Table Of Contents

Install

Install Truffler with npm:

npm install truffler

Usage

Require in Truffler:

var truffler = require('truffler');

Create a test runner by initialising Truffler with a test function. This test function has access to a PhantomJS browser and page instance, as well as a copy of all the passed in options. The test function must accept a third argument which is a callback:

var test = truffler(function(browser, page, options, done) {
    // ... perform testing here ...
    done(error, results);
});

Within this function, you have access to all of the behaviour in PhantomJS.

You can also instantiate Truffler with some default options if you wish. This allows you to change the way PhantomJS and your page is loaded:

var test = truffler({
    // ... options go here ...
}, function(browser, page, options, done) {
    // ... perform testing here ...
});

The test.run function can then be used to run your test function against a URL:

test.run('http://www.nature.com/', function(error, results) {
    // ...
});

The error and results parameters contain errors and results from the PhantomJS run against your page. The results can be any object you like. Here's an example test function which returns the page title if it has one, or errors if not.

var test = truffler(function(browser, page, options, done) {
    page.evaluate(
        function() {
            return document.title;
        },
        function(error, title) {
            if (!title) {
                return done(new Error('The page has no title!'));
            }
            done(null, title);
        }
    );
});

test.run('http://www.nature.com/', function(error, title) {
    // ... do something with the error and title ...
});

Options

Truffler has lots of options you can use to change the way PhantomJS runs, or the way your page is loaded. Options can be set either on the Truffler instance when it's created or the individual test runs. This allows you to set some defaults which can be overridden per-test:

// Set the default Foo header to "bar"
var test = truffler({
    page: {
        headers: {
            Foo: 'bar'
        }
    }
}, function() { /* ... */ });

// Run a test with the Foo header set to "bar"
test.run('http://www.nature.com/', function(error, title) { /* ... */});

// Run a test with the Foo header overridden
test.run('http://www.nature.com/', {
    page: {
        headers: {
            Foo: 'hello'
        }
    }
}, function(error, title) { /* ... */});

Below is a reference of all the options that are available:

log (object)

An object which implments the methods debug, error, and info which will be used to report errors and test information.

truffler({
    log: {
        debug: console.log.bind(console),
        error: console.error.bind(console),
        info: console.info.bind(console)
    }
});

Each of these defaults to an empty function.

page.headers (object)

A key-value map of request headers to send when testing a web page.

truffler({
    page: {
        headers: {
            Cookie: 'foo=bar'
        }
    }
});

Defaults to an empty object.

page.settings (object)

A key-value map of settings to add to the PhantomJS page. For a full list of available settings, see the PhantomJS page settings documentation.

truffler({
    page: {
        settings: {
            loadImages: false,
            userName: 'nature',
            password: 'say the magic word'
        }
    }
});

Defaults to:

{
    resourceTimeout: 30000,
    userAgent: 'truffler/<version>'
}

page.viewport (object)

The viewport width and height in pixels. The viewport object must have both width and height properties.

truffler({
    page: {
        viewport: {
            width: 320,
            height: 480
        }
    }
});

Defaults to:

{
    width: 1024,
    height: 768
}

phantom (object)

A key-value map of settings to initialise PhantomJS with. This is passed directly into the phantom module – documentation can be found here. You can pass PhantomJS command-line parameters in the phantom.parameters option as key-value pairs.

truffler({
    phantom: {
        path: 'PATH_TO_PHANTOMJS_EXE',
        port: 1234,
        parameters: {
            'ignore-ssl-errors': 'true'
        }
    }
});

Defaults to an empty object. If phantom.port is not specified, a random available port will be used.

timeout (number)

The maximum time (in milliseconds) that Truffler should run for. This timeout can sometimes be exceeded, if a long-running task has started within PhantomJS itself. This is rare, but you shouldn't rely on exact timing.

If the timeout is exceeded, the test function will callback with an error and no results.

truffler({
    timeout: 1000
});

Defaults to 30000 (30 seconds).

Examples

Basic Example

Run Truffler on a URL and output the page title:

node example/basic

Multiple Example

Use async to run Truffler on multiple URLs in series, and output the page titles:

node example/multiple

Contributing

To contribute to Truffler, clone this repo locally and commit your code on a separate branch.

Please write unit tests for your code, and check that everything works by running the following before opening a pull-request:

make ci

Support and Migration

Truffler major versions are normally supported for 6 months after their last minor release. This means that patch-level changes will be added and bugs will be fixed. The table below outlines the end-of-support dates for major versions, and the last minor release for that version.

We also maintain a migration guide to help you migrate.

Major Version Last Minor Release Node.js Versions Support End Date
❤️ 3 N/A 4+ N/A
2 2.3 0.12+ 2017-04-23
💀 1 1.0 0.10–4 2016-10-16

If you're opening issues related to these, please mention the version that the issue relates to.

License

Truffler is licensed under the MIT license.
Copyright © 2015, Springer Nature

truffler's People

Contributors

carbonrobot avatar josebolos avatar nickcall avatar robloach avatar rowanmanning avatar whymarrh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

truffler's Issues

New API Ideas

I'm toying with an API overhaul for version 2. I've encountered limitations with the existing one (while working on pa11y) which have made development a pain. Also it's very easy with the current API to spawn zombie processes, forget to exit, and generally do bad stuff like that.

As well as this, the original theory was that you could spawn one PhantomJS process and run multiple tests against it. This proved to be a bit useless if each individual page can't specify its own settings.

Here's what I've come up with as an idea for the new API:

var truffler = require('truffler');

// Create a test function with no default options
var test = truffler(function (browser, page, done) {
    // regular test function, like old times
});

// Create a test function with default options (calls to `test.run` can override these)
var test = truffler({
    // ...
}, function (browser, page, done) {
    // regular test function, like old times
});

// Test a URL
test.run('http://www.nature.com/', function (error, results) {
    // results = the results passed into `done` from the test function
});

// Test a URL with some additional options
test.run('http://www.nature.com/', {
    // ...
}, function (error, results) {
    // results = the results passed into `done` from the test function
});

// Test a URL with some additional options
test.run({
    url: 'http://www.nature.com/'
    // ...
}, function (error, results) {
    // results = the results passed into `done` from the test function
});

// Options
var options = {
    log: {},
    page: {
        headers: {},
        settings: {
            userAgent: 'truffler/<version>'
        },
        viewport: {
            width: 1024,
            height: 768
        }
    },
    phantom: {
        // port will be randomised unless specified
    }
};

The way option inheritance works is like this:

var test = truffler({
    page: {
        viewport: {
            width: 640,
            height: 480
        }
    }
});

// No options so viewport will be 640x480
test.run('http://www.nature.com/');

// Options merge so viewport will be 800x480
test.run('http://www.nature.com/', {
    page: {
        viewport: {
            width: 800
        }
    }
});

Enable POST requests

I have discovered with @rowanmanning that to enable POST requests this change is required (i.e. include options.page.settings as a second argument to state.page.open()):

From:
state.page.open(url, function (error, status) {

To:
state.page.open(url, options.page.settings, function (error, status) {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.