AI_UIAutomation/utils/bot-helper.ts

97 lines
3.1 KiB
TypeScript

import { DeviceDriver } from '../drivers/types';
import { BOT_LOCATORS } from '../locators/bot-locators';
import { getDeviceName } from '../config/device.config';
const botDeviceName = getDeviceName('bot', 'BOT_DEVICE');
export class BotHelper {
private driver: DeviceDriver;
constructor(driver: DeviceDriver) {
this.driver = driver;
}
async findAndTapBotCard(): Promise<boolean> {
let botId = await this.driver.findDeviceCard(botDeviceName);
if (!botId) {
await this.driver.scrollDown(250);
await this.sleep(1500);
botId = await this.driver.findDeviceCard(botDeviceName);
}
if (!botId) return false;
const rect = await this.driver.getElementRect(botId);
if (rect.y < 0 || rect.y > 800) {
await this.driver.scrollDown(150);
await this.sleep(1000);
botId = await this.driver.findDeviceCard(botDeviceName);
if (!botId) return false;
}
await this.driver.tapElement(botId);
return true;
}
async tapSettingsInPopup(): Promise<boolean> {
await this.sleep(1200);
return this.driver.tapByLocator(BOT_LOCATORS.settingsButton);
}
async enterBotSettings(): Promise<boolean> {
const tapped = await this.findAndTapBotCard();
if (!tapped) return false;
return await this.tapSettingsInPopup();
}
async tapSettingsItem(locator: typeof BOT_LOCATORS[string]): Promise<boolean> {
return this.driver.tapByLocator(locator);
}
async tapOnInPopup(): Promise<boolean> {
await this.sleep(1200);
return this.driver.tapByLocator(BOT_LOCATORS.onButton);
}
async tapOffInPopup(): Promise<boolean> {
await this.sleep(1200);
return this.driver.tapByLocator(BOT_LOCATORS.offButton);
}
async goBackToHomepage(): Promise<boolean> {
return this.driver.goBackToHomepage();
}
async getBotStatus(): Promise<'on' | 'off' | 'unknown'> {
if (this.driver.platform === 'ios') {
const onElems = await this.driver.findElementsRaw('predicate string', `name CONTAINS "${botDeviceName}" AND name CONTAINS "On"`);
if (onElems.length > 0) return 'on';
const offElems = await this.driver.findElementsRaw('predicate string', `name CONTAINS "${botDeviceName}" AND name CONTAINS "Off"`);
if (offElems.length > 0) return 'off';
} else {
const source = await this.driver.getSource();
if (source.includes(botDeviceName) && source.includes('"On"')) return 'on';
if (source.includes(botDeviceName) && source.includes('"Off"')) return 'off';
}
return 'unknown';
}
async getBotMode(): Promise<string> {
const source = await this.driver.getSource();
if (source.includes('Press Mode')) return 'press';
if (source.includes('Switch Mode')) return 'switch';
return 'unknown';
}
async isOnSettingsPage(): Promise<boolean> {
const source = await this.driver.getSource();
if (this.driver.platform === 'ios') {
return source.includes('NavigationBar') && source.includes('name="Settings"');
}
return source.includes('Settings');
}
async sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}