UNPKG

appium-uiautomator2-driver

Version:
148 lines 5.29 kB
import { PROTOCOLS } from 'appium/driver.js'; /** * Gets the currently active element. * @returns The currently active element. */ export 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. */ export 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. */ export 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. */ export 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. */ export 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. */ export 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). */ export 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). */ export 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. */ export 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. */ export 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. */ export 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. */ export 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. */ export 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. */ export 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). */ export 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 === 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. */ export 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