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

Option to disable screenshot data in logs #262

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ module.exports = (function() {
result = {};
}

// Clone the result object
var resultLog = {};
for (var key in result) {
resultLog[key] = result[key];
}
// Remove the screenshot base64 data according to settings
if (!Logger.settings.screenshot_data && self.reqOptions.path.indexOf('/screenshot') > -1) {
resultLog.value = '[removed by nightwatch due to settings]';
}

if (errorMessage !== '') {
console.log(Logger.colors.yellow('There was an error while executing the Selenium command') +
(!Logger.isEnabled() ? ' - enabling the --verbose option might offer more details.' : '')
Expand All @@ -123,7 +133,7 @@ module.exports = (function() {
}

var logMethod = response.statusCode.toString().indexOf('5') === 0 ? 'error' : 'info';
Logger[logMethod]('Response ' + response.statusCode + ' ' + self.reqOptions.method + ' ' + self.reqOptions.path, result);
Logger[logMethod]('Response ' + response.statusCode + ' ' + self.reqOptions.method + ' ' + self.reqOptions.path, resultLog);

if (response.statusCode.toString().indexOf('2') === 0 || redirected) {
self.emit('success', result, response, redirected);
Expand Down
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ Nightwatch.prototype.setOptions = function(options) {
} else {
Logger.enable();
}
if (typeof this.options.log_screenshot_data == 'boolean') {
Logger.settings.screenshot_data = this.options.log_screenshot_data;
}

var seleniumPort = this.options.seleniumPort || this.options.selenium_port;
var seleniumHost = this.options.seleniumHost || this.options.selenium_host;
Expand Down
2 changes: 2 additions & 0 deletions lib/util/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var util = require('util');
var Settings = {
log_timestamp : false,
screenshot_data: true,
enabled : true
};

Expand Down Expand Up @@ -188,3 +189,4 @@ exports.isEnabled = function() {
return Settings.enabled;
};
exports.colors = colors;
exports.settings = Settings;
74 changes: 74 additions & 0 deletions tests/src/http/testRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,80 @@ module.exports = {

},

testScreenshotDataInLog : function(test) {
nock('http://localhost:4444')
.post('/wd/hub/123456/screenshot')
.reply(200, {
status: 0,
state: 'success',
value: 'base64-data'
});

var options = {
path : '/:sessionId/screenshot',
method : 'POST',
sessionId : '123456'
};

var oldLoggerInfo = Logger.info;
var loggerInfoCalls = [];
Logger.info = function() {
loggerInfoCalls.push(arguments);
};

Logger.settings.screenshot_data = true;

var request = new HttpRequest(options);
request.on('success', function(result) {
var lastCall = loggerInfoCalls[loggerInfoCalls.length - 1];

test.ok(lastCall[1].value);
test.equal(lastCall[1].value, 'base64-data');
test.ok(result.value);
test.equal(result.value, 'base64-data');

Logger.info = oldLoggerInfo;
test.done();
}).send();
},

testScreenshotDataNotInLog : function(test) {
nock('http://localhost:4444')
.post('/wd/hub/123456/screenshot')
.reply(200, {
status: 0,
state: 'success',
value: 'base64-data'
});

var options = {
path : '/:sessionId/screenshot',
method : 'POST',
sessionId : '123456'
};

var oldLoggerInfo = Logger.info;
var loggerInfoCalls = [];
Logger.info = function() {
loggerInfoCalls.push(arguments);
};

Logger.settings.screenshot_data = false;

var request = new HttpRequest(options);
request.on('success', function(result) {
var lastCall = loggerInfoCalls[loggerInfoCalls.length - 1];

test.ok(lastCall[1].value);
test.equal(lastCall[1].value, '[removed by nightwatch due to settings]');
test.ok(result.value);
test.equal(result.value, 'base64-data');

Logger.info = oldLoggerInfo;
test.done();
}).send();
},

testGetRequest : function(test) {
nock('http://localhost:4444')
.get('/wd/hub/123456/element')
Expand Down
4 changes: 4 additions & 0 deletions tests/src/index/testNightwatchIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ var os = require('os');
var path = require('path');
var fs = require('fs');
var mockery = require('mockery');
var BASE_PATH = process.env.NIGHTWATCH_COV ? 'lib-cov' : 'lib';
var Client = require('../../nightwatch.js');
var Logger = require('../../../' + BASE_PATH + '/util/logger');

module.exports = {
setUp: function (callback) {
Expand Down Expand Up @@ -151,6 +153,7 @@ module.exports = {

testSetOptions : function(test) {
var client = this.client = Client.init({
log_screenshot_data: false,
use_xpath : true,
launch_url : '/home'
});
Expand All @@ -173,6 +176,7 @@ module.exports = {

eq(client.options.screenshots.enabled, false);
eq(client.api.options.screenshots, false);
eq(Logger.settings.screenshot_data, false);

test.done();
},
Expand Down