AI_UIAutomation/tests/find_card/find_card_connect.test.ts

88 lines
4.0 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 {
addDeviceViaBLE,
findDeviceNameOnHomepage,
saveAddedDevice,
enterPairingMode,
sleep,
} from '../../utils/common';
import { getDeviceName } from '../../config/device.config';
import * as dotenv from 'dotenv';
import * as path from 'path';
dotenv.config({ path: path.resolve(__dirname, '../../.env') });
const deviceName = process.env.FIND_CARD_DEVICE || 'Wallet Finder Card'; // 实际名形如 "Wallet Finder Card 1B"
// 首页实际名匹配
const FIND_CARD_PATTERN = process.env.FIND_CARD_PATTERN || 'Wallet Finder Card\\s+[0-9A-Za-z]{1,2}';
// Wallet Finder Card 非 ONES 必测项,无 P0 锚点
const ADD_ANCHOR = '[ONES:NA] ';
describe('Find Card Connect - 添加Wallet Finder Card设备', () => {
let driver: DeviceDriver;
let reporter: TestReporter;
beforeAll(async () => {
driver = createDriver();
await driver.createSession();
reporter = new TestReporter('FindCard_Connect', driver.platform.toUpperCase());
});
afterAll(async () => {
reporter.generate();
await driver.destroySession();
});
it(`${ADD_ANCHOR}通过BLE添加Wallet Finder Card`, { timeout: 180000 }, async () => {
const start = Date.now();
try {
const existing = await findDeviceNameOnHomepage(driver, FIND_CARD_PATTERN);
if (existing) {
console.log(`${existing}已在首页,跳过重新添加`);
saveAddedDevice('findCard', existing);
reporter.record(`${ADD_ANCHOR}添加Wallet Finder Card`, 'SKIP', Date.now() - start, `${existing}已存在`);
return;
}
const result = await addDeviceViaBLE(driver, {
categoryName: 'Wallet Finder Card',
deviceKeyword: deviceName,
skipScanStep: true, // 无扫描列表:Connect Device + 按键后直连命名页
// 选品类后(引导页):① 按 ch30 长按 2s(card beep 进配对)② 点 "Connect Device" 直连
// ③ **iOS 专属**:连接后弹"添加到 Find My App?"→ 点 "Not Yet" 跳过(不加入 Apple Find My,否则流程卡住 connection timeout)
preSelectSteps: async (d) => {
await enterPairingMode('find_card').catch((e) => console.log(`relay ch30 按键失败: ${e.message}`));
await sleep(800);
const btn = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Connect Device")');
if (btn) { await d.tapElement(btn); await sleep(3000); }
else console.log('未找到 Connect Device 按钮');
// iOS:Wallet Finder Card(类 AirTag)连接后弹 Apple "Add to Find My App?"——必须点 "Not Yet" 跳过,
// 否则停在该弹窗、SwitchBot 连接流程不推进 → connection timeout。轮询等其出现(连接需若干秒)。
if (d.platform === 'ios') {
for (let k = 0; k < 24; k++) {
const ny = await d.findElementRaw('predicate string', 'label CONTAINS "Not Yet" OR name CONTAINS "Not Yet"').catch(() => null);
if (ny) { await d.tapElement(ny); console.log('iOS: 点 "Not Yet" 跳过添加到 Find My App'); await sleep(1500); break; }
await sleep(1500);
}
}
},
wizardButtons: ['Next', 'Save', 'Use now', 'Start Using', 'Done', 'Got it', 'OK', 'Skip'],
connectionKeywords: [
'Pick a room', 'Name', 'Save', 'added successfully', 'Added successfully', 'Got it', 'Done',
],
});
expect(result).toBe(true);
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
reporter.record(`${ADD_ANCHOR}添加Wallet Finder Card`, 'PASS', Date.now() - start, `${deviceName}添加成功, 耗时${elapsed}s`);
} catch (e: any) {
const ss = await driver.screenshot().catch(() => '');
reporter.record(`${ADD_ANCHOR}添加Wallet Finder Card`, 'FAIL', Date.now() - start, e.message, ss);
throw e;
}
});
});