Skip to content

Commit

Permalink
Added a few improvements to better support component testing
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Feb 3, 2022
1 parent acbcf27 commit d8dec00
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/api/_loaders/element-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ class ElementGlobal {
Object.assign(node.deferred.promise, api);
if (commandName === 'findElement') {
node.deferred.promise['@nightwatch_element'] = true;
node.deferred.promise['@nightwatch_args'] = args;
if (this.isComponent) {
node.deferred.promise['@nightwatch_component'] = true;
}
Expand Down
13 changes: 13 additions & 0 deletions lib/api/_loaders/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,21 @@ class ExpectLoader extends BaseCommandLoader {
}

if ((args[0] instanceof Promise) && args[0]['@nightwatch_element']) {
const elementArgs = args[0]['@nightwatch_args'];
let selector = '';
if (elementArgs[0] && elementArgs[0].value) {
selector = elementArgs[0].value;
}
args[0].then(result => {
args[0] = result;
if (!result) {
const err = new Error(`Unable to find element <${selector}>.`);
err.isExpect = true;
this.emit('error', err);

return this;
}

this.run(...args);
});
} else {
Expand Down
4 changes: 4 additions & 0 deletions lib/api/element-commands/_baseElementCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class BaseElementCommand extends ElementCommand {
};
}

if ((this.selector instanceof Promise) && this.selector['@nightwatch_element']) {
this.__element = await this.selector;
}

return this.findElement({cacheElementId});
}

Expand Down
10 changes: 8 additions & 2 deletions lib/api/protocol/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ const Element = require('../../element');
module.exports = class Elements extends ProtocolAction {
command(using, value, callback) {
const commandName = 'elements';

let element;
if (using instanceof Element) {
element = using;
} else if (value instanceof Element) {
element = value;
}

if (element) {
return this.findElements({
element: using,
element,
callback: typeof value == 'function' ? value : callback,
commandName
});
Expand Down
9 changes: 8 additions & 1 deletion lib/element/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,23 @@ class Element {
throw new Error(`Invalid selector value specified "${value}"`);
}

if (value instanceof Element) {
if ((value instanceof Element) && value.selector) {
if (!value.locateStrategy) {
value.locateStrategy = using;
}

return value;
}

if (value['@nightwatch_element']) {
return value;
}

let definition;
let options;
if ((value instanceof Element) && (value.webElement instanceof WebElement)) {
return value;
}

if (WebElement.isId(value)) {
definition = {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dependencies": {
"assertion-error": "1.1.0",
"boxen": "5.1.2",
"chai-nightwatch": "0.5.2",
"chai-nightwatch": "0.5.3",
"ci-info": "^3.2.0",
"didyoumean": "^1.2.2",
"dotenv": "10.0.0",
Expand Down
43 changes: 43 additions & 0 deletions test/src/runner/testRunWithExternalGlobals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const path = require('path');
const assert = require('assert');
const common = require('../../common.js');
const CommandGlobals = require('../../lib/globals/commands.js');
const MockServer = require('../../lib/mockserver.js');
const {settings} = common;
const {runTests} = common.require('index.js');

describe('testRunWithExternalGlobals', function() {
beforeEach(function(done) {
this.server = MockServer.init();

this.server.on('listening', () => {
done();
});
});

afterEach(function(done) {
CommandGlobals.afterEach.call(this, done);
});

beforeEach(function() {
process.removeAllListeners('exit');
process.removeAllListeners('uncaughtException');
process.removeAllListeners('unhandledRejection');
});

it('testRun with external globals', function() {
let testsPath = path.join(__dirname, '../../sampletests/before-after/sampleSingleTest.js');
const globals = {
reporterCount: 0
};

return runTests(testsPath, settings({
globals,
globals_path: path.join(__dirname, '../../extra/external-globals.js'),

output_folder: false
}));
});


});

0 comments on commit d8dec00

Please sign in to comment.