Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable global element api to accept element properties as argument. #3835

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
5f87d1d
enabled global element api to accept element properties as argument
yashPratp983 Jul 27, 2023
d45e64f
fixed global element api to work with element properties as argument
yashPratp983 Aug 7, 2023
14d51fe
Linting fix
garg3133 Aug 8, 2023
b15e01f
added test for global element api to accept element property as argument
yashPratp983 Aug 9, 2023
d0f988a
Merge branch 'global-element-api' of https://github.com/yashPratp983/…
yashPratp983 Aug 9, 2023
125d607
set the output to false for the tests of element api
yashPratp983 Aug 9, 2023
5050d29
enabled global element api to accept element properties as argument
yashPratp983 Jul 27, 2023
2420d96
fixed global element api to work with element properties as argument
yashPratp983 Aug 7, 2023
9e8100b
added test for global element api to accept element property as argument
yashPratp983 Aug 9, 2023
1609771
Linting fix
garg3133 Aug 8, 2023
33476a7
set the output to false for the tests of element api
yashPratp983 Aug 9, 2023
b6de5d5
made element api to return the web element at the provided index
yashPratp983 Aug 11, 2023
7e161b8
refactored code of findElement() in element-global.js file
yashPratp983 Aug 11, 2023
1633f8e
refactored code of findElement() in element-global.js file
yashPratp983 Aug 11, 2023
d7194dd
made element api to return the web element at the provided index
yashPratp983 Aug 11, 2023
2f42697
refactored the way of checking the type of index from using typeof to…
yashPratp983 Aug 12, 2023
5d6c169
moved condition for handling for an element having instance of Elemen…
yashPratp983 Sep 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lib/api/_loaders/element-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ElementGlobal {
this.abortOnFailure = this.settings.globals.abortOnElementLocateError;
this.timeout = this.settings.globals.waitForConditionTimeout;
this.retryInterval = this.settings.globals.waitForConditionPollInterval;
this.index = 0;
this.init();
}

Expand All @@ -80,14 +81,30 @@ class ElementGlobal {
return;
}

const {locator} = this;
let {locator} = this;
if ((locator instanceof Element) && locator.resolvedElement) {
this.element = this.createWebElement(locator.resolvedElement);

return;
}

this.element = await this.transport.driver.wait(until.elementLocated(locator), this.timeout, null, this.retryInterval);
if (locator instanceof Element) {
this.setPropertiesFromElement(locator);

locator = Locator.create(locator);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are multiple calls to Locator.create. We should refactor this module and everything should be handled in setLocator method.

}

const elements = await this.transport.driver.wait(until.elementsLocated(locator), this.timeout, null, this.retryInterval);

if (elements.length === 0) {
throw new Error(`No element was found for locator: ${locator}`);
}

if (elements.length <= this.index){
throw new Error(`Index ${this.index} out of bounds for locator: ${locator}`);
}

this.element = elements[this.index];
}

static isElementObject(element) {
Expand All @@ -111,7 +128,7 @@ class ElementGlobal {
abortOnFailure, retryInterval, timeout, suppressNotFoundErrors, index
} = element;

if (isDefined(index)) {
if (isDefined(index) && typeof(index) == 'number') {
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
this.index = index;
}

Expand Down Expand Up @@ -161,7 +178,7 @@ class ElementGlobal {
value = {
value: locator,
using: this.client.locateStrategy
};
};
} else {
value = locator;
}
Expand Down Expand Up @@ -267,7 +284,7 @@ class ElementGlobal {
});
}

const lastArg = args[args.length-1];
const lastArg = args[args.length - 1];
if (isFunction(lastArg)) {
if (error) {
return lastArg.call(this.api, {
Expand Down
7 changes: 7 additions & 0 deletions test/apidemos/elements/elementGlobalTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ describe('get text using element-global', function () {
assert.strictEqual(signupSectionId, '0');
});

test('test for element properties as argument', async function () {
const weblogin = element({selector: '#weblogin', index: 1});
const id = await weblogin.getId();

assert.strictEqual(id, '3783b042-7001-0740-a2c0-afdaac732e9f');
});

});
7 changes: 4 additions & 3 deletions test/lib/command-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,15 @@ module.exports = {
return this;
},

w3cVisible(value = true) {
w3cVisible(value = true, {times = 0} = {}) {
MockServer.addMock({
url: '/session/13521-10219-202/execute/sync',
method: 'POST',
response: JSON.stringify({
value
})
}, true);
}),
times
}, times === 0);
},

w3cSelected(elementId ='5cc459b8-36a8-3042-8b4a-258883ea642b', value = true) {
Expand Down
16 changes: 13 additions & 3 deletions test/src/apidemos/elements/testElementGlobal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ describe('element global demos', function() {

it('getText on element global instance', function() {
const testsPath = path.join(__dirname, '../../../apidemos/elements/elementGlobalTest.js');
Mocks.elementText();
Mocks.tagName('0', 'div');
Mocks.visible('0', true, {
MockServer.addMock({
url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/name',
method: 'GET',
response: JSON.stringify({
value: 'div'
})
}, true);
Mocks.w3cVisible(true, {
times: 2
});
Mocks.getElementText({
elementId: '5cc459b8-36a8-3042-8b4a-258883ea642b',
responseText: 'sample text'
});

const globals = {
waitForConditionPollInterval: 50,
Expand All @@ -40,6 +49,7 @@ describe('element global demos', function() {
};

return NightwatchClient.runTests(testsPath, settings({
selenium_host: null,
output: false,
skip_testcases_on_fail: false,
globals
Expand Down
Loading