AI_UIAutomation/tests/humidifier/humidifier2_connect.test.ts

102 lines
4.6 KiB
TypeScript

import { describe, it, beforeAll, afterAll, expect } from 'vitest';
import { DeviceDriver } from '../../drivers/types';
import { createDriver } from '../../drivers/factory';
import { TestReporter } from '../../utils/test-reporter';
import {
sleep,
addDeviceViaBLE,
findDeviceNameOnHomepage,
saveAddedDevice,
enterPairingMode,
configureHubWifi,
onesAdd,
setupResilientHooks,
} from '../../utils/common';
import { getWifiCredentials } from '../../config/wifi.config';
import * as dotenv from 'dotenv';
import * as path from 'path';
dotenv.config({ path: path.resolve(__dirname, '../../.env') });
// Evaporative Humidifier(口头"加湿器2"):带 WiFi 模块(relay ch63)。流程不同于加湿器1(纯BLE):
// 实测=**Connect Device 直连式**(非扫描列表):长按配对键(relay ch63)→ 点 Connect Device → Discover device 直连 → WiFi 配网 → Start now。
// 入口品类名 / 首页名 pattern 均 env 可覆盖。
const CATEGORY = process.env.HUMIDIFIER2_CATEGORY || 'Evaporative Humidifier';
const SCAN_KEYWORD = process.env.HUMIDIFIER2_SCAN || 'Evaporative Humidifier';
const NAME_PATTERN = process.env.HUMIDIFIER2_PATTERN || 'Evaporative Humidifier[^"]*\\s*[0-9A-Za-z]{0,4}';
const SKIP_SCAN = process.env.HUMIDIFIER2_SKIP_SCAN !== '0'; // 默认直连(Connect Device);=0 可回退扫描列表
const ADD_ANCHOR = onesAdd('humidifier', 'Evaporative Humidifier'); // ONES add: 74104
describe('Humidifier2 Connect - 添加加湿器2(WiFi模块, 长按配对 + WiFi配网)', () => {
let driver: DeviceDriver;
let reporter: TestReporter;
setupResilientHooks(() => driver);
beforeAll(async () => {
driver = createDriver();
await driver.createSession();
reporter = new TestReporter('Humidifier2_Connect', driver.platform.toUpperCase());
});
afterAll(async () => {
reporter.generate();
await driver.destroySession();
});
it(`${ADD_ANCHOR} 通过BLE+WiFi添加加湿器2`, { timeout: 240000 }, async () => {
const start = Date.now();
const wifi = getWifiCredentials();
try {
const existing = await findDeviceNameOnHomepage(driver, NAME_PATTERN);
if (existing) {
console.log(`${existing}已在首页,跳过重新添加`);
saveAddedDevice('humidifier', existing);
reporter.record(`${ADD_ANCHOR} 添加加湿器2`, 'SKIP', Date.now() - start, `${existing}已存在`);
return;
}
const result = await addDeviceViaBLE(driver, {
categoryName: CATEGORY,
deviceKeyword: SCAN_KEYWORD,
scanTimeout: 30000,
skipScanStep: SKIP_SCAN,
// 选品类后:① 过 Disclaimers/Next(若有)② 长按配对键(relay ch63)进入添加模式 → 点 Next/Connect Device
preSelectSteps: async (d) => {
const tapByLabel = async (labels: string[]): Promise<boolean> => {
for (const t of labels) {
const el = d.platform === 'ios'
? (await d.findElementRaw('name', t).catch(() => null) || await d.findElementRaw('predicate string', `name CONTAINS "${t}" OR label CONTAINS "${t}"`).catch(() => null))
: (await d.findElementRaw('-android uiautomator', `new UiSelector().text("${t}")`).catch(() => null));
if (el) { await d.tapElement(el); return true; }
}
return false;
};
for (let i = 0; i < 2; i++) {
if (!(await tapByLabel(['Next']))) break;
await sleep(2000);
}
await enterPairingMode('humidifier2').catch((e) => console.log(`relay ch63 长按配对失败: ${e.message}`));
await sleep(2000);
if (SKIP_SCAN) {
if (await tapByLabel(['Connect Device', 'Connect device', 'Next'])) await sleep(3000);
}
},
postConnectionSteps: async (d) => { await configureHubWifi(d, wifi); },
wizardButtons: ['Start now', 'Return Home', 'Done', 'Use now', 'Start Using', 'Got it', 'OK', 'Skip', 'Next', 'Save'],
connectionKeywords: [
'Configure Wi-Fi', 'Wi-Fi Settings', 'Connecting', 'Pick a room', 'Done',
'added successfully', 'Added successfully', 'Initial Setup', 'Start now', 'Select a room', 'Display Type',
],
});
expect(result).toBe(true);
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
reporter.record(`${ADD_ANCHOR} 添加加湿器2`, 'PASS', Date.now() - start, `添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`);
} catch (e: any) {
const ss = await driver.screenshot().catch(() => '');
reporter.record(`${ADD_ANCHOR} 添加加湿器2`, 'FAIL', Date.now() - start, e.message, ss);
throw e;
}
});
});