Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add screenshot utilities #11

Open
themouette opened this issue Oct 17, 2013 · 0 comments
Open

add screenshot utilities #11

themouette opened this issue Oct 17, 2013 · 0 comments

Comments

@themouette
Copy link
Owner

For easy saving

module.exports.register = function (Driver, config) {
    var path = require('path');
    var fs = require('fs');

    function readKey(obj,i) {return obj && i in obj ? obj[i] : null; }
    var destDir = 'screenstory.storage'.split('.').reduce(readKey, config);
    destDir || (destDir = path.resolve(process.cwd(), "screenshots"));
    createFullPath(destDir, function (err) {
                if (err) console.log(err);
                else console.log('"%s" created to store screenshots', destDir);
            });

    Driver.prototype.thenScreenstory = function (id) {
        return this.thenTakeScreenshot(function (image, next) {
            saveScreenshot(id, image, this._desired, next);
        });
    };

    function saveScreenshot(id, b64screen, desired, next) {
        var file = filename(id, desired);
        fs.writeFile(file, b64screen, 'base64', function (err) {
            next(err);
        });
    }

    function filename(id, desired) {
        return path.resolve(destDir, id+'_'+desired.browserName+'.png');
    }

    // thx to https://gist.github.com/aelaguiz/864454
    function createFullPath(fullPath, callback) {
        var parts = path.normalize(fullPath).split("/"),
        working = '/',
        pathList = [];

        for(var i = 0, max = parts.length; i < max; i++) {
            working = path.join(working, parts[i]);

            pathList.push(working);
        }

        var recursePathList = function recursePathList(paths) {
            if(0 === paths.length) {
                callback(null);
                return ;
            }

            var working = paths.shift();

            try {
                fs.exists(working, function(exists) {
                    if(!exists) {
                        try {
                            fs.mkdir(working, 0755, function() {
                                recursePathList(paths);
                            });
                        }
                        catch(e) {
                            callback(new Error("Failed to create path: " + working + " with " + e.toString()));
                        }
                    }
                    else {
                        recursePathList(paths);
                    }
                });
            }
            catch(e) {
                callback(new Error("Invalid path specified: " + working));
            }
        };

        if(0 === pathList.length)
            callback(new Error("Path list was empty"));
        else
            recursePathList(pathList);
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant