diff --git a/lib/element/locator.js b/lib/element/locator.js index 3c517efed3..d44155dbe1 100644 --- a/lib/element/locator.js +++ b/lib/element/locator.js @@ -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}".`); } @@ -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() { @@ -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(); @@ -319,5 +334,5 @@ class NoSuchElementError extends Error { } } -module.exports = LocateElement; +module.exports = Locator; module.exports.NoSuchElementError = NoSuchElementError;