Open themouette opened 11 years ago
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); } };
For easy saving