113 lines
5.4 KiB
TypeScript
113 lines
5.4 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,
|
|
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') });
|
|
|
|
// URC(Universal Remote 万能遥控器):带 WiFi 模块。入口品类「Universal Remote」。
|
|
// 配对:同时长按 off(ch21)+on(ch22)键(relay 'urc' 的 pairing)。
|
|
// 流程先按 WiFi 直连(Connect Device)起手:选品类 → 过 Next/Disclaimers → 两键同按配对 → Connect Device → WiFi 配网 → 收尾。
|
|
// 首版按截图实测后再校准(扫描列表? 配网页?)。ONES 添加号 190801。
|
|
const CATEGORY = process.env.URC_CATEGORY || 'Universal Remote';
|
|
const SCAN_KEYWORD = process.env.URC_SCAN || 'Universal Remote';
|
|
const NAME_PATTERN = process.env.URC_PATTERN || 'Universal Remote[^"]*\\s*[0-9A-Za-z]{0,4}';
|
|
const SKIP_SCAN = process.env.URC_SKIP_SCAN !== '0'; // 默认直连;=0 回退扫描列表
|
|
const ADD_ANCHOR = '[P0][ONES:190801]'; // 添加URC验证
|
|
|
|
describe('URC Connect - 添加万能遥控器(两键同按配对 + WiFi配网)', () => {
|
|
let driver: DeviceDriver;
|
|
let reporter: TestReporter;
|
|
setupResilientHooks(() => driver);
|
|
|
|
beforeAll(async () => {
|
|
driver = createDriver();
|
|
await driver.createSession();
|
|
reporter = new TestReporter('URC_Connect', driver.platform.toUpperCase());
|
|
});
|
|
|
|
afterAll(async () => {
|
|
reporter.generate();
|
|
await driver.destroySession();
|
|
});
|
|
|
|
it(`${ADD_ANCHOR} 通过BLE+WiFi添加万能遥控器`, { timeout: 240000 }, async () => {
|
|
const start = Date.now();
|
|
const wifi = getWifiCredentials();
|
|
try {
|
|
const existing = await findDeviceNameOnHomepage(driver, NAME_PATTERN);
|
|
if (existing) {
|
|
console.log(`${existing}已在首页,跳过重新添加`);
|
|
saveAddedDevice('urc', existing);
|
|
reporter.record(`${ADD_ANCHOR} 添加万能遥控器`, 'SKIP', Date.now() - start, `${existing}已存在`);
|
|
return;
|
|
}
|
|
|
|
const result = await addDeviceViaBLE(driver, {
|
|
categoryName: CATEGORY,
|
|
deviceKeyword: SCAN_KEYWORD,
|
|
scanTimeout: 15000,
|
|
skipScanStep: SKIP_SCAN,
|
|
// URC 流程:选品类 → 推进到「发现页」(含 Discover device/Connect device)→ **先按继电器(ch21+22)进配对**
|
|
// → **再点发现页的 Connect device 按钮**触发发现 → title 含 "add" 即进入添加模式页 → WiFi 配网。
|
|
preSelectSteps: async (d) => {
|
|
// 1. 推进到发现页(点 Next/Connect 直到出现 Discover device/Connect device)
|
|
for (let i = 0; i < 4; i++) {
|
|
const src = await d.getSource();
|
|
if (/Discover device|Connect device/i.test(src)) break;
|
|
let moved = false;
|
|
for (const t of ['Next', 'Connect']) {
|
|
const b = await d.findElementRaw('-android uiautomator', `new UiSelector().text("${t}")`).catch(() => null);
|
|
if (b) { await d.tapElement(b); moved = true; break; }
|
|
}
|
|
if (!moved) break;
|
|
await sleep(2000);
|
|
}
|
|
// 2. 先按继电器进配对模式(就在发现页上按,避免过早退出配对)
|
|
await enterPairingMode('urc').catch((e) => console.log(`relay ch21+22 两键同按配对失败: ${e.message}`));
|
|
await sleep(2500);
|
|
// 3. 再点发现页的 Connect device 按钮触发发现
|
|
let clicked = '';
|
|
for (const t of ['Connect device', 'Connect Device', 'Connect Devices', 'Connect']) {
|
|
const b = await d.findElementRaw('-android uiautomator', `new UiSelector().text("${t}")`).catch(() => null);
|
|
if (b) { await d.tapElement(b); clicked = t; break; }
|
|
}
|
|
await sleep(3000);
|
|
// 诊断:确认是否进添加模式页(title 含 add)
|
|
const src = await d.getSource();
|
|
const inAdd = /add/i.test(src);
|
|
const btns = [...new Set(Array.from(src.matchAll(/text="([^"]+)"/g)).map((m) => m[1]).filter((t) => t && t.length < 24))].slice(0, 25);
|
|
console.log(`[URC诊断] 发现页点击="${clicked}" 已进添加页(含add)=${inAdd}`);
|
|
console.log(`[URC诊断] 页面文案: ${JSON.stringify(btns)}`);
|
|
},
|
|
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',
|
|
],
|
|
});
|
|
expect(result).toBe(true);
|
|
|
|
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
|
|
reporter.record(`${ADD_ANCHOR} 添加万能遥控器`, 'PASS', Date.now() - start, `添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`);
|
|
} catch (e: any) {
|
|
const ss = await driver.screenshot().catch(() => '');
|
|
reporter.record(`${ADD_ANCHOR} 添加万能遥控器`, 'FAIL', Date.now() - start, e.message, ss);
|
|
throw e;
|
|
}
|
|
});
|
|
});
|