AI_UIAutomation/tests/ceiling_light/floor_lamp_connect.test.ts

101 lines
4.5 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') });
// 落地灯:灯类 WiFi 产品(relay ch42)。入口品类「RGBWW Floor Lamp」。长按配对键 2s + Connect Device 直连 + WiFi 配网。
const CATEGORY = process.env.FLOOR_LAMP_CATEGORY || 'RGBWW Floor Lamp';
const SCAN_KEYWORD = process.env.FLOOR_LAMP_SCAN || 'RGBWW Floor Lamp'; // skipScanStep=true → 仅首页校验,须用可区分全名(勿用 'Floor Lamp',会与 RGBICWW Floor Lamp 互串)
const NAME_PATTERN = process.env.FLOOR_LAMP_PATTERN || 'RGBWW Floor Lamp[^"]*\\s*[0-9A-Za-z]{0,4}';
describe('Floor Lamp Connect - 添加落地灯(长按配对 + WiFi配网)', () => {
let driver: DeviceDriver;
let reporter: TestReporter;
setupResilientHooks(() => driver);
beforeAll(async () => {
driver = createDriver();
await driver.createSession();
reporter = new TestReporter('FloorLamp_Connect', driver.platform.toUpperCase());
});
afterAll(async () => {
reporter.generate();
await driver.destroySession();
});
it('[P0][ONES:301586] 通过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('floorLamp', existing);
reporter.record('[P0][ONES:301586] 添加落地灯', 'SKIP', Date.now() - start, `${existing}已存在`);
return;
}
const result = await addDeviceViaBLE(driver, {
categoryName: CATEGORY,
deviceKeyword: SCAN_KEYWORD,
registryCategory: 'floorLamp', // 独立 key,灯控制按注册名找卡片(方案A)
namePattern: NAME_PATTERN,
scanTimeout: 30000,
skipScanStep: true, // Connect Device 直连进配网页,无扫描列表
// 选品类后:① 过 Disclaimers(若有)② 长按配对键 2s(relay ch42)→ 点 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('floor_lamp').catch((e) => console.log(`relay ch42 长按配对失败: ${e.message}`));
await sleep(2000);
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',
],
});
expect(result).toBe(true);
await pressButton('floor_lamp', 'main', 400).catch((e) => console.log(`关灯按键失败: ${e.message}`)); // 添加后关灯
await sleep(1000);
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
reporter.record('[P0][ONES:301586] 添加落地灯', 'PASS', Date.now() - start, `添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`);
} catch (e: any) {
const ss = await driver.screenshot().catch(() => '');
reporter.record('[P0][ONES:301586] 添加落地灯', 'FAIL', Date.now() - start, e.message, ss);
throw e;
}
});
});