Skip to content

Commit

Permalink
- updated expect.element().value to use the getProperty('value')
Browse files Browse the repository at this point in the history
…command;

- fixed a problem with `.within()` command for component tests
  • Loading branch information
beatfactor committed Dec 28, 2022
1 parent 2b6394d commit 972b30a
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/pages/google/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
consentModal: {
selector: consentModal,
elements: {
customizeButton: 'div.VDity button:nth-child(1)'
rejectAllButton: '.GzLjMd button:nth-child(1)'
}
}
},
Expand Down
8 changes: 2 additions & 6 deletions examples/tests/googlePageObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ describe('google search with consent form - page objects', function() {

after(async (browser) => browser.quit());

it('should complete the consent form', async function (browser) {

it.only('should complete the consent form', async function (browser) {
const consentPresent = await homePage.isPresent('@consentModal');

if (consentPresent) {
await homePage.expect.section('@consentModal').to.be.visible;

const {consentModal} = homePage.section;
await consentModal.click('@customizeButton');

await browser.expect.url().toContain('https://consent.google.');
await consentPage.turnOffEverything();
await consentModal.click('@rejectAllButton');
}
});

Expand Down
2 changes: 1 addition & 1 deletion lib/api/_loaders/_base-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ class BaseLoader extends EventEmitter {
}

//prevent unhandled rejection.
node.deferred.promise.catch(err=> {
node.deferred.promise.catch(err => {
BaseLoader.lastDeferred.reject(err);
});

Expand Down
1 change: 0 additions & 1 deletion lib/api/_loaders/_command-loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const Element = require('../../element');
const BaseLoader = require('./_base-loader.js');
const Utils = require('../../utils');

class BaseCommandLoader extends BaseLoader {
constructor(nightwatchInstance) {
Expand Down
5 changes: 2 additions & 3 deletions lib/api/_loaders/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,11 @@ class CommandLoader extends BaseCommandLoader {
return;
}

Logger.error(err);
if (!['NightwatchAssertError', 'NightwatchMountError', 'TestingLibraryError'].includes(err.name)) {
Logger.error(err);
instance.client.reporter.registerTestError(err);
}


return err;
})
.then(result => {
Expand All @@ -207,7 +206,7 @@ class CommandLoader extends BaseCommandLoader {
}

if (result && result.status === -1 && instance.reportProtocolErrors(result) && reportErrors) {
let err = new Error(`Error while running .${this.commandName}(): ${result.error}`);;
let err = new Error(`Error while running .${this.commandName}(): ${result.error}`);

if (result.stack) {
err.stack = result.stack;
Expand Down
12 changes: 10 additions & 2 deletions lib/api/_loaders/within-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WithinLoader extends BaseLoader {
if (nightwatchInstance.startSessionEnabled) {
return apiLoader
.loadCustomCommands(__commands_cache)
.then(() => apiLoader.initPluginTransforms())
.then(() => apiLoader.loadPlugins(__commands_cache))
.then(() => resolve());
}
Expand All @@ -36,9 +37,16 @@ class WithinLoader extends BaseLoader {
}

loadApi(context) {
lodashMerge(context, __commands_cache);

const ApiLoader = require('../index.js');

Object.keys(__commands_cache).forEach((command) => {
context[command] = ((commandName) => {
return (...args) => {
return this.nightwatchInstance.api[commandName](...args);
};
})(command);
});

const elementCommands = ApiLoader.getElementsCommandsStrict();

if (elementCommands.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/expect/assertions/element/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ValueAssertion extends BaseAssertion {
}

executeCommand() {
return this.executeProtocolAction('getElementAttribute', ['value']);
return this.executeProtocolAction('getElementProperty', ['value']);
}

onExecuteCommandResult(result) {
Expand Down
21 changes: 12 additions & 9 deletions lib/page-object/command-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,26 @@ class Command {
createWrapper(commandFn) {
const self = this;

return (...args) => {
if (args.length > 0 && self.parent && self.parent.__needsRecursion) {
return function (...args) {

if (args.length > 0 && this.__needsRecursion) {
// within commands
let inputElement = Element.createFromSelector(args[0]);

if (this.isUserDefined) {
inputElement.container = self.parent.__element;
if (self.isUserDefined) {
inputElement.container = this.__element;
args[0] = inputElement;
} else {
args[0] = Element.createFromSelector({
locateStrategy: 'recursion',
selector: [self.parent.__element, inputElement]
selector: [this.__element, inputElement]
});
}
}

const result = self.executeCommand(commandFn, args);
const {client = {}} = self.parent;
const client = this.client || self.parent && self.parent.client || {};
const result = self.executeCommand(commandFn, args, client);

const {isES6AsyncTestcase} = client;

if ((result instanceof Promise) && (self.parent.constructor.name === 'Page' || isES6AsyncTestcase)) {
Expand Down Expand Up @@ -229,8 +231,9 @@ class Command {
/**
* @param {Function} commandFn
* @param {Array} args
* @param {Object} context
*/
executeCommand(commandFn, args) {
executeCommand(commandFn, args, context) {
let parseArgs;
if (Utils.isObject(args[0]) && Array.isArray(args[0].args)) {
parseArgs = args[0].args;
Expand All @@ -240,7 +243,7 @@ class Command {

this.parseElementSelector(parseArgs);

return commandFn.apply(this.parent.client, args);
return commandFn.apply(context, args);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/lib/nocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ module.exports = {

value(value, times) {
var mock = nock('http://localhost:10195')
.get('/wd/hub/session/1352110219202/element/0/attribute/value');
.get('/wd/hub/session/1352110219202/element/0/property/value');

if (times) {
mock.times(times);
Expand Down
2 changes: 1 addition & 1 deletion test/src/api/expect/testExpectValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('expect.value', function () {
.elementFound()
.elementStateError({
error: 'stale element reference',
url: '/wd/hub/session/1352110219202/element/0/attribute/value',
url: '/wd/hub/session/1352110219202/element/0/property/value',
method: 'get'
})
.elementNotFound()
Expand Down

0 comments on commit 972b30a

Please sign in to comment.