Skip to content

Commit

Permalink
refactored code of findElement() in element-global.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
yashPratp983 committed Aug 11, 2023
2 parents b6de5d5 + 125d607 commit 7e161b8
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions lib/api/_loaders/element-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,23 @@ class ElementGlobal {

const byLocator = Locator.create(locator);

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

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

if (this.element.length === 0) {
throw new Error(`Element ${locator} was not found.`);
if (elements.length === 0) {
throw new Error(`Element ${byLocator} 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 (typeof(this.index) != 'number'){
throw new Error(`Index ${this.index} is not of type number for locator: ${byLocator}`);
}

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

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

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

static isElementObject(element) {
Expand Down Expand Up @@ -266,6 +259,7 @@ class ElementGlobal {
if (commandName === 'findElement' && args.length === 0) {
return this.element;
}

args = this.computeArguments(args, commandName);
let value;
let error;
Expand Down

0 comments on commit 7e161b8

Please sign in to comment.