Skip to content

Commit

Permalink
made element api to return the web element at the provided index
Browse files Browse the repository at this point in the history
  • Loading branch information
yashPratp983 committed Aug 11, 2023
1 parent 33476a7 commit b6de5d5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
31 changes: 27 additions & 4 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 @@ -89,7 +90,30 @@ class ElementGlobal {

const byLocator = Locator.create(locator);

this.element = await this.transport.driver.wait(until.elementLocated(byLocator), this.timeout, null, this.retryInterval);
this.element = await this.transport.driver.wait(until.elementsLocated(byLocator), this.timeout, null, this.retryInterval);

this.index = locator.__index;

if (this.element.length === 0) {
throw new Error(`Element ${locator} was not found.`);
}

if (!this.index){
this.element = this.element[0];

return;
}

if (typeof(locator.__index) != 'number'){
throw new Error('Index should be of type number');
}

if (this.element.lenght < this.index){
throw new Error('Index is out of bounds.');
}

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

}

static isElementObject(element) {
Expand Down Expand Up @@ -163,7 +187,7 @@ class ElementGlobal {
value = {
value: locator,
using: this.client.locateStrategy
};
};
} else {
value = locator;
}
Expand Down Expand Up @@ -242,7 +266,6 @@ class ElementGlobal {
if (commandName === 'findElement' && args.length === 0) {
return this.element;
}

args = this.computeArguments(args, commandName);
let value;
let error;
Expand All @@ -269,7 +292,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
4 changes: 2 additions & 2 deletions test/apidemos/elements/elementGlobalTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('get text using element-global', function () {
test('test for element properties as argument', async function () {
const weblogin = element({selector: '#weblogin', index: 1});
const id = await weblogin.getId();

assert.strictEqual(id, '5cc459b8-36a8-3042-8b4a-258883ea642b');
assert.strictEqual(id, '3783b042-7001-0740-a2c0-afdaac732e9f');
});

});

0 comments on commit b6de5d5

Please sign in to comment.