appium-uiautomator2-driver
Version:
UiAutomator2 integration for Appium
191 lines • 6.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.active = active;
exports.getAttribute = getAttribute;
exports.elementDisplayed = elementDisplayed;
exports.elementEnabled = elementEnabled;
exports.elementSelected = elementSelected;
exports.getName = getName;
exports.getLocation = getLocation;
exports.getSize = getSize;
exports.doSetElementValue = doSetElementValue;
exports.setValueImmediate = setValueImmediate;
exports.getText = getText;
exports.click = click;
exports.getElementScreenshot = getElementScreenshot;
exports.clear = clear;
exports.getElementRect = getElementRect;
exports.mobileReplaceElementValue = mobileReplaceElementValue;
const bluebird_1 = __importDefault(require("bluebird"));
const lodash_1 = __importDefault(require("lodash"));
const driver_1 = require("appium/driver");
/**
* @this {AndroidUiautomator2Driver}
* @returns {Promise<import('@appium/types').Element>}
*/
async function active() {
return /** @type {import('@appium/types').Element} */ (await this.uiautomator2.jwproxy.command('/element/active', 'GET'));
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} attribute
* @param {string} elementId
* @returns {Promise<string?>}
*/
async function getAttribute(attribute, elementId) {
return String(await this.uiautomator2.jwproxy.command(`/element/${elementId}/attribute/${attribute}`, 'GET', {}));
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} elementId
* @returns {Promise<boolean>}
*/
async function elementDisplayed(elementId) {
return toBool(await this.getAttribute('displayed', elementId));
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} elementId
* @returns {Promise<boolean>}
*/
async function elementEnabled(elementId) {
return toBool(await this.getAttribute('enabled', elementId));
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} elementId
* @returns {Promise<boolean>}
*/
async function elementSelected(elementId) {
return toBool(await this.getAttribute('selected', elementId));
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} elementId
* @returns {Promise<string>}
*/
async function getName(elementId) {
return /** @type {string} */ (await this.uiautomator2.jwproxy.command(`/element/${elementId}/name`, 'GET', {}));
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} elementId
* @returns {Promise<import('@appium/types').Position>}
*/
async function getLocation(elementId) {
return /** @type {import('@appium/types').Position} */ (await this.uiautomator2.jwproxy.command(`/element/${elementId}/location`, 'GET', {}));
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} elementId
* @returns {Promise<import('@appium/types').Size>}
*/
async function getSize(elementId) {
return /** @type {import('@appium/types').Size} */ (await this.uiautomator2.jwproxy.command(`/element/${elementId}/size`, 'GET', {}));
}
/**
* @this {AndroidUiautomator2Driver}
* @param {import('appium-android-driver').DoSetElementValueOpts} params
* @returns {Promise<void>}
*/
async function doSetElementValue(params) {
await this.uiautomator2.jwproxy.command(`/element/${params.elementId}/value`, 'POST', params);
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string|string[]} keys
* @param {string} elementId
* @returns {Promise<void>}
*/
async function setValueImmediate(keys, elementId) {
await this.uiautomator2.jwproxy.command(`/element/${elementId}/value`, 'POST', {
elementId,
text: lodash_1.default.isArray(keys) ? keys.join('') : keys,
replace: false,
});
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} elementId
* @returns {Promise<string>}
*/
async function getText(elementId) {
return String(await this.uiautomator2.jwproxy.command(`/element/${elementId}/text`, 'GET', {}));
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} element
* @returns {Promise<void>}
*/
async function click(element) {
await this.uiautomator2.jwproxy.command(`/element/${element}/click`, 'POST', { element });
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} element
* @returns {Promise<string>}
*/
async function getElementScreenshot(element) {
return String(await this.uiautomator2.jwproxy.command(`/element/${element}/screenshot`, 'GET', {}));
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} elementId
* @returns {Promise<void>}
*/
async function clear(elementId) {
await this.uiautomator2.jwproxy.command(`/element/${elementId}/clear`, 'POST', {
elementId,
});
}
/**
* @this {AndroidUiautomator2Driver}
* @param {string} elementId
* @returns {Promise<import('@appium/types').Rect>}
*/
async function getElementRect(elementId) {
const chromedriver = /** @type {import('appium-chromedriver').default} */ (this.chromedriver);
if (this.isWebContext()) {
this.log.debug(`Detected downstream chromedriver protocol: ${chromedriver.jwproxy.downstreamProtocol}`);
if (chromedriver.jwproxy.downstreamProtocol === driver_1.PROTOCOLS.MJSONWP) {
const [{ x, y }, { width, height }] =
/** @type {[import('@appium/types').Position, import('@appium/types').Size]} */ (await bluebird_1.default.all([
chromedriver.jwproxy.command(`/element/${elementId}/location`, 'GET'),
chromedriver.jwproxy.command(`/element/${elementId}/size`, 'GET'),
]));
return { x, y, width, height };
}
return /** @type {import('@appium/types').Rect} */ (await chromedriver.jwproxy.command(`/element/${elementId}/rect`, 'GET'));
}
return /** @type {import('@appium/types').Rect} */ (await this.uiautomator2.jwproxy.command(`/element/${elementId}/rect`, 'GET'));
}
/**
* Sends text to the given element by replacing its previous content
* @this {AndroidUiautomator2Driver}
* @param {string} elementId The id of the element whose content will be replaced.
* @param {string} text The actual text to set.
* @throws {Error} If there was a faulre while setting the text
* @returns {Promise<void>}
*/
async function mobileReplaceElementValue(elementId, text) {
await this.uiautomator2.jwproxy.command(`/element/${elementId}/value`, 'POST', {
text,
replace: true,
});
}
// #region Internal Helpers
/**
* @param {any} s
* @returns {boolean}
*/
function toBool(s) {
return lodash_1.default.isString(s) ? s.toLowerCase() === 'true' : !!s;
}
// #endregion
/**
* @typedef {import('../driver').AndroidUiautomator2Driver} AndroidUiautomator2Driver
*/
//# sourceMappingURL=element.js.map