npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

truffler

v3.1.0

Published

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

Downloads

1,349

Readme

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.

| :grey_question: | Major Version | Last Minor Release | Node.js Versions | Support End Date | | :-------------- | :------------ | :----------------- | :--------------- | :--------------- | | :heart: | 3 | N/A | 4+ | N/A | | :hourglass: | 2 | 2.3 | 0.12+ | 2017-04-23 | | :skull: | 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