Skip to content

Commit

Permalink
Create Locator sub-class to work with Appium client.
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 committed Feb 3, 2023
1 parent 4f740d7 commit 42d08a7
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions lib/element/locator.js
Expand Up @@ -17,10 +17,9 @@ const availableLocators = {
class LocateElement {
/**
* @param {object|string} element
* @param {boolean} isAppiumClient
* @return {*}
* @return {Element|By|RelativeBy}
*/
static create(element, isAppiumClient) {
static create(element) {
if (!element) {
throw new Error(`Error while trying to locate element: missing element definition; got: "${element}".`);
}
Expand Down Expand Up @@ -51,13 +50,7 @@ class LocateElement {
selector = element;
}

const elementInstance = Element.createFromSelector(selector, strategy);

if (isAppiumClient && strategy === 'id') {
return new By(strategy, selector);
}

return By[availableLocators[elementInstance.locateStrategy]](elementInstance.selector);
return Element.createFromSelector(selector, strategy);
}

get api() {
Expand Down Expand Up @@ -306,6 +299,28 @@ class LocateElement {
}
}

class Locator extends LocateElement {
/**
* @param {object|string} element
* @param {boolean} [isAppiumClient]
* @return {By|RelativeBy}
*/
static create(element, isAppiumClient) {
const elementInstance = super.create(element);

if (!(elementInstance instanceof Element)) {
// elementInstance actually an instance of By or RelativeBy
return elementInstance;
}

if (isAppiumClient && elementInstance.locateStrategy === 'id') {
return new By(elementInstance.locateStrategy, elementInstance.selector);
}

return By[availableLocators[elementInstance.locateStrategy]](elementInstance.selector);
}
}

class NoSuchElementError extends Error {
constructor({element, ms, abortOnFailure, retries}) {
super();
Expand All @@ -319,5 +334,5 @@ class NoSuchElementError extends Error {
}
}

module.exports = LocateElement;
module.exports = Locator;
module.exports.NoSuchElementError = NoSuchElementError;

0 comments on commit 42d08a7

Please sign in to comment.