import * as dotenv from 'dotenv'; import * as path from 'path'; dotenv.config({ path: path.resolve(__dirname, '../.env') }); import { createDriver } from '../drivers/factory'; import { findHomeCard } from '../utils/common/device-settings.helper'; import { sleep } from '../utils/common/element.helper'; import * as fs from 'fs'; (async () => { const d = createDriver(); await d.createSession(); await d.goBackToHomepage(); await sleep(800); const name = process.env.AP || 'Air Purifier'; const el = await findHomeCard(d, name); console.log('找到卡:', !!el); if (el) { const r = await d.getElementRect(el); await d.tap(r.x + r.width / 2, r.y + r.height / 2); await sleep(2200); } let s = await d.getSource(); console.log('点卡后(浮层)text:', [...new Set((s.match(/text="[^"]{1,24}"/g) || []))].slice(0, 18).join(' ')); const more = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("More")').catch(() => null); if (more) { await d.tapElement(more); await sleep(2500); console.log('点 More 进功能页'); } else console.log('无 More'); for (let i = 0; i < 3; i++) { const g = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Got it")').catch(() => null); if (!g) break; await d.tapElement(g); await sleep(1000); } await sleep(1000); s = await d.getSource(); const rows = [...s.matchAll(/(?:text|content-desc)="([^"]+)"[^>]*bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"/g)] .map(m => ({ t: m[1], y: +m[3], x: +m[2] })).filter(r => r.t.length >= 1).sort((a, b) => a.y - b.y); console.log('\n功能页文字/desc(按y):'); for (const r of rows.slice(0, 26)) console.log(` y=${String(r.y).padStart(4)} x=${String(r.x).padStart(4)} "${r.t}"`); console.log('\nclickable 元素:'); for (const m of s.matchAll(/<[^>]*class="([^"]*)"[^>]*clickable="true"[^>]*bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"/g)) { console.log(` ${m[1].split('.').pop()} [${m[2]},${m[3]}][${m[4]},${m[5]}]`); } const png = await d.screenshot().catch(() => ''); if (png) { fs.writeFileSync('/tmp/ap_feat.png', Buffer.from(png, 'base64')); console.log('\n截图 /tmp/ap_feat.png'); } await d.destroySession(); })().catch(e => { console.error('ERR', e.message); process.exit(1); });