appium-uiautomator2-driver
Version:
UiAutomator2 integration for Appium
166 lines • 5.86 kB
JavaScript
;
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 driver_1 = require("appium/driver");
/**
* Gets the currently active element.
* @returns The currently active element.
*/
async function active() {
return (await this.uiautomator2.jwproxy.command('/element/active', 'GET'));
}
/**
* Gets an element attribute value.
* @param attribute - Name of the attribute to retrieve.
* @param elementId - ID of the element.
* @returns The attribute value as a string.
*/
async function getAttribute(attribute, elementId) {
return String(await this.uiautomator2.jwproxy.command(`/element/${elementId}/attribute/${attribute}`, 'GET', {}));
}
/**
* Returns whether the element is displayed.
* @param elementId - ID of the element.
* @returns True if the element is displayed, false otherwise.
*/
async function elementDisplayed(elementId) {
return toBool(await this.getAttribute('displayed', elementId));
}
/**
* Returns whether the element is enabled.
* @param elementId - ID of the element.
* @returns True if the element is enabled, false otherwise.
*/
async function elementEnabled(elementId) {
return toBool(await this.getAttribute('enabled', elementId));
}
/**
* Returns whether the element is selected.
* @param elementId - ID of the element.
* @returns True if the element is selected, false otherwise.
*/
async function elementSelected(elementId) {
return toBool(await this.getAttribute('selected', elementId));
}
/**
* Gets the element tag name.
* @param elementId - ID of the element.
* @returns The element tag name.
*/
async function getName(elementId) {
return (await this.uiautomator2.jwproxy.command(`/element/${elementId}/name`, 'GET', {}));
}
/**
* Gets the element location.
* @param elementId - ID of the element.
* @returns The element position coordinates (x, y).
*/
async function getLocation(elementId) {
return (await this.uiautomator2.jwproxy.command(`/element/${elementId}/location`, 'GET', {}));
}
/**
* Gets the element size.
* @param elementId - ID of the element.
* @returns The element size (width, height).
*/
async function getSize(elementId) {
return (await this.uiautomator2.jwproxy.command(`/element/${elementId}/size`, 'GET', {}));
}
/**
* Sets the value of an element using the upstream driver API.
* @param params - Options containing the element ID and value to set.
*/
async function doSetElementValue(params) {
await this.uiautomator2.jwproxy.command(`/element/${params.elementId}/value`, 'POST', params);
}
/**
* Sends text to an element without replacement.
* @param keys - Text to send, either as a string or array of strings (which will be joined).
* @param elementId - ID of the element.
*/
async function setValueImmediate(keys, elementId) {
await this.uiautomator2.jwproxy.command(`/element/${elementId}/value`, 'POST', {
elementId,
text: Array.isArray(keys) ? keys.join('') : keys,
replace: false,
});
}
/**
* Gets the element text.
* @param elementId - ID of the element.
* @returns The element text content.
*/
async function getText(elementId) {
return String(await this.uiautomator2.jwproxy.command(`/element/${elementId}/text`, 'GET', {}));
}
/**
* Clicks the given element.
* @param element - ID of the element to click.
*/
async function click(element) {
await this.uiautomator2.jwproxy.command(`/element/${element}/click`, 'POST', { element });
}
/**
* Takes a screenshot of the element.
* @param element - ID of the element.
* @returns Base64-encoded PNG screenshot of the element.
*/
async function getElementScreenshot(element) {
return String(await this.uiautomator2.jwproxy.command(`/element/${element}/screenshot`, 'GET', {}));
}
/**
* Clears the element text.
* @param elementId - ID of the element to clear.
*/
async function clear(elementId) {
await this.uiautomator2.jwproxy.command(`/element/${elementId}/clear`, 'POST', { elementId });
}
/**
* Gets the element rectangle.
* @param elementId - ID of the element.
* @returns The element rectangle (x, y, width, height).
*/
async function getElementRect(elementId) {
if (!this.isWebContext()) {
return (await this.uiautomator2.jwproxy.command(`/element/${elementId}/rect`, 'GET'));
}
const chromedriver = this.chromedriver;
if (chromedriver.jwproxy.downstreamProtocol === driver_1.PROTOCOLS.MJSONWP) {
const [{ x, y }, { width, height }] = (await Promise.all([
chromedriver.jwproxy.command(`/element/${elementId}/location`, 'GET'),
chromedriver.jwproxy.command(`/element/${elementId}/size`, 'GET'),
]));
return { x, y, width, height };
}
return (await chromedriver.jwproxy.command(`/element/${elementId}/rect`, 'GET'));
}
/**
* Replaces the element text.
* @param elementId - ID of the element.
* @param text - Text to replace the current element value with.
*/
async function mobileReplaceElementValue(elementId, text) {
await this.uiautomator2.jwproxy.command(`/element/${elementId}/value`, 'POST', {
text,
replace: true,
});
}
function toBool(value) {
return typeof value === 'string' ? value.toLowerCase() === 'true' : !!value;
}
//# sourceMappingURL=element.js.map