Skip to content

Commit

Permalink
Fix for issue nightwatchjs#65: Screenshots not saved into missing fol…
Browse files Browse the repository at this point in the history
…ders

Create directory structure if needed when saving screetshots (using mkdirp module).

Includes unit test.
  • Loading branch information
sethmcl committed Jun 20, 2014
1 parent c89a983 commit 38f1729
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
25 changes: 21 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var util = require('util');
var fs = require('fs');
var path = require('path');
var events = require('events');
var mkdirp = require('mkdirp');
var HttpRequest = require('./http/request.js');
var CommandQueue = require('./core/queue.js');
var Assertion = require('./core/assertion.js');
Expand Down Expand Up @@ -326,11 +327,27 @@ Nightwatch.prototype.enqueueCommand = function(commandName, args, callback) {
return this.queue.run();
};

Nightwatch.prototype.saveScreenshotToFile = function(fileName, content) {
fs.writeFile(fileName, content, 'base64', function(err) {
Nightwatch.prototype.saveScreenshotToFile = function(fileName, content, cb) {
cb = cb || function() {};

var dir = path.resolve(fileName, '..');
var fail = function (err) {
console.log(Logger.colors.yellow('Couldn\'t save screenshot to '), fileName);
Logger.warn(err);
cb(err);
};

mkdirp(dir, function (err) {
if (err) {
console.log(Logger.colors.yellow('Couldn\'t save screenshot to '), fileName);
Logger.warn(err);
fail(err);
} else {
fs.writeFile(fileName, content, 'base64', function(err) {
if (err) {
fail(err);
} else {
cb(null, fileName);
}
});
}
});
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"ejs": ">=0.8.3",
"optimist": ">=0.3.5",
"minimatch": "~0.2.14",
"mkpath": ">=0.1.0"
"mkpath": ">=0.1.0",
"mkdirp": "~0.5.0"
},
"devDependencies": {
"nodeunit": "latest",
Expand Down
20 changes: 20 additions & 0 deletions tests/src/index/testNightwatchIndex.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var os = require('os');
var path = require('path');
var fs = require('fs');
var Client = require('../../nightwatch.js');

module.exports = {
Expand Down Expand Up @@ -83,6 +86,23 @@ module.exports = {
});
},

'Test saveScreenshotToFile' : function(test) {
var client = this.client = Client.init();
var tmp = os.tmpdir();
var filePath = path.resolve(tmp, 'r3lekb', 'foo.png');
var data = 'nightwatch';

client.saveScreenshotToFile(filePath, data, function(err, actualFilePath) {
test.equal(err, null);
test.equal(actualFilePath, filePath);

fs.readFile(actualFilePath, function(err) {
test.equal(err, null);
test.done();
});
});
},

tearDown : function(callback) {
this.client.queue.reset();
this.client = null;
Expand Down

0 comments on commit 38f1729

Please sign in to comment.