Skip to content

Commit

Permalink
Reject custom commands node promise on abortOnFailure. (#4314)
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 authored Dec 27, 2024
1 parent 715a751 commit 44ba57c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
12 changes: 12 additions & 0 deletions lib/api/_loaders/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ class CommandLoader extends BaseCommandLoader {

createWrapper() {
if (this.module) {
// this place is only reached by client-commands, protocol commands and custom-commands (no assertions or element-commands).
if (this.isUserDefined) {
// only custom-commands will reach here.
// later extend this to client-commands and protocol commands as well.
Object.defineProperty(this.module, 'rejectNodeOnAbortFailure', {
configurable: true,
get() {
return true;
}
});
}

this.commandFn = function commandFn({args, stackTrace}) {
const instance = CommandLoader.createInstance(this.nightwatchInstance, this.module, {
stackTrace,
Expand Down
5 changes: 2 additions & 3 deletions lib/core/asynctree.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class AsyncTree extends EventEmitter{
shouldRejectNodePromise(err, abortOnFailure, node = this.currentNode) {
const rejectNodeOnAbortFailure = node.options?.rejectNodeOnAbortFailure;

if ((err.isExpect || node.namespace === 'assert' || (abortOnFailure && rejectNodeOnAbortFailure)) && this.currentNode.isES6Async) {
if ((err.isExpect || node.namespace === 'assert' || (abortOnFailure && rejectNodeOnAbortFailure)) && node.isES6Async) {
return true;
}

Expand Down Expand Up @@ -134,7 +134,6 @@ class AsyncTree extends EventEmitter{
this.emit('asynctree:command:start', {node});

const result = await node.run();
const {parent} = this.currentNode;

let abortOnFailure = false;
let err;
Expand All @@ -155,7 +154,7 @@ class AsyncTree extends EventEmitter{
}

if (this.shouldRejectParentNodePromise(err, node)) {
parent.reject(err);
node.parent.reject(err);
}
} else {
node.resolveValue = result;
Expand Down
5 changes: 5 additions & 0 deletions test/extra/commands/customCommandWithFailureClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = class CustomCommandWithFailureClass{
async command() {
await this.api.waitForElementPresent('#badElement', 100);
}
};
15 changes: 15 additions & 0 deletions test/sampletests/withcustomcommands/sampleWithAsyncFailures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
before(client) {
client.globals.increment++;
},

async demoTestAsync(client) {
await client.url('http://localhost').customCommandWithFailureClass();
// below statement should be unreachable because the custom command should be rejected
client.globals.increment++;
},

after(client) {
client.globals.increment++;
}
};
22 changes: 11 additions & 11 deletions test/src/runner/testRunWithCustomCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ describe('testRunWithCustomCommands', function() {
});

it('testRunner with custom command which has failures', function() {
let testsPath = path.join(__dirname, '../../sampletests/withcustomcommands');
let globals = {
const testsPath = path.join(__dirname, '../../sampletests/withcustomcommands');
const globals = {
increment: 0,
retryAssertionTimeout: 0,
waitForConditionPollInterval: 10,
waitForConditionTimeout: 20,
reporter(results, cb) {
assert.strictEqual(globals.increment, 4);
assert.strictEqual(globals.increment, 6);
cb();
}
};
Expand All @@ -55,12 +55,12 @@ describe('testRunWithCustomCommands', function() {
});

it('testRunner with ES6 Async custom commands', function() {
let testsPath = path.join(__dirname, '../../sampletests/withes6asynccommands');
const testsPath = path.join(__dirname, '../../sampletests/withes6asynccommands');
let testResults;
const origExit = process.exit;
process.exit = function() {};

let globals = {
const globals = {
increment: 0,
logResult: null,
retryAssertionTimeout: 0,
Expand Down Expand Up @@ -101,12 +101,12 @@ describe('testRunWithCustomCommands', function() {
});

it('testRunner with ES6 Async custom commands', function() {
let testsPath = path.join(__dirname, '../../sampletests/withes6asynccommands');
const testsPath = path.join(__dirname, '../../sampletests/withes6asynccommands');
let testResults;
const origExit = process.exit;
process.exit = function() {};

let globals = {
const globals = {
increment: 0,
logResult: null,
retryAssertionTimeout: 0,
Expand Down Expand Up @@ -147,12 +147,12 @@ describe('testRunWithCustomCommands', function() {
});

it('testRunner custom command which extends built-in command', function() {
let testsPath = path.join(__dirname, '../../sampletests/withcustomcommands/element');
const testsPath = path.join(__dirname, '../../sampletests/withcustomcommands/element');
let testResults;
const origExit = process.exit;
process.exit = function() {};

let globals = {
const globals = {
increment: 0,
logResult: null,
retryAssertionTimeout: 0,
Expand Down Expand Up @@ -229,12 +229,12 @@ describe('testRunWithCustomCommands', function() {
});

it('testRunner custom command path as glob pattern', function() {
let testsPath = path.join(__dirname, '../../sampletests/withcustomcommands/element');
const testsPath = path.join(__dirname, '../../sampletests/withcustomcommands/element');
let testResults;
const origExit = process.exit;
process.exit = function() {};

let globals = {
const globals = {
increment: 0,
logResult: null,
retryAssertionTimeout: 0,
Expand Down

0 comments on commit 44ba57c

Please sign in to comment.