Skip to content

Commit

Permalink
Save command result values in Json Report. (#4258)
Browse files Browse the repository at this point in the history
* Save command result values in Json Report.

* Small fix in saved file name.

* Fix result from new Element API commands not getting returned to asynctree and logged.

Also fixes a bug where the nodes from new Element API were getting resolved early due to which the following Nightwatch commands were getting added as child nodes of the last new Element API command.
  • Loading branch information
garg3133 authored Sep 17, 2024
1 parent e06fd65 commit da3cc62
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
18 changes: 11 additions & 7 deletions lib/api/web-element/scoped-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,23 @@ class ScopedWebElement {

const createAction = (actions, webElement) => function () {
if (isFunction(commandName)) {
return commandName(webElement, ...args).then((result) => {
node.deferred.resolve(result);
});
return commandName(webElement, ...args);
}

return actions[commandName](webElement, ...args).then((result) => {
// eslint-disable-next-line no-prototype-builtins
node.deferred.resolve(result.hasOwnProperty('value') ? result.value : result);
});
return actions[commandName](webElement, ...args);
};

const node = this.queueAction({name: commandName, createAction});

// TODO: check what changes if we keep the original `getResult` instead of below.
node.getResult = function(result) {
// here, we resolve the node with `result.value` even if the result contains an error and status === -1
// which results in the command result to be `null` in test case as well, while the command actually failed.
// Is this expected? To return `null` result in case of failure as well?
// eslint-disable-next-line no-prototype-builtins
return result.hasOwnProperty('value') ? result.value : result;
};

return node;
}

Expand Down
12 changes: 12 additions & 0 deletions lib/reporter/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path');
const Utils = require('../utils');
const TestResults = require('./results.js');
const SimplifiedReporter = require('./simplified.js');
Expand Down Expand Up @@ -217,6 +218,17 @@ class Reporter extends SimplifiedReporter {
stack,
beautifiedStack
};

if (this.settings.reporter_options.save_command_result_value) {
if (Utils.isString(result.value) && result.value.length > 500) {
const filepath = path.join(this.settings.output_folder || '', 'tmp', this.testResults.uuid, `${node.fullName}_${Date.now()}.txt`);
commandResult.valuePath = filepath;

Utils.writeToFile(filepath, result.value);
} else {
commandResult.value = result.value;
}
}
}

if (this.shouldLogCommand(node)) {
Expand Down
7 changes: 7 additions & 0 deletions lib/reporter/results.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const lodashMerge = require('lodash/merge');
const uuid = require('uuid');
const Utils = require('../utils');
const {Logger} = Utils;

Expand Down Expand Up @@ -54,6 +55,7 @@ module.exports = class Results {
this.name = opts.suiteName || '';
this.tags = opts.tags || [];
this.__retryTest = false;
this.__uuid = uuid.v4();

this.initCount(allScreenedTests);
}
Expand Down Expand Up @@ -103,6 +105,10 @@ module.exports = class Results {
return this.getTestResult(testName, {returnFullResult: true});
}

get uuid() {
return this.__uuid;
}

/**
* @param {TestCase} value
*/
Expand Down Expand Up @@ -234,6 +240,7 @@ module.exports = class Results {
moduleKey,
hasFailures: !this.testsPassed(),
results: {
uuid: this.uuid,
reportPrefix,
assertionsCount: this.initialResult.assertions.length
}
Expand Down
5 changes: 4 additions & 1 deletion lib/settings/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ module.exports = {
// The file name formatting to use while saving HTML report
filename_format: null,

minimal_report_file_path: 'tests_output/minimal_report.json'
minimal_report_file_path: 'tests_output/minimal_report.json',

// Save command result values in Json report (inline or in separate file).
save_command_result_value: false
},

// A string or array of folders (excluding subfolders) where the tests are located.
Expand Down

0 comments on commit da3cc62

Please sign in to comment.