101 lines
4.7 KiB
TypeScript
101 lines
4.7 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,
|
|
addDeviceWithSerialPairing,
|
|
findDeviceNameOnHomepage,
|
|
saveAddedDevice,
|
|
configureHubWifi,
|
|
} 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') });
|
|
|
|
// Relay Switch 1:WiFi 产品(relay ch48),添加目录里在 "Power & Switch" 分组、与 Plug Mini 同组。
|
|
// **流程同 Plug Mini**:选品类 "Relay Switch" → Before Using/Next → 长按配对键(ch48)→ 连接 → WiFi 配网。
|
|
// 入口品类名实测为 "Relay Switch"(首字母大写)。配对时长待实测(先 4s)。ONES 添加号待补,先 [P0]。
|
|
const CATEGORY = process.env.RELAY_SWITCH_CATEGORY || 'Relay Switch';
|
|
const deviceName = process.env.RELAY_SWITCH_DEVICE || 'Relay Switch 1';
|
|
const NAME_PATTERN = process.env.RELAY_SWITCH_PATTERN || 'Relay Switch 1\\s*[0-9A-Za-z]{0,4}';
|
|
const ADD_ANCHOR = '[P0]'; // TODO: 待补 Relay Switch 添加 ONES 编号
|
|
|
|
describe('RelaySwitch Connect - 添加Relay Switch(串口配对 + WiFi配网)', () => {
|
|
let driver: DeviceDriver;
|
|
let reporter: TestReporter;
|
|
|
|
beforeAll(async () => {
|
|
driver = createDriver();
|
|
await driver.createSession();
|
|
reporter = new TestReporter('RelaySwitch_Connect', driver.platform.toUpperCase());
|
|
});
|
|
|
|
afterAll(async () => {
|
|
reporter.generate();
|
|
await driver.destroySession();
|
|
});
|
|
|
|
it(`${ADD_ANCHOR} 通过BLE+WiFi添加Relay Switch`, { timeout: 360000 }, async () => {
|
|
const start = Date.now();
|
|
const wifi = getWifiCredentials();
|
|
try {
|
|
const existing = await findDeviceNameOnHomepage(driver, NAME_PATTERN);
|
|
if (existing) {
|
|
console.log(`${existing}已在首页,跳过重新添加`);
|
|
saveAddedDevice('relay_switch', existing);
|
|
reporter.record(`${ADD_ANCHOR} 添加Relay Switch`, 'SKIP', Date.now() - start, `${existing}已存在`);
|
|
return;
|
|
}
|
|
|
|
const result = await addDeviceWithSerialPairing(driver, {
|
|
categoryName: CATEGORY,
|
|
categoryScrollHint: 0,
|
|
deviceKeyword: deviceName,
|
|
relayDevice: 'relay_switch', // ch48,长按(config/relay.config,时长待实测)
|
|
registryCategory: 'relay_switch',
|
|
namePattern: NAME_PATTERN,
|
|
repressOnRetry: false,
|
|
pairRetries: 2,
|
|
pairPollIters: 16,
|
|
// 选 "Relay Switch" 后:① 二级选型号 "Relay Switch 1" ② 接线设置引导页(AC/DC等)点 "Skip this guide" 跳过
|
|
// → 到连接页(按键2s + Connect Device,由 helper 配对+tapNext 处理)
|
|
beforePairing: async (d) => {
|
|
const sub = await d.findElementRaw('-android uiautomator', 'new UiSelector().textMatches("(?i)Relay Switch 1")').catch(() => null);
|
|
if (sub) { await d.tapElement(sub); await sleep(2000); }
|
|
for (let i = 0; i < 3; i++) {
|
|
const skip = await d.findElementRaw('-android uiautomator', 'new UiSelector().textContains("Skip this guide")').catch(() => null);
|
|
if (!skip) break;
|
|
await d.tapElement(skip); await sleep(2000);
|
|
}
|
|
},
|
|
// 连接后流程:① "Select Device" 列表页选中发现的设备 → Next ② Connecting → Configure Wi-Fi 配网
|
|
postConnectSteps: async (d) => {
|
|
const src = await d.getSource();
|
|
if (/Select Device/i.test(src)) {
|
|
const dev = await d.findElementRaw('-android uiautomator', 'new UiSelector().textMatches("(?i)Relay Switch 1.*")').catch(() => null);
|
|
if (dev) { await d.tapElement(dev); await sleep(1500); }
|
|
const n = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Next")').catch(() => null);
|
|
if (n) { await d.tapElement(n); await sleep(3500); } // 等 Connecting → 配网页
|
|
}
|
|
await configureHubWifi(d, wifi);
|
|
},
|
|
connectionKeywords: [
|
|
'Select Device', 'Configure Wi-Fi', 'Wi-Fi Settings', 'Connecting', 'Pick a room', 'Done',
|
|
'added successfully', 'Added successfully', 'Initial Setup',
|
|
],
|
|
});
|
|
expect(result).toBe(true);
|
|
|
|
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
|
|
reporter.record(`${ADD_ANCHOR} 添加Relay Switch`, 'PASS', Date.now() - start, `添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`);
|
|
} catch (e: any) {
|
|
const ss = await driver.screenshot().catch(() => '');
|
|
reporter.record(`${ADD_ANCHOR} 添加Relay Switch`, 'FAIL', Date.now() - start, e.message, ss);
|
|
throw e;
|
|
}
|
|
});
|
|
});
|