115 lines
5.3 KiB
TypeScript
115 lines
5.3 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,
|
|
pressButton,
|
|
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') });
|
|
|
|
// 屋檐灯(Permanent Outdoor Lights):灯类 WiFi 产品(relay ch38)。**流程同落地灯(Floor Lamp)**:
|
|
// 长按配对键 2s + Connect Device 直连 + WiFi 配网 + Start now。入口品类名「Permanent Outdoor Lights」。
|
|
// ONES 添加号待补,先 [P0] 占位。
|
|
const CATEGORY = process.env.EAVE_LIGHT_CATEGORY || 'Permanent Outdoor Lights';
|
|
const SCAN_KEYWORD = process.env.EAVE_LIGHT_SCAN || 'Permanent Outdoor Lights';
|
|
const NAME_PATTERN = process.env.EAVE_LIGHT_PATTERN || 'Permanent Outdoor Lights[^"]*\\s*[0-9A-Za-z]{0,4}';
|
|
const ADD_ANCHOR = '[P0]'; // TODO: 待补 屋檐灯 添加 ONES 编号
|
|
|
|
describe('Eave Light Connect - 添加屋檐灯(长按配对 + WiFi配网)', () => {
|
|
let driver: DeviceDriver;
|
|
let reporter: TestReporter;
|
|
setupResilientHooks(() => driver);
|
|
|
|
beforeAll(async () => {
|
|
driver = createDriver();
|
|
await driver.createSession();
|
|
reporter = new TestReporter('EaveLight_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('eaveLight', existing);
|
|
reporter.record(`${ADD_ANCHOR} 添加屋檐灯`, 'SKIP', Date.now() - start, `${existing}已存在`);
|
|
return;
|
|
}
|
|
|
|
const result = await addDeviceViaBLE(driver, {
|
|
categoryName: CATEGORY,
|
|
deviceKeyword: SCAN_KEYWORD,
|
|
registryCategory: 'eaveLight', // 独立 key,供灯控制按注册名找卡片(方案A)
|
|
namePattern: NAME_PATTERN,
|
|
scanTimeout: 30000,
|
|
skipScanStep: true, // Connect Device 直连进配网页,无扫描列表
|
|
// 屋檐灯流程:推进到「发现页」(Discover device/Connect device)→ 长按配对键 2s(relay ch38)→ 点 Connect device → title 含 add
|
|
preSelectSteps: async (d) => {
|
|
// 1. 推进到发现页
|
|
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. 长按配对键(relay ch38)进配对
|
|
await enterPairingMode('eave_light').catch((e) => console.log(`relay ch38 长按配对失败: ${e.message}`));
|
|
await sleep(2500);
|
|
// 3. 点发现页的 Connect device 触发发现
|
|
let clicked = '';
|
|
for (const t of ['Connect device', 'Connect Device', 'Connect Devices', 'Connect', 'Next']) {
|
|
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);
|
|
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(`[屋檐灯诊断] 发现页点击="${clicked}" 已进添加页(含add)=${inAdd}`);
|
|
console.log(`[屋檐灯诊断] 页面文案: ${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);
|
|
|
|
await pressButton('eave_light', 'main', 400).catch((e) => console.log(`关灯按键失败: ${e.message}`)); // 添加后关灯
|
|
await sleep(1000);
|
|
|
|
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;
|
|
}
|
|
});
|
|
});
|