Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
} else if (opts.toVisible && !swipe) {
params.toVisible = opts.toVisible;
} else {
let msg = swipe
? 'Mobile swipe requires direction'
: 'Mobile scroll supports the following strategies: name, direction, predicateString, and toVisible. Specify one of these';
log.errorAndThrow(msg);
}
// we can also optionally pass a distance which appears to be a ratio of
// screen height, so 1.0 means a full screen's worth of scrolling
if (!swipe && opts.distance) {
params.distance = opts.distance;
}
let element = util.unwrapElement(opts.element);
let endpoint = `/wda/element/${element}/${swipe ? 'swipe' : 'scroll'}`;
return await this.proxyCommand(endpoint, 'POST', params);
};
commands.getElementRect = async function getElementRect (el) {
if (this.isWebContext()) {
// Mobile safari doesn't support rect
const {x, y} = await this.getLocation(el);
const {width, height} = await this.getSize(el);
return {x, y, width, height};
}
el = util.unwrapElement(el);
return await this.getNativeRect(el);
};
async function tapWebElementNatively (driver, atomsElement) {
// try to get the text of the element, which will be accessible in the
// native context
try {
let text = await driver.executeAtom('get_text', [atomsElement]);
if (!text) {
text = await driver.executeAtom('get_attribute_value', [atomsElement, 'value']);
}
if (text) {
const els = await driver.findNativeElementOrElements('accessibility id', text, true);
if (els.length === 1 || els.length === 2) {
const el = els[0];
// use tap because on iOS 11.2 and below `nativeClick` crashes WDA
const rect = await driver.proxyCommand(`/element/${util.unwrapElement(el)}/rect`, 'GET');
if (els.length === 2) {
const el2 = els[1];
const rect2 = await driver.proxyCommand(`/element/${util.unwrapElement(el2)}/rect`, 'GET');
if ((rect.x !== rect2.x || rect.y !== rect2.y) ||
(rect.width !== rect2.width || rect.height !== rect2.height)) {
// These 2 native elements are not referring to the same web element
return false;
}
}
const coords = {
x: Math.round(rect.x + rect.width / 2),
y: Math.round(rect.y + rect.height / 2),
};
await driver.clickCoords(coords);
return true;
logger.info(`Trying to unlock device using pin ${capabilities.unlockKey}`);
await helpers.dismissKeyguard(driver, adb);
let keys = helpers.stringKeyToArr(capabilities.unlockKey);
if (await adb.getApiLevel() >= 21) {
let els = await driver.findElOrEls('id', 'com.android.systemui:id/digit_text', true);
if (_.isEmpty(els)) {
throw new Error('Error finding unlock pin buttons!');
}
let pins = {};
for (let el of els) {
let text = await driver.getAttribute('text', util.unwrapElement(el));
pins[text] = el;
}
for (let pin of keys) {
let el = pins[pin];
await driver.click(util.unwrapElement(el));
}
} else {
for (let pin of keys) {
let el = await driver.findElOrEls('id', `com.android.keyguard:id/key${pin}`, false);
if (el === null) {
throw new Error(`Error finding unlock pin '${pin}' button!`);
}
await driver.click(util.unwrapElement(el));
}
}
// Some devices accept entering the code without pressing the Enter key
// When I rushed commands without this wait before pressKeyCode, rarely UI2 sever crashed
await sleep(UNLOCK_WAIT_TIME);
if (await adb.isScreenLocked()) {
await driver.pressKeyCode(KEYCODE_NUMPAD_ENTER);
await sleep(UNLOCK_WAIT_TIME);
helpers.mobileRotateElement = async function mobileRotateElement (opts = {}) {
if (!opts.element) {
log.errorAndThrow('Element id is expected to be set for rotateElement method');
}
const el = util.unwrapElement(opts.element);
const params = {
rotation: parseFloatParameter('rotation', opts.rotation, 'rotateElement'),
velocity: parseFloatParameter('velocity', opts.velocity, 'rotateElement'),
};
return await this.proxyCommand(`/wda/element/${el}/rotate`, 'POST', params);
};
helpers.mobileTwoFingerTap = async function mobileTwoFingerTap (opts = {}) {
if (!opts.element) {
opts.element = await this.findNativeElementOrElements(`class name`, `XCUIElementTypeApplication`, false);
}
const el = util.unwrapElement(opts.element);
return await this.proxyCommand(`/wda/element/${el}/twoFingerTap`, 'POST');
};
const assertButtonName = async (button, expectedName) => {
button = util.unwrapElement(button);
const name = await this.proxyCommand(`/element/${button}/attribute/name`, 'GET');
if (_.isNil(name) || name.toLowerCase() !== expectedName) {
throw new errors.NoAlertOpenError();
}
};
alert.cancel = async () => {
await this.proxyCommand(`/element/${util.unwrapElement(cancelButton)}/click`, 'POST');
};
alert.ok = async () => {
commands.nativeClick = async function nativeClick (el) {
el = util.unwrapElement(el);
let endpoint = `/element/${el}/click`;
return await this.proxyCommand(endpoint, 'POST', {});
};
commands.getSize = async function getSize (el) {
el = util.unwrapElement(el);
if (this.isWebContext()) {
let atomsElement = this.getAtomsElement(el);
if (atomsElement === null) {
throw new errors.UnknownError(`Error converting element ID for using in WD atoms: '${el}'`);
}
return await this.executeAtom('get_size', [atomsElement]);
}
const rect = await this.getElementRect(el);
return {width: rect.width, height: rect.height};
};