Skip to content

Commit

Permalink
Added support for defining a custom return function for custom command
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Dec 29, 2023
1 parent c60f3f2 commit fe9a010
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/api/_loaders/_base-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,13 @@ class BaseLoader extends EventEmitter {
BaseLoader.lastDeferred.reject(err);
});

return node.deferred.promise;
if (!this.module?.returnFn) {
return node.deferred.promise;
}
}

if (this.module?.returnFn) {
return this.module.returnFn(node, apiToReturn || api);
}

return apiToReturn || api;
Expand Down
4 changes: 4 additions & 0 deletions lib/api/web-element/scoped-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ class ScopedWebElement {

async findElementAction({parentElement, condition, index, timeout, retryInterval, abortOnFailure, suppressNotFoundErrors}) {
const createAction = () => async ({args}) => {
if ((args[0] instanceof Promise) && !args[0]['@nightwatch_element']) {
args[0] = await args[0];
}

const parentElement = args[0];

try {
Expand Down
14 changes: 14 additions & 0 deletions test/apidemos/custom-commands/testUsingCommandReturnFn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const assert = require('assert');

describe('Test using custom commands with returnFn', function() {
before(browser => {
browser
.url('http://localhost');
});

it('sampleTest', browser => {
const result = browser.customCommandReturnFn();
assert.deepStrictEqual(result, {status: 0});
browser.end();
});
});
17 changes: 17 additions & 0 deletions test/extra/commands/returnFn/customCommandReturnFn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = class CustomCommandReturnFn {
static get returnFn() {
return function(node, api) {
api.globals.count++;

return {
status: 0
};
};
}

command() {
this.api.perform(function() {
this.globals.count++;
});
}
};
30 changes: 29 additions & 1 deletion test/src/apidemos/custom-commands/testCustomCommandAutoInvoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('custom commands with auto invoke', function() {
});
});

it('custom find elements es6 async', function() {
it('test custom command using autoInvoke', function() {
const testsPath = path.join(__dirname, '../../../apidemos/custom-commands/testUsingAutoInvokeCommand.js');
Mocks.createNewW3CSession({
testName: 'Test Using ES6 Async Custom Commands'
Expand Down Expand Up @@ -50,4 +50,32 @@ describe('custom commands with auto invoke', function() {
}));
});

it('test custom command with returnFn', function() {
const testsPath = path.join(__dirname, '../../../apidemos/custom-commands/testUsingCommandReturnFn.js');
Mocks.createNewW3CSession({
testName: 'Test using custom commands with returnFn'
});

const globals = {
waitForConditionPollInterval: 50,
waitForConditionTimeout: 120,
retryAssertionTimeout: 1000,
count: 0,
reporter(results) {
if (results.lastError) {
throw results.lastError;
}
assert.strictEqual(this.count, 2);
}
};

return NightwatchClient.runTests(testsPath, settings({
output: true,
silent: false,
selenium_host: null,
custom_commands_path: [path.join(__dirname, '../../../extra/commands/returnFn')],
globals
}));
});

});

0 comments on commit fe9a010

Please sign in to comment.