UNPKG

multi-automator

Version:
226 lines (225 loc) 5.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * @desc: iOS Handler * @author: john_chen * @date: 2024.12.30 */ const fs_1 = require("fs"); const WDA_1 = __importDefault(require("./WDA")); const config_1 = require("../config"); const idevice_1 = require("./idevice"); const Element_1 = __importDefault(require("./Element")); class IOSHandler { constructor(uuid, wdaProjPath) { this.id = uuid; this.wdaProjPath = wdaProjPath; this.wda = null; } /** * init iOS handler */ async init() { config_1.logger.info('[iOS.Handler.init]'); this.wda = new WDA_1.default(this.id, this.wdaProjPath); await this.wda.init(); } /** * 关闭 iOS handler */ async close() { if (!this.wda) { throw new Error('WDA not initialized'); } await this.wda.stop(); } /** * home */ async home() { if (!this.wda) { throw new Error('WDA not initialized'); } await this.wda.home(); } /** * 获取应用列表 */ async appList() { return await (0, idevice_1.getAppList)(this.id); } /** * 安装应用 */ async installApp(appPath) { return await (0, idevice_1.installApp)(this.id, appPath); } /** * 卸载应用 */ async uninstallApp(appId) { return await (0, idevice_1.uninstallApp)(this.id, appId); } /** * 启动 APP */ async launchApp(packageName) { if (!this.wda) { throw new Error('WDA not initialized'); } return this.wda.launchApp(packageName); } /** * 终止 APP */ async terminateApp(packageName) { if (!this.wda) { throw new Error('WDA not initialized'); } return this.wda.terminateApp(packageName); } /** * 激活 APP */ async activateApp(packageName) { if (!this.wda) { throw new Error('WDA not initialized'); } return this.wda.activateApp(packageName); } /** * 获取 dom 树 */ async source(timeout = 20000) { if (!this.wda) { throw new Error('WDA not initialized'); } return this.wda.getSource(timeout); } /** * 获取当前设备页面截图 */ async screenshot(path) { if (!this.wda) { throw new Error('WDA not initialized'); } const res = await this.wda.screenshot(); if (path) { (0, fs_1.writeFileSync)(path, res); } return res; } /** * 屏幕点击 */ async tap(x, y) { if (!this.wda) { throw new Error('WDA not initialized'); } return this.wda.tap(x, y); } /** * 长按屏幕 */ async longpress(x, y, duration) { if (!this.wda) { throw new Error('WDA not initialized'); } return this.wda.longpress(x, y, duration / 1000); } /** * 滑动屏幕 */ async swipe(fx, fy, tx, ty, options) { if (!this.wda) { throw new Error('WDA not initialized'); } const { startPressDuration } = options; // 获取屏幕宽高 const screenSize = await this.getScreenSize(); const { width, height } = screenSize; // 处理坐标 if (fx < 0) { fx = 0; } if (fx > width) { fx = width; } if (fy < 0) { fy = 0; } if (fy > height) { fy = height; } if (tx < 0) { tx = 0; } if (tx > width) { tx = width; } if (ty < 0) { ty = 0; } if (ty > height) { ty = height; } return await this.wda.drag(fx, fy, tx, ty, (startPressDuration || 1) / 1000); } /** * 获取屏幕宽高 */ async getScreenSize() { if (!this.wda) { throw new Error('WDA not initialized'); } return this.wda.getScreenSize(); } /** * 获取当前设备屏幕信息 */ async getScreenInfo() { if (!this.wda) { throw new Error('WDA not initialized'); } return this.wda.getScreenInfo(); } /** * 跳转页面 */ async goto(url) { if (!this.wda) { throw new Error('WDA not initialized'); } return this.wda.openUrl(url); } /** * 重新激活当前活动的应用(先home桌面,再打开该应用) */ async deactivateApp() { if (!this.wda) { throw new Error('WDA not initialized'); } return this.wda.deactivateApp(); } /** * 获取元素对象 */ async $x(expression) { if (!this.wda) { throw new Error('WDA not initialized'); } const res = await this.wda.findElements('xpath', expression); let elements = []; for (let element of res) { elements.push(new Element_1.default({ device: this, element, })); } return elements; } } exports.default = IOSHandler;