@flowspec-qa/runner-core 0.2.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/api-client.d.ts +87 -10
  2. package/dist/api-client.d.ts.map +1 -1
  3. package/dist/api-client.js +108 -7
  4. package/dist/api-client.js.map +1 -1
  5. package/dist/auth.d.ts.map +1 -1
  6. package/dist/auth.js +4 -3
  7. package/dist/auth.js.map +1 -1
  8. package/dist/config.d.ts +1 -0
  9. package/dist/config.d.ts.map +1 -1
  10. package/dist/config.js +4 -1
  11. package/dist/config.js.map +1 -1
  12. package/dist/discovery-report.d.ts +13 -0
  13. package/dist/discovery-report.d.ts.map +1 -0
  14. package/dist/discovery-report.js +977 -0
  15. package/dist/discovery-report.js.map +1 -0
  16. package/dist/discovery.d.ts +213 -3
  17. package/dist/discovery.d.ts.map +1 -1
  18. package/dist/discovery.js +2125 -264
  19. package/dist/discovery.js.map +1 -1
  20. package/dist/evidence-corpus.d.ts +338 -0
  21. package/dist/evidence-corpus.d.ts.map +1 -0
  22. package/dist/evidence-corpus.js +927 -0
  23. package/dist/evidence-corpus.js.map +1 -0
  24. package/dist/evidence-types.d.ts +230 -0
  25. package/dist/evidence-types.d.ts.map +1 -0
  26. package/dist/evidence-types.js +24 -0
  27. package/dist/evidence-types.js.map +1 -0
  28. package/dist/executor.d.ts +1 -0
  29. package/dist/executor.d.ts.map +1 -1
  30. package/dist/executor.js +93 -195
  31. package/dist/executor.js.map +1 -1
  32. package/dist/index.d.ts +5 -1
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +3 -0
  35. package/dist/index.js.map +1 -1
  36. package/dist/suite-manager.d.ts +6 -0
  37. package/dist/suite-manager.d.ts.map +1 -1
  38. package/dist/suite-manager.js +55 -2
  39. package/dist/suite-manager.js.map +1 -1
  40. package/package.json +4 -1
  41. package/src/api-client.ts +0 -170
  42. package/src/auth.ts +0 -45
  43. package/src/backup-manager.ts +0 -63
  44. package/src/config.ts +0 -52
  45. package/src/devices.ts +0 -196
  46. package/src/discovery.ts +0 -563
  47. package/src/executor.ts +0 -681
  48. package/src/index.ts +0 -11
  49. package/src/result-sanitizer.ts +0 -60
  50. package/src/suite-manager.ts +0 -73
  51. package/src/uploader.ts +0 -74
  52. package/tsconfig.json +0 -8
