Skip to content

Commit fe9a010

Browse files
committedDec 29, 2023
Added support for defining a custom return function for custom command
1 parent c60f3f2 commit fe9a010

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed
 

‎lib/api/_loaders/_base-loader.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,13 @@ class BaseLoader extends EventEmitter {
467467
BaseLoader.lastDeferred.reject(err);
468468
});
469469

470-
return node.deferred.promise;
470+
if (!this.module?.returnFn) {
471+
return node.deferred.promise;
472+
}
473+
}
474+
475+
if (this.module?.returnFn) {
476+
return this.module.returnFn(node, apiToReturn || api);
471477
}
472478

473479
return apiToReturn || api;

‎lib/api/web-element/scoped-element.js

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ class ScopedWebElement {
147147

148148
async findElementAction({parentElement, condition, index, timeout, retryInterval, abortOnFailure, suppressNotFoundErrors}) {
149149
const createAction = () => async ({args}) => {
150+
if ((args[0] instanceof Promise) && !args[0]['@nightwatch_element']) {
151+
args[0] = await args[0];
152+
}
153+
150154
const parentElement = args[0];
151155

152156
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const assert = require('assert');
2+
3+
describe('Test using custom commands with returnFn', function() {
4+
before(browser => {
5+
browser
6+
.url('http://localhost');
7+
});
8+
9+
it('sampleTest', browser => {
10+
const result = browser.customCommandReturnFn();
11+
assert.deepStrictEqual(result, {status: 0});
12+
browser.end();
13+
});
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = class CustomCommandReturnFn {
2+
static get returnFn() {
3+
return function(node, api) {
4+
api.globals.count++;
5+
6+
return {
7+
status: 0
8+
};
9+
};
10+
}
11+
12+
command() {
13+
this.api.perform(function() {
14+
this.globals.count++;
15+
});
16+
}
17+
};

‎test/src/apidemos/custom-commands/testCustomCommandAutoInvoke.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('custom commands with auto invoke', function() {
2020
});
2121
});
2222

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

53+
it('test custom command with returnFn', function() {
54+
const testsPath = path.join(__dirname, '../../../apidemos/custom-commands/testUsingCommandReturnFn.js');
55+
Mocks.createNewW3CSession({
56+
testName: 'Test using custom commands with returnFn'
57+
});
58+
59+
const globals = {
60+
waitForConditionPollInterval: 50,
61+
waitForConditionTimeout: 120,
62+
retryAssertionTimeout: 1000,
63+
count: 0,
64+
reporter(results) {
65+
if (results.lastError) {
66+
throw results.lastError;
67+
}
68+
assert.strictEqual(this.count, 2);
69+
}
70+
};
71+
72+
return NightwatchClient.runTests(testsPath, settings({
73+
output: true,
74+
silent: false,
75+
selenium_host: null,
76+
custom_commands_path: [path.join(__dirname, '../../../extra/commands/returnFn')],
77+
globals
78+
}));
79+
});
80+
5381
});

0 commit comments

Comments
 (0)
Please sign in to comment.