243 lines
8.7 KiB
TypeScript
243 lines
8.7 KiB
TypeScript
import { describe, it, beforeAll, afterAll, beforeEach, expect } from 'vitest';
|
|
import { DeviceDriver } from '../../drivers/types';
|
|
import { createDriver } from '../../drivers/factory';
|
|
import { TestReporter } from '../../utils/test-reporter';
|
|
import {
|
|
sleep,
|
|
scrollToAndTap,
|
|
waitForSource,
|
|
} 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 = getDeviceName('waterDetector', 'WATER_DETECTOR_DEVICE');
|
|
|
|
describe('Water Detector Control - 功能页操作', () => {
|
|
let driver: DeviceDriver;
|
|
let reporter: TestReporter;
|
|
|
|
beforeAll(async () => {
|
|
driver = createDriver();
|
|
await driver.createSession();
|
|
reporter = new TestReporter('WaterDetector_Control', driver.platform.toUpperCase());
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await driver.dismissPopupIfPresent();
|
|
await driver.goBackToHomepage();
|
|
await driver.dismissPopupIfPresent();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
reporter.generate();
|
|
await driver.destroySession();
|
|
});
|
|
|
|
async function enterControlPage(): Promise<void> {
|
|
await driver.goBackToHomepage().catch(() => {});
|
|
await sleep(800);
|
|
const { height: winH } = await driver.getWindowSize().catch(() => ({ height: 844 }));
|
|
// 滚到找到(原只滚1次 + 'name' 精确匹配;实测设备在第4屏 → 够不到)。CONTAINS + 屏内校验 + 滚动找。
|
|
for (let r = 0; r < 14; r++) {
|
|
const el = driver.platform === 'ios'
|
|
? await driver.findElementRaw('predicate string', `type == "XCUIElementTypeCell" AND name CONTAINS "${deviceName}"`).catch(() => null)
|
|
: await driver.findElementRaw('-android uiautomator', `new UiSelector().textContains("${deviceName}")`).catch(() => null);
|
|
if (el) {
|
|
const rect = await driver.getElementRect(el).catch(() => null);
|
|
if (rect && rect.width > 0 && rect.y > winH * 0.06 && rect.y + rect.height < winH * 0.92) {
|
|
await driver.tap(Math.round(rect.x + rect.width / 2), Math.round(rect.y + rect.height / 2));
|
|
await sleep(3000); return;
|
|
}
|
|
}
|
|
await driver.scrollDown(500); await sleep(800);
|
|
}
|
|
throw new Error(`找不到${deviceName}卡片`);
|
|
}
|
|
|
|
it('切换检测模式', async () => {
|
|
const start = Date.now();
|
|
try {
|
|
await enterControlPage();
|
|
|
|
const source = await driver.getSource();
|
|
// Find detection mode options
|
|
const hasMode = source.includes('Detection Mode') || source.includes('Mode') ||
|
|
source.includes('Water Leak') || source.includes('Drip');
|
|
|
|
// Tap mode option to switch
|
|
const modeEl = await driver.findElementRaw('name', 'Detection Mode') ||
|
|
await driver.findElementRaw('name', 'Mode');
|
|
if (modeEl) {
|
|
await driver.tapElement(modeEl);
|
|
await sleep(2000);
|
|
} else {
|
|
const tapped = await scrollToAndTap(driver, 'Detection Mode') ||
|
|
await scrollToAndTap(driver, 'Mode');
|
|
expect(tapped).toBe(true);
|
|
await sleep(2000);
|
|
}
|
|
|
|
// Select a different mode option
|
|
const newSource = await driver.getSource();
|
|
const modeOption = await driver.findElementRaw('name', 'Water Leak') ||
|
|
await driver.findElementRaw('name', 'Drip Detection') ||
|
|
await driver.findElementRaw('name', 'Flood');
|
|
if (modeOption) {
|
|
await driver.tapElement(modeOption);
|
|
await sleep(2000);
|
|
}
|
|
|
|
const detail = `检测模式页面加载=${hasMode}, 模式切换操作完成`;
|
|
console.log(detail);
|
|
expect(hasMode).toBe(true);
|
|
|
|
reporter.record('切换检测模式', 'PASS', Date.now() - start, detail);
|
|
} catch (e: any) {
|
|
const ss = await driver.screenshot().catch(() => '');
|
|
reporter.record('切换检测模式', 'FAIL', Date.now() - start, e.message, ss);
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it('查看历史日志', async () => {
|
|
const start = Date.now();
|
|
try {
|
|
await enterControlPage();
|
|
|
|
const tapped = await scrollToAndTap(driver, 'Logs');
|
|
if (!tapped) {
|
|
const altTapped = await scrollToAndTap(driver, 'History') ||
|
|
await scrollToAndTap(driver, 'Log');
|
|
expect(altTapped).toBe(true);
|
|
}
|
|
await waitForSource(driver, 'Logs', 5000);
|
|
|
|
const source = await driver.getSource();
|
|
const hasLogEntries = source.includes('Logs') || source.includes('History') ||
|
|
source.includes('Water Leak') || source.includes('Normal') ||
|
|
source.includes('No more data') || source.includes('No logs');
|
|
|
|
const detail = `Logs页面加载成功, 有日志记录=${hasLogEntries}`;
|
|
console.log(detail);
|
|
expect(hasLogEntries).toBe(true);
|
|
|
|
reporter.record('查看历史日志', 'PASS', Date.now() - start, detail);
|
|
} catch (e: any) {
|
|
const ss = await driver.screenshot().catch(() => '');
|
|
reporter.record('查看历史日志', 'FAIL', Date.now() - start, e.message, ss);
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it('设置报警持续时间', async () => {
|
|
const start = Date.now();
|
|
try {
|
|
await enterControlPage();
|
|
|
|
const tapped = await scrollToAndTap(driver, 'Alert Duration') ||
|
|
await scrollToAndTap(driver, 'Alarm Duration') ||
|
|
await scrollToAndTap(driver, 'Duration');
|
|
expect(tapped).toBe(true);
|
|
await sleep(2000);
|
|
|
|
const source = await driver.getSource();
|
|
const hasDurationPage = source.includes('Duration') || source.includes('seconds') ||
|
|
source.includes('min') || source.includes('Alert');
|
|
|
|
// Select an option
|
|
const option = await driver.findElementRaw('name', '30 seconds') ||
|
|
await driver.findElementRaw('name', '1 min') ||
|
|
await driver.findElementRaw('name', '60 seconds');
|
|
if (option) {
|
|
await driver.tapElement(option);
|
|
await sleep(2000);
|
|
}
|
|
|
|
const detail = `Alert Duration页面=${hasDurationPage}`;
|
|
console.log(detail);
|
|
expect(hasDurationPage).toBe(true);
|
|
|
|
reporter.record('设置报警持续时间', 'PASS', Date.now() - start, detail);
|
|
} catch (e: any) {
|
|
const ss = await driver.screenshot().catch(() => '');
|
|
reporter.record('设置报警持续时间', 'FAIL', Date.now() - start, e.message, ss);
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it('设置报警间隔', async () => {
|
|
const start = Date.now();
|
|
try {
|
|
await enterControlPage();
|
|
|
|
const tapped = await scrollToAndTap(driver, 'Alert Interval') ||
|
|
await scrollToAndTap(driver, 'Alarm Interval') ||
|
|
await scrollToAndTap(driver, 'Interval');
|
|
expect(tapped).toBe(true);
|
|
await sleep(2000);
|
|
|
|
const source = await driver.getSource();
|
|
const hasIntervalPage = source.includes('Interval') || source.includes('minutes') ||
|
|
source.includes('min') || source.includes('Alert');
|
|
|
|
// Select an option
|
|
const option = await driver.findElementRaw('name', '5 min') ||
|
|
await driver.findElementRaw('name', '10 min') ||
|
|
await driver.findElementRaw('name', '5 minutes');
|
|
if (option) {
|
|
await driver.tapElement(option);
|
|
await sleep(2000);
|
|
}
|
|
|
|
const detail = `Alert Interval页面=${hasIntervalPage}`;
|
|
console.log(detail);
|
|
expect(hasIntervalPage).toBe(true);
|
|
|
|
reporter.record('设置报警间隔', 'PASS', Date.now() - start, detail);
|
|
} catch (e: any) {
|
|
const ss = await driver.screenshot().catch(() => '');
|
|
reporter.record('设置报警间隔', 'FAIL', Date.now() - start, e.message, ss);
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it('切换报警音量', async () => {
|
|
const start = Date.now();
|
|
try {
|
|
await enterControlPage();
|
|
|
|
const tapped = await scrollToAndTap(driver, 'Volume') ||
|
|
await scrollToAndTap(driver, 'Alarm Volume') ||
|
|
await scrollToAndTap(driver, 'Alert Volume');
|
|
expect(tapped).toBe(true);
|
|
await sleep(2000);
|
|
|
|
const source = await driver.getSource();
|
|
const hasVolumePage = source.includes('Volume') || source.includes('Low') ||
|
|
source.includes('Medium') || source.includes('High') || source.includes('Mute');
|
|
|
|
// Select a different volume level
|
|
const option = await driver.findElementRaw('name', 'Low') ||
|
|
await driver.findElementRaw('name', 'Medium') ||
|
|
await driver.findElementRaw('name', 'High');
|
|
if (option) {
|
|
await driver.tapElement(option);
|
|
await sleep(2000);
|
|
}
|
|
|
|
const detail = `Volume页面=${hasVolumePage}`;
|
|
console.log(detail);
|
|
expect(hasVolumePage).toBe(true);
|
|
|
|
reporter.record('切换报警音量', 'PASS', Date.now() - start, detail);
|
|
} catch (e: any) {
|
|
const ss = await driver.screenshot().catch(() => '');
|
|
reporter.record('切换报警音量', 'FAIL', Date.now() - start, e.message, ss);
|
|
throw e;
|
|
}
|
|
});
|
|
});
|