AI_UIAutomation/tests/kata/kata_connect.test.ts

128 lines
6.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,
isDeviceOnHomepage,
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') });
// KATA(入口品类「KATA Friends」):relay ch17+18 **同时长按 3s** 进添加模式;添加流程同 AI Hub
//(Connect Device 直连 → 发现设备 → 配网 → 向导)。设备名/是否需配网待实测,先按 aihub 模板。
const KATA_NAME = process.env.KATA_NAME || 'KATA';
const CATEGORY = process.env.KATA_CATEGORY || 'KATA Friends';
const SCAN_KEYWORD = process.env.KATA_SCAN || KATA_NAME;
const NAME_PATTERN = process.env.KATA_PATTERN || `${KATA_NAME}[^"]*\\s*[0-9A-Za-z]{0,4}`;
const ADD_ANCHOR = '[P0]'; // TODO: 待补 KATA 添加 ONES 编号
describe('KATA Connect - 添加KATA(KATA Friends,ch17+18长按3s)', () => {
let driver: DeviceDriver;
let reporter: TestReporter;
setupResilientHooks(() => driver);
beforeAll(async () => {
driver = createDriver();
await driver.createSession();
reporter = new TestReporter('KATA_Connect', driver.platform.toUpperCase());
});
afterAll(async () => {
reporter.generate();
await driver.destroySession();
});
it(`${ADD_ANCHOR} 通过BLE添加KATA`, { timeout: 360000 }, async () => {
const start = Date.now();
const wifi = getWifiCredentials();
try {
if (await isDeviceOnHomepage(driver, KATA_NAME)) {
console.log(`${KATA_NAME}已在首页,跳过重新添加`);
reporter.record(`${ADD_ANCHOR} 添加KATA`, 'SKIP', Date.now() - start, `${KATA_NAME}已存在`);
return;
}
const result = await addDeviceViaBLE(driver, {
categoryName: CATEGORY, // 入口「KATA Friends」
deviceKeyword: SCAN_KEYWORD,
registryCategory: 'kata',
namePattern: NAME_PATTERN,
scanTimeout: 30000,
skipScanStep: true, // Connect Device 直连,无扫描列表(同 AI Hub)
skipNextButton: true,
wizardButtons: ['Start now', 'Return Home', 'Done', 'Use now', 'Start Using', 'Got it', 'OK', 'Skip', 'Next', 'Save'],
connectionKeywords: [
'Initial Setup', 'Start Using', 'Done', 'added successfully', 'Added successfully',
'Got it', 'Pick a room', 'Select a room', 'Display Type', 'cloud service',
'Wi-Fi', 'WiFi', 'SSID', 'Network', 'Configure Wi-Fi', 'Connecting', 'Start now',
],
// 流程同 AI Hub:选品类(可能 Agree)→ 继电器 ch17+18 同时长按 3s 进配对 → Connect Device → 发现 → 配网
preSelectSteps: async (d: DeviceDriver) => {
const agreeEl = await d.findElementRaw('name', 'Agree').catch(() => null);
if (agreeEl) { await d.clickElement(agreeEl); await sleep(2000); }
// KATA 实测流程:Step1「Power on」页 → **先点 Next** 进 Step2「Connect to the network」→ 再点 Connect Device。
const nextEl = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Next")').catch(() => null);
if (nextEl) { await d.tapElement(nextEl); console.log('KATA: 点击 Next 进 Step2(Connect to the network)'); await sleep(2500); }
// 继电器:同时长按 ch17+18 3s 进添加模式(relay.config 'kata' 已配 holdMs 3000)
await enterPairingMode('kata').catch((e) => console.log(`relay ch17+18 长按失败: ${e.message}`));
console.log('KATA: 已长按 ch17+18 3s,等待设备进配对...');
await sleep(5000);
const tapByLabel = async (labels: string[]): Promise<boolean> => {
for (const t of labels) {
let el: string | null = null;
if (d.platform === 'ios') {
el = await d.findElementRaw('name', t).catch(() => null)
|| await d.findElementRaw('predicate string', `name CONTAINS "${t}" OR label CONTAINS "${t}"`).catch(() => null);
} else {
el = await d.findElementRaw('-android uiautomator', `new UiSelector().text("${t}")`).catch(() => null)
|| await d.findElementRaw('-android uiautomator', `new UiSelector().textContains("${t}")`).catch(() => null);
}
if (el) { console.log(`KATA: 点击 "${t}"`); await d.clickElement(el); return true; }
}
return false;
};
await tapByLabel(['Connect Device', 'Connect device', 'Connect devices', 'Connect Devices']);
await sleep(2000);
// 连接后可能弹隐私协议(Please Note)→ 勾选 + 同意
if (d.platform === 'android') {
const cb = await d.findElementRaw('-android uiautomator', 'new UiSelector().className("android.widget.CheckBox")').catch(() => null);
if (cb) { await d.clickElement(cb).catch(() => {}); await sleep(500); }
}
if (await tapByLabel(['Agree', 'Agree and Continue', 'I Agree', 'Accept', 'Continue', '同意', '同意并继续'])) await sleep(2500);
// 等待发现设备 → 进配网/后续页(上电后约 1 分钟才出现,最长 ~100s)
console.log('KATA: 等待发现设备...');
for (let i = 0; i < 33; i++) {
await sleep(3000);
const s = await d.getSource().catch(() => '');
if (/Configure Wi-?Fi|Wi-?Fi Settings|SSID|Network name|Pick a room|Select a room|Setting up|Verifying|Name your|added successfully|Start now/i.test(s)) {
console.log('KATA: 已进入配网/后续页'); break;
}
}
},
postConnectionSteps: async (d: DeviceDriver) => { await configureHubWifi(d, wifi); },
});
expect(result).toBe(true);
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
reporter.record(`${ADD_ANCHOR} 添加KATA`, 'PASS', Date.now() - start, `${KATA_NAME}添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`);
} catch (e: any) {
const ss = await driver.screenshot().catch(() => '');
reporter.record(`${ADD_ANCHOR} 添加KATA`, 'FAIL', Date.now() - start, e.message, ss);
throw e;
}
});
});