Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
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
99 changes: 55 additions & 44 deletions lib/helper/WebDriverIO.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class WebDriverIO extends Helper {
withinStore.elFn = this.browser.element;
withinStore.elsFn = this.browser.elements;
this.context = locator;
return client.element(withStrictLocator(locator)).then((res) => {
return client.element(withStrictLocator.call(this, locator)).then((res) => {
this.browser.element = function (l) {
return this.elementIdElement(res.value.ELEMENT, l);
};
Expand Down Expand Up @@ -416,12 +416,12 @@ class WebDriverIO extends Helper {
*/
_locate(locator, smartWait = false) {

if (!this.options.smartWait || !smartWait) return this.browser.elements(withStrictLocator(locator));
if (!this.options.smartWait || !smartWait) return this.browser.elements(withStrictLocator.call(this, locator));
let els;

return this.defineTimeout({implicit: this.options.smartWait})
.then(() => this.debugSection('SmartWait', `Locating ${locator} in ${this.options.smartWait}`))
.then(() => this.browser.elements(withStrictLocator(locator)));
.then(() => this.browser.elements(withStrictLocator.call(this, locator)));
}

/**
Expand All @@ -432,7 +432,7 @@ class WebDriverIO extends Helper {
* ```
*/
_locateCheckable(locator) {
return findCheckable(locator, this.browser.elements.bind(this)).then((res) => res.value);
return findCheckable.call(this, locator, this.browser.elements.bind(this)).then((res) => res.value);
}

/**
Expand All @@ -443,7 +443,7 @@ class WebDriverIO extends Helper {
* ```
*/
_locateClickable(locator) {
return findClickable(locator, this.browser.elements.bind(this)).then((res) => res.value);
return findClickable.call(this, locator, this.browser.elements.bind(this)).then((res) => res.value);
}

/**
Expand Down Expand Up @@ -505,7 +505,7 @@ class WebDriverIO extends Helper {
let clickMethod = this.browser.isMobile ? 'touchClick' : 'elementIdClick';
let locateFn = prepareLocateFn.call(this, context);

return findClickable(locator, locateFn).then((res) => {
return findClickable.call(this, locator, locateFn).then((res) => {
if (!res.value || res.value.length === 0) {
if (typeof locator === "object") locator = JSON.stringify(locator);
if (context) locator += ` inside ${context}`;
Expand All @@ -523,7 +523,7 @@ class WebDriverIO extends Helper {
let clickMethod = this.browser.isMobile ? 'touchClick' : 'elementIdClick';
let locateFn = prepareLocateFn.call(this, context);

return findClickable(locator, locateFn).then((res) => {
return findClickable.call(this, locator, locateFn).then((res) => {
if (!res.value || res.value.length === 0) {
throw new ElementNotFound(locator, "Clickable element");
}
Expand Down Expand Up @@ -667,7 +667,7 @@ class WebDriverIO extends Helper {

let locateFn = prepareLocateFn.call(this, context);

return findCheckable(field, locateFn).then((res) => {
return findCheckable.call(this, field, locateFn).then((res) => {
if (!res.value || res.value.length === 0) {
throw new ElementNotFound(field, "Checkable");
}
Expand Down Expand Up @@ -710,7 +710,7 @@ class WebDriverIO extends Helper {
* ```
*/
grabHTMLFrom(locator) {
return this.browser.getHTML(withStrictLocator(locator)).then(function (html) {
return this.browser.getHTML(withStrictLocator.call(this, locator)).then(function (html) {
return html;
});
}
Expand Down Expand Up @@ -905,7 +905,7 @@ class WebDriverIO extends Helper {
* Appium: support
*/
dontSeeElement(locator) {
return this.browser.elements(withStrictLocator(locator)).then((res) => {
return this.browser.elements(withStrictLocator.call(this, locator)).then((res) => {
if (!res.value || res.value.length === 0) {
return truth(`elements of ${locator}`, 'to be seen').negate(false);
}
Expand All @@ -924,7 +924,7 @@ class WebDriverIO extends Helper {
* Appium: support
*/
seeElementInDOM(locator) {
return this.browser.elements(withStrictLocator(locator)).then(function (res) {
return this.browser.elements(withStrictLocator.call(this, locator)).then(function (res) {
return empty('elements').negate(res.value);
});
}
Expand All @@ -934,7 +934,7 @@ class WebDriverIO extends Helper {
* Appium: support
*/
dontSeeElementInDOM(locator) {
return this.browser.elements(withStrictLocator(locator)).then(function (res) {
return this.browser.elements(withStrictLocator.call(this, locator)).then(function (res) {
return empty('elements').assert(res.value);
});
}
Expand Down Expand Up @@ -991,7 +991,7 @@ class WebDriverIO extends Helper {
* ```
*/
seeNumberOfElements(selector, num) {
return this._locate(withStrictLocator(selector))
return this._locate(withStrictLocator.call(this, selector))
.then(function (res) {
return assert.equal(res.value.length, num,
`expected number of elements (${selector}) is ${num}, but found ${res.value.length}`);
Expand Down Expand Up @@ -1101,7 +1101,7 @@ class WebDriverIO extends Helper {
* ```
*/
grabNumberOfVisibleElements(locator) {
return this.browser.elements(withStrictLocator(locator)).then((res) => {
return this.browser.elements(withStrictLocator.call(this, locator)).then((res) => {
if (!res.value || res.value.length === 0) {
return 0;
}
Expand Down Expand Up @@ -1195,7 +1195,7 @@ class WebDriverIO extends Helper {
}

if (locator) {
return this._locate(withStrictLocator(locator), true).then(function (res) {
return this._locate(withStrictLocator.call(this, locator), true).then(function (res) {
if (!res.value || res.value.length === 0) {
return truth(`elements of ${locator}`, 'to be seen').assert(false);
}
Expand Down Expand Up @@ -1229,7 +1229,7 @@ class WebDriverIO extends Helper {
hasOffsetParams = false;
}

return this._locate(withStrictLocator(locator), true).then((res) => {
return this._locate(withStrictLocator.call(this, locator), true).then((res) => {
if (!res.value || res.value.length === 0) {
return truth(`elements of ${locator}`, 'to be seen').assert(false);
}
Expand Down Expand Up @@ -1442,17 +1442,17 @@ class WebDriverIO extends Helper {


if (client.isMobile) {
return client.element(withStrictLocator(srcElement)).then(function (res) {
return client.element(withStrictLocator.call(this, srcElement)).then((res) => {
if (!res.value || res.value.length === 0) return truth(`elements of ${srcElement}`, 'to be seen').assert(false);
let elem = res.value;
return this.elementIdLocation(elem.ELEMENT).then(function (location) {
return this.elementIdLocation(elem.ELEMENT).then((location) => {
if (!location.value || location.value.length === 0) {
throw new Error(
`Failed to receive (${srcElement}) location`);
}
return this.touchDown(location.value.x, location.value.y).then(function (res) {
return this.touchDown(location.value.x, location.value.y).then((res) => {
if (res.state !== 'success') throw new Error(`Failed to touch button down on (${srcElement})`);
return client.element(withStrictLocator(destElement)).then(function (res) {
return client.element(withStrictLocator.call(this, destElement)).then(function (res) {
if (!res.value || res.value.length === 0) {
return truth(`elements of ${destElement}`, 'to be seen')
.assert(false);
Expand All @@ -1474,11 +1474,11 @@ class WebDriverIO extends Helper {
});
}

return this2.moveCursorTo(withStrictLocator(srcElement)).then(function (res) {
return this2.moveCursorTo(withStrictLocator.call(this, srcElement)).then((res) => {
if (res.state !== 'success') throw new Error(`Unable to move cursor to (${srcElement})`);
return this.buttonDown().then(function (res) {
return this.buttonDown().then((res) => {
if (res.state !== 'success') throw new Error(`Failed to press button down on (${srcElement})`);
return this2.moveCursorTo(withStrictLocator(destElement)).then(function (res) {
return this2.moveCursorTo(withStrictLocator.call(this, destElement)).then(function (res) {
if (res.state !== 'success') throw new Error(`Unable to move cursor to (${destElement})`);
return this.buttonUp();
});
Expand Down Expand Up @@ -1525,8 +1525,8 @@ class WebDriverIO extends Helper {
waitForEnabled(locator, sec = null) {
let client = this.browser;
let aSec = sec || this.options.waitForTimeout;
return client.waitUntil(function () {
return client.elements(withStrictLocator(locator)).then(function (res) {
return client.waitUntil(() => {
return client.elements(withStrictLocator.call(this, locator)).then(function (res) {
if (!res.value || res.value.length === 0) {
return false;
}
Expand All @@ -1551,8 +1551,8 @@ class WebDriverIO extends Helper {
waitForElement(locator, sec = null) {
let client = this.browser;
let aSec = sec || this.options.waitForTimeout;
return client.waitUntil(function () {
return client.elements(withStrictLocator(locator)).then(function (res) {
return client.waitUntil(() => {
return client.elements(withStrictLocator.call(this, locator)).then(function (res) {
if (!res.value || res.value.length === 0) {
return false;
} else return true;
Expand All @@ -1567,8 +1567,8 @@ class WebDriverIO extends Helper {
waitUntilExists(locator, sec = null) {
let client = this.browser;
sec = sec || this.options.waitForTimeout;
return client.waitUntil(function () {
return client.elements(withStrictLocator(locator)).then(function (res) {
return client.waitUntil(() => {
return client.elements(withStrictLocator.call(this, locator)).then(function (res) {
if (!res.value || res.value.length === 0) {
return true;
} else return false;
Expand Down Expand Up @@ -1642,8 +1642,8 @@ class WebDriverIO extends Helper {
let client = this.browser;
let aSec = sec || this.options.waitForTimeout;
let context = aContext || this.root;
return client.waitUntil(function () {
return client.elements(withStrictLocator(context)).then(function (res) {
return client.waitUntil(() => {
return client.elements(withStrictLocator.call(this, context)).then(function (res) {
if (!res.value || res.value.length === 0) {
return false;
}
Expand Down Expand Up @@ -1703,8 +1703,8 @@ class WebDriverIO extends Helper {
waitForVisible(locator, sec = null) {
let client = this.browser;
let aSec = sec || this.options.waitForTimeout;
return client.waitUntil(function () {
return client.elements(withStrictLocator(locator)).then(function (res) {
return client.waitUntil(() => {
return client.elements(withStrictLocator.call(this, locator)).then(function (res) {
if (!res.value || res.value.length === 0) {
return false;
}
Expand Down Expand Up @@ -1732,8 +1732,8 @@ class WebDriverIO extends Helper {
waitNumberOfVisibleElements(locator, num, sec = null) {
let client = this.browser;
let aSec = sec || this.options.waitForTimeout;
return client.waitUntil(function () {
return client.elements(withStrictLocator(locator)).then(function (res) {
return client.waitUntil(() => {
return client.elements(withStrictLocator.call(this, locator)).then(function (res) {
if (!res.value || res.value.length === 0) {
return false;
}
Expand All @@ -1756,8 +1756,8 @@ class WebDriverIO extends Helper {
waitForInvisible(locator, sec = null) {
let client = this.browser;
let aSec = sec || this.options.waitForTimeout;
return client.waitUntil(function () {
return client.elements(withStrictLocator(locator)).then(function (res) {
return client.waitUntil(() => {
return client.elements(withStrictLocator.call(this, locator)).then(function (res) {
if (!res.value || res.value.length === 0) {
return true;
}
Expand Down Expand Up @@ -1791,8 +1791,8 @@ class WebDriverIO extends Helper {
waitForStalenessOf(locator, sec = null) {
let client = this.browser;
let aSec = sec || this.options.waitForTimeout;
return client.waitUntil(function () {
return client.elements(withStrictLocator(locator)).then(function (res) {
return client.waitUntil(() => {
return client.elements(withStrictLocator.call(this, locator)).then(function (res) {
if (!res.value || res.value.length === 0) {
return true;
} else return false;
Expand All @@ -1819,7 +1819,7 @@ class WebDriverIO extends Helper {
} else if (Number.isInteger(locator)) {
return this.browser.frame(locator);
}
return this.browser.element(withStrictLocator(locator)).then((res) => {
return this.browser.element(withStrictLocator.call(this, locator)).then((res) => {
if (!res.value || res.value.length === 0) {
throw new ElementNotFound(locator);
}
Expand Down Expand Up @@ -1958,7 +1958,7 @@ function proceedSee(assertType, text, context, strict = false) {

let smartWaitEnabled = assertType === 'assert';

return this._locate(withStrictLocator(context), smartWaitEnabled).then((res) => {
return this._locate(withStrictLocator.call(this, context), smartWaitEnabled).then((res) => {
if (!res.value || res.value.length === 0) {
throw new ElementNotFound(context);
}
Expand All @@ -1974,7 +1974,7 @@ function proceedSee(assertType, text, context, strict = false) {
}

function findClickable(locator, locateFn) {
if (typeof locator === 'object') return locateFn(withStrictLocator(locator), true);
if (typeof locator === 'object' || locator[0] === '~') return locateFn(withStrictLocator.call(this, locator), true);
if (isCSSorXPathLocator(locator)) return locateFn(locator, true);

let literal = xpathLocator.literal(locator);
Expand Down Expand Up @@ -2019,7 +2019,7 @@ function toStrictLocator(locator) {
}

function findFields(locator) {
if (typeof locator === 'object') return this._locate(withStrictLocator(locator), true);
if (typeof locator === 'object' || locator[0] === '~') return this._locate(withStrictLocator.call(this, locator), true);
if (isCSSorXPathLocator(locator)) return this._locate(locator, true);

let literal = xpathLocator.literal(locator);
Expand Down Expand Up @@ -2100,7 +2100,7 @@ function proceedSeeCheckbox(assertType, field) {
}

function findCheckable(locator, locateFn) {
if (typeof locator === 'object') return locateFn(withStrictLocator(locator), true);
if (typeof locator === 'object' || locator[0] === '~') return locateFn(withStrictLocator.call(this, locator), true);
if (isCSSorXPathLocator(locator)) return locateFn(locator, true);

let literal = xpathLocator.literal(locator);
Expand Down Expand Up @@ -2130,6 +2130,17 @@ function isCSSorXPathLocator(locator) {

function withStrictLocator(locator) {
if (!locator) return null;
if (typeof locator === 'string') {
if (locator[0] === '~') {
if (this.isWeb || this.isWeb === undefined) {
// hook before webdriverio supports native ~ locators in web
return `[aria-label="${locator.slice(1)}"]`;
} else {
return `accessibility id:${locator.slice(1)}`;
}
}
}

if (typeof locator !== 'object') return locator;
let key = Object.keys(locator)[0];
let value = locator[key];
Expand Down