siygle / grunt-autoshot

Create a quick screenshot for your site which could help for document or testing
118 stars 14 forks source link

Allow an options.preCreate function to be run on a PhantomJS WebPage instance #8

Closed wamberg closed 8 years ago

wamberg commented 10 years ago

This pull request allows us to access the WebPage before generating the screenshot. This is helpful to me because I want to add cookies and access pages that require authentication. With this pull request I can use something like the following Gruntfile.js:

module.exports = function(grunt) {
    grunt.initConfig({
        autoshot: {
            public: {
                options: {
                    path: '/tmp',
                    remote: {
                        files: [
                            { src: 'http://localhost:8000/', dest: 'login.png' }
                        ]
                    },
                    local: null,
                    preCreate: null,
                    viewport: [
                        '320x480',
                        '600x1024',
                        '1024x768',
                        '1280x800',
                        '1440x900'
                    ]
                }
            },
            authenticated: {
                options: {
                    path: '/tmp',
                    remote: {
                        files: [
                            { src: 'http://localhost:8000/', dest: 'dashboard.png' }
                        ]
                    },
                    local: null,
                    preCreate: function(ph) {
                        ph.addCookie({
                          'name': 'sessionid',
                          'value': 'v48054jkxxjwtb7cmzwa6iaqimjzs0ax',
                          'domain': 'localhost',
                          'path': '/',
                          'httponly': true,
                          'secure': false,
                          'expires':  (new Date()).getTime() + (1000 * 60 * 60)
                        });
                    },
                    viewport: [
                        '320x480',
                        '600x1024',
                        '1024x768',
                        '1280x800',
                        '1440x900'
                    ]
                }
            }
        }
    });
    grunt.loadNpmTasks('grunt-autoshot');
};

What you do in preCreate (how you get the cookie value, for example) is up to you.