package/src/discovery.ts DELETED
@@ -1,563 +0,0 @@
1
- import { execSync, spawnSync } from 'child_process';
2
- import fs from 'fs';
3
- import os from 'os';
4
- import path from 'path';
5
- import chalk from 'chalk';
6
- import { FlowSpecQAApi } from './api-client.js';
7
-
8
- const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
9
-
10
- // ─── ADB path resolution (mirrors devices.ts) ───────────────────────────────
11
- const ADB_COMMON_PATHS = [
12
- 'adb',
13
- '/opt/homebrew/share/android-commandlinetools/platform-tools/adb',
14
- `${process.env.HOME}/Library/Android/sdk/platform-tools/adb`,
15
- '/usr/local/share/android-commandlinetools/platform-tools/adb',
16
- '/usr/local/bin/adb',
17
- `${process.env.ANDROID_HOME}/platform-tools/adb`,
18
- `${process.env.ANDROID_SDK_ROOT}/platform-tools/adb`,
19
- ];
20
-
21
- function resolveAdb(): string {
22
- for (const candidate of ADB_COMMON_PATHS) {
23
- if (!candidate) continue;
24
- try {
25
- execSync(`"${candidate}" version`, { stdio: 'ignore', timeout: 2000 });
26
- return candidate;
27
- } catch {}
28
- }
29
- throw new Error('adb not found. Install Android SDK platform-tools and add to PATH.');
30
- }
31
-
32
- export interface DiscoveryOptions {
33
- url: string;
34
- framework: 'playwright' | 'maestro';
35
- projectId?: string;
36
- entityId?: string;
37
- outputDir?: string;
38
- username?: string;
39
- password?: string;
40
- /** Android device serial (from `flowspecqa devices`). Required when multiple Android devices are connected. */
41
- deviceId?: string;
42
- }
43
-
44
- export interface DiscoveryResult {
45
- planContent: string;
46
- rawScanData: any;
47
- }
48
-
49
- /**
50
- * Autonomous Discovery Engine 2.0
51
- * Uses Playwright and Maestro to extract real UI intelligence.
52
- */
53
- export const discoveryEngine = {
54
- async run(options: DiscoveryOptions): Promise<DiscoveryResult> {
55
- console.log(chalk.blue(`\n🔍 Starting autonomous high-fidelity discovery for: ${options.url}`));
56
-
57
- let result: DiscoveryResult;
58
-
59
- if (options.framework === 'maestro') {
60
- result = await this.discoverMobile(options);
61
- } else {
62
- result = await this.discoverWeb(options);
63
- }
64
-
65
- // Persist locally
66
- const outputDir = options.outputDir || path.join(process.cwd(), 'specs');
67
- if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
68
-
69
- const planPath = path.join(outputDir, 'discovery_plan.md');
70
- fs.writeFileSync(planPath, result.planContent);
71
- console.log(chalk.green(`\n✅ Local discovery plan saved: ${chalk.bold(planPath)}`));
72
-
73
- // Sync with SaaS
74
- try {
75
- console.log(chalk.yellow('📡 Syncing rich intelligence with FlowSpecQA SaaS...'));
76
- await FlowSpecQAApi.syncDiscoveryPlan({
77
- url: options.url,
78
- plan_content: result.planContent,
79
- raw_scan_data: result.rawScanData,
80
- framework: options.framework,
81
- project_id: options.projectId,
82
- entity_id: options.entityId,
83
- });
84
- console.log(chalk.green('✅ Sync successful! The AI generator is now "UI-Aware".'));
85
- } catch (error: any) {
86
- const errorMsg = error.response?.data?.detail || error.message || String(error);
87
- console.log(chalk.red(`⚠️ SaaS Sync skipped: ${errorMsg}`));
88
- console.log(chalk.gray(` Run: flowspecqa generate --from ${planPath}\n`));
89
- }
90
-
91
- return result;
92
- },
93
-
94
- /** Capture maestro hierarchy from current screen and return parsed JSON */
95
- async _captureHierarchy(env?: NodeJS.ProcessEnv): Promise<any> {
96
- const rawOutput = execSync('maestro hierarchy', {
97
- encoding: 'utf8',
98
- stdio: ['ignore', 'pipe', 'pipe'],
99
- env: env ?? process.env,
100
- });
101
- const lines = rawOutput.split('\n');
102
- const jsonStart = lines.findIndex(l => l.trim().startsWith('{'));
103
- const jsonStr = jsonStart >= 0 ? lines.slice(jsonStart).join('\n') : rawOutput;
104
- return JSON.parse(jsonStr);
105
- },
106
-
107
- /** Execute a Maestro YAML flow (written to a temp file) */
108
- _runMaestroFlow(appId: string, steps: string[], env?: NodeJS.ProcessEnv): boolean {
109
- const yaml = `appId: ${appId}\n---\n${steps.join('\n')}\n`;
110
- const tmpFile = path.join(os.tmpdir(), `discovery_flow_${Date.now()}.yaml`);
111
- fs.writeFileSync(tmpFile, yaml);
112
- try {
113
- const result = spawnSync('maestro', ['test', tmpFile], {
114
- encoding: 'utf8',
115
- timeout: 120000,
116
- stdio: ['ignore', 'pipe', 'pipe'],
117
- env: env ?? process.env,
118
- });
119
- fs.unlinkSync(tmpFile);
120
- return result.status === 0;
121
- } catch {
122
- try { fs.unlinkSync(tmpFile); } catch {}
123
- return false;
124
- }
125
- },
126
-
127
- async discoverMobile(options: DiscoveryOptions): Promise<DiscoveryResult> {
128
- const appId = options.url;
129
- const username = options.username || '';
130
- const password = options.password || '';
131
- const adb = resolveAdb();
132
- // -s flag for device targeting when serial is provided
133
- const adbS = options.deviceId ? `"${adb}" -s ${options.deviceId}` : `"${adb}"`;
134
- // Maestro device env var
135
- const maestroEnv = options.deviceId
136
- ? { ...process.env, MAESTRO_DEVICE_ID: options.deviceId }
137
- : process.env;
138
-
139
- console.log(chalk.gray(`📱 Starting multi-screen autonomous discovery${options.deviceId ? ` on device ${options.deviceId}` : ''}...`));
140
-
141
- const allScreens: { name: string; elements: any[]; hierarchy: any }[] = [];
142
-
143
- try {
144
- // ── Reset app state ───────────────────────────────────────────────
145
- console.log(chalk.gray(' 🔄 Resetting app state...'));
146
- try { execSync(`${adbS} shell am force-stop ${appId}`, { stdio: 'ignore' }); } catch {}
147
- try { execSync(`${adbS} shell pm clear ${appId}`, { stdio: 'ignore' }); } catch {}
148
- await sleep(1000);
149
-
150
- // ── Screen 1: Launch & capture Login ─────────────────────────────
151
- console.log(chalk.gray(' 📸 Screen 1/3 — Login...'));
152
- execSync(`${adbS} shell monkey -p ${appId} -c android.intent.category.LAUNCHER 1`, { stdio: 'ignore' });
153
- await sleep(2500);
154
-
155
- const loginHierarchy = await this._captureHierarchy(maestroEnv);
156
- const loginElements = this.extractMaestroSelectors(loginHierarchy);
157
- allScreens.push({ name: 'Login', elements: loginElements, hierarchy: loginHierarchy });
158
- console.log(chalk.green(` ✅ Login: ${loginElements.length} elements`));
159
-
160
- // ── Error state: Login — submit empty to reveal tv_error ─────────
161
- console.log(chalk.gray(' 📸 Screen 1 error state — empty submit...'));
162
- const emptyLoginOk = this._runMaestroFlow(appId, [
163
- `- tapOn:\n id: "btn_login"`,
164
- `- waitForAnimationToEnd:\n timeout: 1500`,
165
- ], maestroEnv);
166
- if (emptyLoginOk) {
167
- const loginErrorHierarchy = await this._captureHierarchy(maestroEnv);
168
- const loginErrorAll = this.extractMaestroSelectors(loginErrorHierarchy);
169
- const knownLoginIds = new Set(loginElements.map(e => e.id));
170
- const loginErrorNew = loginErrorAll.filter(e => !knownLoginIds.has(e.id));
171
- if (loginErrorNew.length > 0) {
172
- allScreens.push({ name: 'Login (Error State)', elements: loginErrorNew, hierarchy: loginErrorHierarchy });
173
- console.log(chalk.green(` ✅ Login error state: ${loginErrorNew.length} new elements (e.g. ${loginErrorNew.map(e => e.id).join(', ')})`));
174
- }
175
- }
176
-
177
- // ── Navigate to Screen 2: Accounts (via login) ───────────────────
178
- if (username && password) {
179
- console.log(chalk.gray(' 🔐 Logging in to discover authenticated screens...'));
180
-
181
- // Run login steps — no success assertion here (extendedWaitUntil is flaky).
182
- // We verify login success afterward by inspecting the hierarchy.
183
- this._runMaestroFlow(appId, [
184
- // clearState must be a property of launchApp, not a standalone command
185
- `- launchApp:\n clearState: true`,
186
- `- waitForAnimationToEnd`,
187
- `- tapOn:\n id: "et_username"`,
188
- `- eraseText: 50`,
189
- `- inputText: "${username}"`,
190
- `- waitForAnimationToEnd`,
191
- // Dismiss keyboard so et_password is not covered on smaller screens
192
- `- hideKeyboard`,
193
- `- tapOn:\n id: "et_password"`,
194
- `- eraseText: 30`,
195
- `- inputText: "${password}"`,
196
- `- waitForAnimationToEnd`,
197
- // Dismiss keyboard so btn_login is not covered
198
- `- hideKeyboard`,
199
- `- tapOn:\n id: "btn_login"`,
200
- `- waitForAnimationToEnd`,
201
- ], maestroEnv);
202
-
203
- // Give the app extra time to complete the login network request & navigate
204
- await sleep(3000);
205
-
206
- // Verify login success by checking what screen we're on
207
- let postLoginHierarchy: any;
208
- try { postLoginHierarchy = await this._captureHierarchy(maestroEnv); } catch { postLoginHierarchy = null; }
209
- const postLoginElements = postLoginHierarchy ? this.extractMaestroSelectors(postLoginHierarchy) : [];
210
- const loginSuccess = postLoginElements.some(e => e.id === 'tv_welcome' || e.id === 'tv_total_balance');
211
-
212
- if (loginSuccess) {
213
- console.log(chalk.gray(' 📸 Screen 2/3 — Accounts...'));
214
- await sleep(1500);
215
- const accountsHierarchy = await this._captureHierarchy(maestroEnv);
216
- const accountsElements = this.extractMaestroSelectors(accountsHierarchy);
217
- allScreens.push({ name: 'Accounts', elements: accountsElements, hierarchy: accountsHierarchy });
218
- console.log(chalk.green(` ✅ Accounts: ${accountsElements.length} elements`));
219
-
220
- // ── Navigate to Screen 3: Transfer ─────────────────────────
221
- console.log(chalk.gray(' 📸 Screen 3/3 — Transfer...'));
222
- const navSuccess = this._runMaestroFlow(appId, [
223
- `- tapOn:\n id: "btn_transfer"`,
224
- `- waitForAnimationToEnd`,
225
- // No text assertion — Transfer screen title is unknown until discovery runs
226
- ], maestroEnv);
227
-
228
- if (navSuccess) {
229
- await sleep(1200);
230
- const transferHierarchy = await this._captureHierarchy(maestroEnv);
231
- const transferElements = this.extractMaestroSelectors(transferHierarchy);
232
- allScreens.push({ name: 'Transfer', elements: transferElements, hierarchy: transferHierarchy });
233
- console.log(chalk.green(` ✅ Transfer: ${transferElements.length} elements`));
234
-
235
- // ── Error state: Transfer — submit empty amount ─────────
236
- console.log(chalk.gray(' 📸 Transfer error state — empty amount submit...'));
237
- const emptyTransferOk = this._runMaestroFlow(appId, [
238
- `- tapOn:\n id: "btn_confirm"`,
239
- `- waitForAnimationToEnd:\n timeout: 1500`,
240
- ], maestroEnv);
241
- if (emptyTransferOk) {
242
- const transferErrorHierarchy = await this._captureHierarchy(maestroEnv);
243
- const transferErrorAll = this.extractMaestroSelectors(transferErrorHierarchy);
244
- const knownTransferIds = new Set(transferElements.map(e => e.id));
245
- const transferErrorNew = transferErrorAll.filter(e => !knownTransferIds.has(e.id));
246
- if (transferErrorNew.length > 0) {
247
- allScreens.push({ name: 'Transfer (Error State)', elements: transferErrorNew, hierarchy: transferErrorHierarchy });
248
- console.log(chalk.green(` ✅ Transfer error state: ${transferErrorNew.length} new elements (e.g. ${transferErrorNew.map(e => e.id).join(', ')})`));
249
- }
250
- }
251
- } else {
252
- console.log(chalk.yellow(' ⚠️ Transfer screen not reachable — skipping'));
253
- }
254
- } else {
255
- console.log(chalk.yellow(' ⚠️ Login failed — only Login screen captured'));
256
- }
257
- }
258
-
259
- // ── Build comprehensive discovery plan ────────────────────────────
260
- const totalElements = allScreens.reduce((sum, s) => sum + s.elements.length, 0);
261
-
262
- const screenSections = allScreens.map(screen => `
263
- ### Screen: ${screen.name}
264
- | Element Type | Text/Content | testID / Identifier |
265
- |--------------|--------------|---------------------|
266
- ${screen.elements.map(e => `| ${e.type} | ${e.text} | \`${e.id}\` |`).join('\n') || '| — | No interactive elements found | — |'}
267
- `).join('\n');
268
-
269
- const planContent = `# Discovery Plan: Mobile App (${appId})
270
- Generated: ${new Date().toISOString()}
271
-
272
- ## App Overview
273
- - **App ID**: ${appId}
274
- - **Screens discovered**: ${allScreens.length}
275
- - **Total interactive elements**: ${totalElements}
276
- - **Framework**: Maestro (Android native)
277
- - **Device**: Physical device (ADB)
278
-
279
- ## Screen Intelligence
280
- ${screenSections}
281
-
282
- ## Suggested Test Flows
283
-
284
- ### Flow 1 — Login con credenciales válidas
285
- 1. Ingresar email en \`et_username\`
286
- 2. Ingresar contraseña en \`et_password\`
287
- 3. Tap \`btn_login\`
288
- 4. Verificar pantalla de cuentas visible
289
-
290
- ### Flow 2 — Login con credenciales inválidas
291
- 1. Ingresar email incorrecto
292
- 2. Ingresar contraseña incorrecta
293
- 3. Tap \`btn_login\`
294
- 4. Verificar mensaje de error visible
295
-
296
- ### Flow 3 — Consulta de saldo
297
- 1. Login exitoso
298
- 2. Verificar \`tv_total_balance\` visible
299
- 3. Verificar \`card_checking\` y \`card_savings\` presentes
300
-
301
- ### Flow 4 — Transferencia exitosa
302
- 1. Login → tap \`btn_transfer\`
303
- 2. Ingresar monto en \`et_amount\`
304
- 3. Ingresar destinatario en \`et_recipient\`
305
- 4. Tap \`btn_confirm\`
306
- 5. Verificar mensaje de éxito
307
-
308
- ### Flow 5 — Transferencia con monto inválido
309
- 1. Login → tap \`btn_transfer\`
310
- 2. Dejar monto vacío
311
- 3. Tap \`btn_confirm\`
312
- 4. Verificar mensaje de error
313
-
314
- ### Flow 6 — Logout
315
- 1. Login exitoso
316
- 2. Tap \`btn_logout\`
317
- 3. Verificar redirección a Login screen
318
-
319
- ## UI Health
320
- - Total interactive nodes detected: ${totalElements}
321
- - Screens mapped: ${allScreens.map(s => s.name).join(', ')}
322
- - Screen structure complexity: ${totalElements > 15 ? 'High' : totalElements > 8 ? 'Medium' : 'Low'}
323
- `;
324
-
325
- return {
326
- planContent,
327
- rawScanData: {
328
- screens: allScreens.map(s => ({ name: s.name, elements: s.elements })),
329
- total: totalElements,
330
- appId,
331
- framework: 'maestro',
332
- },
333
- };
334
-
335
- } catch (error: any) {
336
- console.log(chalk.red(` ❌ Discovery error: ${error.message}`));
337
- return {
338
- planContent: `# Discovery Plan: Mobile App (${appId})\nStatus: FAIL\nError: ${error.message}\n`,
339
- rawScanData: { error: error.message },
340
- };
341
- }
342
- },
343
-
344
- async handleLogin(page: any, username: string, password: string): Promise<boolean> {
345
- try {
346
- const passwordField = page.locator('input[type="password"]').first();
347
- const count = await passwordField.count();
348
- if (count === 0) return false;
349
-
350
- console.log(chalk.gray('🔐 Login form detected — filling credentials...'));
351
-
352
- const usernameField = page.locator(
353
- 'input[type="email"], input[name="email"], input[name="username"], input[name="user"], ' +
354
- 'input[placeholder*="correo" i], input[placeholder*="email" i], input[placeholder*="usuario" i]'
355
- ).first();
356
-
357
- await usernameField.fill(username);
358
- await passwordField.fill(password);
359
-
360
- const submitBtn = page.locator(
361
- 'button[type="submit"], input[type="submit"], ' +
362
- 'button:has-text("Ingresar"), button:has-text("Login"), ' +
363
- 'button:has-text("Sign in"), button:has-text("Entrar"), button:has-text("Iniciar")'
364
- ).first();
365
-
366
- await submitBtn.click();
367
- await page.waitForLoadState('networkidle', { timeout: 10000 }).catch(() => {});
368
- await page.waitForTimeout(600);
369
-
370
- console.log(chalk.green('✅ Login successful — continuing discovery on authenticated page...'));
371
- return true;
372
- } catch (err: any) {
373
- console.log(chalk.yellow(`⚠️ Login attempt failed: ${err.message}`));
374
- return false;
375
- }
376
- },
377
-
378
- async discoverWeb(options: DiscoveryOptions): Promise<DiscoveryResult> {
379
- const { chromium } = await import('playwright');
380
- console.log(chalk.gray('🌐 Launching Autonomous Playwright Crawler...'));
381
-
382
- const browser = await chromium.launch({ headless: true });
383
- const context = await browser.newContext({
384
- viewport: { width: 1440, height: 900 },
385
- });
386
- const page = await context.newPage();
387
-
388
- try {
389
- await page.goto(options.url, { waitUntil: 'networkidle' });
390
-
391
- // ── Handle login if credentials provided ────────────────────────
392
- if (options.username && options.password) {
393
- await this.handleLogin(page, options.username, options.password);
394
- }
395
-
396
- const title = await page.title();
397
-
398
- // ── Phase 1: Extract DOM elements ────────────────────────────────
399
- const elements: any[] = await page.evaluate(() => {
400
- // @ts-ignore — runs in browser context
401
- const interactive = Array.from(document.querySelectorAll(
402
- 'button, a, input, select, textarea, [role="button"], [role="link"], [role="menuitem"]'
403
- ));
404
- return interactive.map(el => {
405
- const e = el as any;
406
- const rect = e.getBoundingClientRect();
407
- return {
408
- tag: e.tagName.toLowerCase(),
409
- text: e.textContent?.trim().substring(0, 50) || '',
410
- id: e.id || '',
411
- name: e.name || '',
412
- placeholder: e.placeholder || '',
413
- href: e.href || '',
414
- type: e.type || '',
415
- ariaLabel: e.getAttribute('aria-label') || '',
416
- testId: e.getAttribute('data-testid') || e.getAttribute('test-id') || '',
417
- visible: rect.width > 0 && rect.height > 0,
418
- top: Math.round(rect.top),
419
- };
420
- }).filter((e: any) => e.visible);
421
- });
422
-
423
- // ── Phase 2: Take screenshots (viewport + scrolled sections) ─────
424
- console.log(chalk.gray('📸 Capturing page screenshots for Vision AI analysis...'));
425
- const screenshots: string[] = [];
426
-
427
- // Full-page screenshot
428
- const fullPageBuffer = await page.screenshot({ fullPage: true, type: 'png' });
429
- screenshots.push(fullPageBuffer.toString('base64'));
430
-
431
- // Viewport sections (scroll through page)
432
- // @ts-ignore — runs in browser context
433
- const pageHeight: number = await page.evaluate(() => document.body.scrollHeight);
434
- const viewportHeight = 900;
435
- const sections = Math.min(Math.ceil(pageHeight / viewportHeight), 4);
436
-
437
- for (let i = 1; i < sections; i++) {
438
- // @ts-ignore — runs in browser context
439
- await page.evaluate((scrollY: number) => window.scrollTo(0, scrollY), i * viewportHeight);
440
- await page.waitForTimeout(400);
441
- const sectionBuffer = await page.screenshot({ type: 'png' });
442
- screenshots.push(sectionBuffer.toString('base64'));
443
- }
444
-
445
- // @ts-ignore — runs in browser context
446
- await page.evaluate(() => window.scrollTo(0, 0));
447
-
448
- await browser.close();
449
- console.log(chalk.gray(` Captured ${screenshots.length} screenshots, ${elements.length} DOM elements`));
450
-
451
- // ── Phase 3: Vision AI Analysis (if project/entity provided) ─────
452
- let planContent: string;
453
-
454
- if (options.projectId && options.entityId) {
455
- try {
456
- console.log(chalk.gray('🧠 Sending to Vision AI for semantic analysis...'));
457
- const visionResult = await FlowSpecQAApi.analyzeVision({
458
- url: options.url,
459
- entity_id: options.entityId,
460
- project_id: options.projectId,
461
- screenshots,
462
- dom_elements: elements,
463
- page_title: title,
464
- });
465
- planContent = visionResult.plan_content;
466
- console.log(chalk.gray(
467
- ` Vision analysis: ${visionResult.analysis_chars} chars, ` +
468
- `${visionResult.screenshots_analyzed} screenshots processed`
469
- ));
470
- } catch (visionErr: any) {
471
- console.log(chalk.yellow(`⚠️ Vision AI unavailable (${visionErr.message}), falling back to DOM plan...`));
472
- planContent = this._buildDomPlan(title, options.url, elements);
473
- }
474
- } else {
475
- // No project/entity → DOM plan only (local use)
476
- planContent = this._buildDomPlan(title, options.url, elements);
477
- }
478
-
479
- return {
480
- planContent,
481
- rawScanData: { elements, url: options.url, framework: 'playwright', total: elements.length },
482
- };
483
-
484
- } catch (error) {
485
- await browser.close();
486
- throw error;
487
- }
488
- },
489
-
490
- /** Fallback DOM-based plan (no Vision AI) */
491
- _buildDomPlan(title: string, url: string, elements: any[]): string {
492
- return `# Discovery Plan: Web App (${title})
493
- URL: ${url}
494
- Generated: ${new Date().toISOString()}
495
-
496
- ## Intelligence Summary
497
- Found **${elements.length}** interactive elements on the landing page.
498
-
499
- ### Primary UI Map
500
- | Tag | Description / Text | Suggested Selector | testID |
501
- |-----|--------------------|-------------------|--------|
502
- ${elements.slice(0, 20).map((e: any) => {
503
- let selector = e.tag;
504
- if (e.testId) selector = `[data-testid="${e.testId}"]`;
505
- else if (e.id) selector = `#${e.id}`;
506
- else if (e.name) selector = `[name="${e.name}"]`;
507
- else if (e.text) selector = `${e.tag}:has-text("${e.text}")`;
508
- else if (e.placeholder) selector = `${e.tag}[placeholder="${e.placeholder}"]`;
509
- return `| ${e.tag} | ${e.text || e.placeholder || e.ariaLabel || '-'} | \`${selector}\` | ${e.testId || '-'} |`;
510
- }).join('\n')}
511
-
512
- ## Structure
513
- - Detected ${elements.filter((e: any) => e.tag === 'button').length} buttons.
514
- - Detected ${elements.filter((e: any) => e.tag === 'a').length} links.
515
- - Detected ${elements.filter((e: any) => e.tag === 'input').length} inputs.
516
- `;
517
- },
518
-
519
- extractMaestroSelectors(node: any): any[] {
520
- const elements: any[] = [];
521
- const INTERACTIVE_CLASSES = [
522
- 'android.widget.Button',
523
- 'android.widget.EditText',
524
- 'android.widget.CheckBox',
525
- 'android.widget.RadioButton',
526
- 'android.widget.Switch',
527
- 'android.widget.ImageButton',
528
- 'android.widget.TextView',
529
- ];
530
-
531
- const traverse = (current: any) => {
532
- if (!current || typeof current !== 'object') return;
533
-
534
- // maestro hierarchy JSON format: { attributes: {...}, children: [...] }
535
- const attrs = current.attributes || {};
536
- const resourceId: string = attrs['resource-id'] || '';
537
- const text: string = attrs['text'] || attrs['accessibilityText'] || attrs['hintText'] || '';
538
- const cls: string = attrs['class'] || '';
539
- const clickable: boolean = attrs['clickable'] === 'true';
540
-
541
- // Only include app-specific elements (skip Android system UI)
542
- const isAppElement = resourceId && !resourceId.startsWith('android:id/') && !resourceId.startsWith('com.android.');
543
- const isInteractive = INTERACTIVE_CLASSES.includes(cls) || clickable;
544
-
545
- if (isAppElement && isInteractive) {
546
- elements.push({
547
- type: cls.split('.').pop() || cls,
548
- text: text || 'N/A',
549
- id: resourceId.includes('/') ? resourceId.split('/')[1] : resourceId,
550
- });
551
- }
552
-
553
- // Recurse into children
554
- const children = current.children || [];
555
- for (const child of children) {
556
- traverse(child);
557
- }
558
- };
559
-
560
- traverse(node);
561
- return elements.slice(0, 30);
562
- }
563
- };