@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.
- package/dist/api-client.d.ts +87 -10
- package/dist/api-client.d.ts.map +1 -1
- package/dist/api-client.js +108 -7
- package/dist/api-client.js.map +1 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +4 -3
- package/dist/auth.js.map +1 -1
- package/dist/config.d.ts +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +4 -1
- package/dist/config.js.map +1 -1
- package/dist/discovery-report.d.ts +13 -0
- package/dist/discovery-report.d.ts.map +1 -0
- package/dist/discovery-report.js +977 -0
- package/dist/discovery-report.js.map +1 -0
- package/dist/discovery.d.ts +213 -3
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +2125 -264
- package/dist/discovery.js.map +1 -1
- package/dist/evidence-corpus.d.ts +338 -0
- package/dist/evidence-corpus.d.ts.map +1 -0
- package/dist/evidence-corpus.js +927 -0
- package/dist/evidence-corpus.js.map +1 -0
- package/dist/evidence-types.d.ts +230 -0
- package/dist/evidence-types.d.ts.map +1 -0
- package/dist/evidence-types.js +24 -0
- package/dist/evidence-types.js.map +1 -0
- package/dist/executor.d.ts +1 -0
- package/dist/executor.d.ts.map +1 -1
- package/dist/executor.js +93 -195
- package/dist/executor.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/suite-manager.d.ts +6 -0
- package/dist/suite-manager.d.ts.map +1 -1
- package/dist/suite-manager.js +55 -2
- package/dist/suite-manager.js.map +1 -1
- package/package.json +4 -1
- package/src/api-client.ts +0 -170
- package/src/auth.ts +0 -45
- package/src/backup-manager.ts +0 -63
- package/src/config.ts +0 -52
- package/src/devices.ts +0 -196
- package/src/discovery.ts +0 -563
- package/src/executor.ts +0 -681
- package/src/index.ts +0 -11
- package/src/result-sanitizer.ts +0 -60
- package/src/suite-manager.ts +0 -73
- package/src/uploader.ts +0 -74
- package/tsconfig.json +0 -8
package/dist/discovery.js
CHANGED
|
@@ -4,6 +4,8 @@ import os from 'os';
|
|
|
4
4
|
import path from 'path';
|
|
5
5
|
import chalk from 'chalk';
|
|
6
6
|
import { FlowSpecQAApi } from './api-client.js';
|
|
7
|
+
import { normalizeResultType, } from './evidence-types.js';
|
|
8
|
+
import { addRecord, buildRecord, corpusSummary, createCorpus, detectForm, detectLoginForm, detectScreenChange, finalizeCorpus, findBaselineForFingerprint, inferScreenName, isSameScreen, legacyElementsToUIElements, looksLikeButton, rankNavigableButtons, shouldCapture, } from './evidence-corpus.js';
|
|
7
9
|
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
8
10
|
// ─── ADB path resolution (mirrors devices.ts) ───────────────────────────────
|
|
9
11
|
const ADB_COMMON_PATHS = [
|
|
@@ -27,13 +29,411 @@ function resolveAdb() {
|
|
|
27
29
|
}
|
|
28
30
|
throw new Error('adb not found. Install Android SDK platform-tools and add to PATH.');
|
|
29
31
|
}
|
|
32
|
+
// ─── Maestro path resolution ─────────────────────────────────────────────────
|
|
33
|
+
function resolveMaestro() {
|
|
34
|
+
const home = process.env.HOME ?? '';
|
|
35
|
+
// Absolute paths: check file existence — avoids needing PATH set in child process
|
|
36
|
+
const absoluteCandidates = [
|
|
37
|
+
`${home}/.maestro/bin/maestro`,
|
|
38
|
+
'/usr/local/bin/maestro',
|
|
39
|
+
'/opt/homebrew/bin/maestro',
|
|
40
|
+
];
|
|
41
|
+
for (const p of absoluteCandidates) {
|
|
42
|
+
if (fs.existsSync(p))
|
|
43
|
+
return p;
|
|
44
|
+
}
|
|
45
|
+
// Fallback: rely on PATH (works when user has ~/.maestro/bin in their shell PATH)
|
|
46
|
+
try {
|
|
47
|
+
execSync('which maestro', { stdio: 'ignore', timeout: 2000 });
|
|
48
|
+
return 'maestro';
|
|
49
|
+
}
|
|
50
|
+
catch { /* not in PATH */ }
|
|
51
|
+
throw new Error('Maestro CLI no encontrado. Instálalo con:\n' +
|
|
52
|
+
' curl -Ls "https://get.maestro.mobile.dev" | bash\n' +
|
|
53
|
+
'Luego abre una terminal nueva o ejecuta: export PATH="$PATH:$HOME/.maestro/bin"');
|
|
54
|
+
}
|
|
55
|
+
/** Returns an env object that always includes ~/.maestro/bin in PATH */
|
|
56
|
+
function maestroEnvWithPath() {
|
|
57
|
+
const home = process.env.HOME ?? '';
|
|
58
|
+
const maestroBin = `${home}/.maestro/bin`;
|
|
59
|
+
const currentPath = process.env.PATH ?? '';
|
|
60
|
+
return {
|
|
61
|
+
...process.env,
|
|
62
|
+
PATH: currentPath.includes(maestroBin) ? currentPath : `${maestroBin}:${currentPath}`,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const cleanYamlText = (t) => {
|
|
66
|
+
return t.replace(/"/g, '\\"').replace(/\n/g, ' ');
|
|
67
|
+
};
|
|
68
|
+
const MAX_TEXT_SELECTOR_LEN = 40;
|
|
69
|
+
const buildMaestroSelector = (selectorId, isIos = false) => {
|
|
70
|
+
if (selectorId.startsWith('text:')) {
|
|
71
|
+
const raw = cleanYamlText(selectorId.replace('text:', ''));
|
|
72
|
+
// Maestro uses text as a regex — very long strings cause "element not found" failures.
|
|
73
|
+
// Truncate to the first MAX_TEXT_SELECTOR_LEN characters for reliable matching.
|
|
74
|
+
if (raw.length > MAX_TEXT_SELECTOR_LEN) {
|
|
75
|
+
const safe = raw.slice(0, MAX_TEXT_SELECTOR_LEN);
|
|
76
|
+
const escaped = safe.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
77
|
+
return `textRegex: ".*${escaped}.*"`;
|
|
78
|
+
}
|
|
79
|
+
return `text: "${raw}"`;
|
|
80
|
+
}
|
|
81
|
+
if (selectorId.startsWith('below:')) {
|
|
82
|
+
const textVal = cleanYamlText(selectorId.replace('below:', ''));
|
|
83
|
+
return `below:\n text: "${textVal}"\n index: 0`;
|
|
84
|
+
}
|
|
85
|
+
return `id: "${selectorId}"`;
|
|
86
|
+
};
|
|
30
87
|
/**
|
|
31
88
|
* Autonomous Discovery Engine 2.0
|
|
32
89
|
* Uses Playwright and Maestro to extract real UI intelligence.
|
|
33
90
|
*/
|
|
34
91
|
export const discoveryEngine = {
|
|
92
|
+
// ── Progress helper ─────────────────────────────────────────────────────
|
|
93
|
+
_progress(opts, step, detail) {
|
|
94
|
+
if (opts.onProgress) {
|
|
95
|
+
opts.onProgress(step, detail);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
// ── ADB screenshot capture ───────────────────────────────────────────────
|
|
99
|
+
/**
|
|
100
|
+
* Captures a screenshot from the connected Android device via ADB and
|
|
101
|
+
* returns it as a base64-encoded PNG string.
|
|
102
|
+
* Returns null silently if ADB capture fails (non-fatal).
|
|
103
|
+
*/
|
|
104
|
+
/**
|
|
105
|
+
* Capture a screenshot from the Android device and return it as a
|
|
106
|
+
* base64-encoded PNG string. Returns null (PARTIAL_EVIDENCE) if all
|
|
107
|
+
* methods fail — the screen was still discovered via hierarchy.
|
|
108
|
+
*
|
|
109
|
+
* Fallback chain:
|
|
110
|
+
* 1. adb exec-out screencap -p (direct pipe, no temp file on device)
|
|
111
|
+
* 2. adb shell screencap + pull (classic two-step)
|
|
112
|
+
* 3. null → PARTIAL_EVIDENCE
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* Verifies that the target app is in the foreground (Android only).
|
|
116
|
+
* Uses `adb shell dumpsys window` to read the current focused window package.
|
|
117
|
+
*
|
|
118
|
+
* If the app is NOT in foreground (e.g. home screen showed up because the app
|
|
119
|
+
* failed to launch or crashed), it retries launching with increasing wait times:
|
|
120
|
+
* Attempt 1: monkey launch + 5 s
|
|
121
|
+
* Attempt 2: force-stop + monkey launch + 8 s
|
|
122
|
+
* Attempt 3: pm clear + monkey launch + 12 s
|
|
123
|
+
*
|
|
124
|
+
* If all retries fail, asks the user interactively via options.onRetryFailed.
|
|
125
|
+
* Returns true if the app is confirmed in foreground, false otherwise.
|
|
126
|
+
*/
|
|
127
|
+
async _ensureAppInForeground(adbS, appId, options, maxRetries = 3) {
|
|
128
|
+
const getForegroudPackage = () => {
|
|
129
|
+
try {
|
|
130
|
+
// dumpsys window returns lines like:
|
|
131
|
+
// mCurrentFocus=Window{... cl.consorcio.mobileapp/com.example.MainActivity}
|
|
132
|
+
const out = execSync(`${adbS} shell dumpsys window | grep -E "mCurrentFocus|mFocusedApp"`, {
|
|
133
|
+
encoding: 'utf8',
|
|
134
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
135
|
+
timeout: 5000,
|
|
136
|
+
});
|
|
137
|
+
const match = out.match(/([a-zA-Z][a-zA-Z0-9_.]+)\/[a-zA-Z]/);
|
|
138
|
+
return match ? match[1] : '';
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return '';
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
const isAppInForeground = () => {
|
|
145
|
+
const pkg = getForegroudPackage();
|
|
146
|
+
return pkg === appId || pkg.startsWith(appId);
|
|
147
|
+
};
|
|
148
|
+
// Fast-path: app already in foreground
|
|
149
|
+
if (isAppInForeground())
|
|
150
|
+
return true;
|
|
151
|
+
const waitTimes = [5000, 8000, 12000];
|
|
152
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
153
|
+
const waitMs = waitTimes[attempt - 1] ?? 12000;
|
|
154
|
+
console.log(chalk.yellow(` ⚠️ La app "${appId}" no está en primer plano (intento ${attempt}/${maxRetries}). Relanzando...`));
|
|
155
|
+
this._progress(options, 'launch', `Relanzando app (intento ${attempt})...`);
|
|
156
|
+
try {
|
|
157
|
+
if (attempt >= 2) {
|
|
158
|
+
// Force stop first on second+ attempts
|
|
159
|
+
try {
|
|
160
|
+
execSync(`${adbS} shell am force-stop ${appId}`, { stdio: 'ignore' });
|
|
161
|
+
}
|
|
162
|
+
catch { }
|
|
163
|
+
await sleep(800);
|
|
164
|
+
}
|
|
165
|
+
if (attempt === maxRetries) {
|
|
166
|
+
// Last resort: full state clear
|
|
167
|
+
try {
|
|
168
|
+
execSync(`${adbS} shell pm clear ${appId}`, { stdio: 'ignore' });
|
|
169
|
+
}
|
|
170
|
+
catch { }
|
|
171
|
+
await sleep(1500);
|
|
172
|
+
}
|
|
173
|
+
execSync(`${adbS} shell monkey -p ${appId} -c android.intent.category.LAUNCHER 1`, { stdio: 'ignore' });
|
|
174
|
+
}
|
|
175
|
+
catch { }
|
|
176
|
+
await sleep(waitMs);
|
|
177
|
+
if (isAppInForeground()) {
|
|
178
|
+
console.log(chalk.green(` ✅ App "${appId}" confirmada en primer plano tras reintento ${attempt}.`));
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// All retries exhausted — ask the user if there is an interactive callback
|
|
183
|
+
console.log(chalk.red(` ❌ La app "${appId}" no pudo ser confirmada en primer plano tras ${maxRetries} intentos.`));
|
|
184
|
+
if (options.onRetryFailed) {
|
|
185
|
+
const proceed = await options.onRetryFailed(`La app "${appId}" no respondió después de ${maxRetries} intentos de relanzamiento. ` +
|
|
186
|
+
`¿Deseas continuar de todas formas (puede generar evidencia incorrecta)?`);
|
|
187
|
+
return proceed ?? false;
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
},
|
|
191
|
+
async _captureScreenshotMobile(adbS, tag, deviceId, appId) {
|
|
192
|
+
// ── iOS path: use Maestro screenshot flow ──────────────────────────────
|
|
193
|
+
if (adbS === 'maestro') {
|
|
194
|
+
const tempPath = path.join(os.tmpdir(), `fsqa-shot-${Date.now()}-${tag}.png`);
|
|
195
|
+
const deviceFlag = deviceId ? `--device ${deviceId} ` : '';
|
|
196
|
+
const maestro = resolveMaestro();
|
|
197
|
+
try {
|
|
198
|
+
const flow = `appId: ${appId}\n---\n- screenshot: ${tempPath}`;
|
|
199
|
+
const tmp = path.join(os.tmpdir(), `fsqa-shot-flow-${Date.now()}.yaml`);
|
|
200
|
+
fs.writeFileSync(tmp, flow);
|
|
201
|
+
execSync(`"${maestro}" ${deviceFlag}test ${tmp}`, { stdio: ['ignore', 'pipe', 'pipe'], timeout: 15000 });
|
|
202
|
+
try {
|
|
203
|
+
fs.unlinkSync(tmp);
|
|
204
|
+
}
|
|
205
|
+
catch { }
|
|
206
|
+
if (fs.existsSync(tempPath)) {
|
|
207
|
+
const b64 = fs.readFileSync(tempPath, { encoding: 'base64' });
|
|
208
|
+
try {
|
|
209
|
+
fs.unlinkSync(tempPath);
|
|
210
|
+
}
|
|
211
|
+
catch { }
|
|
212
|
+
return `data:image/png;base64,${b64}`;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
catch (err) {
|
|
216
|
+
console.log(chalk.dim(` ⚠️ Captura iOS falló (${tag}): ${err instanceof Error ? err.message.split('\n')[0] : String(err)}`));
|
|
217
|
+
}
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
// ── Method 1: exec-out direct to buffer (no temp file on device or disk) ──
|
|
221
|
+
try {
|
|
222
|
+
// execSync with no encoding returns a Buffer of raw bytes
|
|
223
|
+
const buffer = execSync(`${adbS} exec-out screencap -p`, { timeout: 8000, maxBuffer: 20 * 1024 * 1024 });
|
|
224
|
+
if (Buffer.isBuffer(buffer) && buffer.length > 1000) {
|
|
225
|
+
return buffer.toString('base64');
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
catch { }
|
|
229
|
+
// ── Method 2: shell screencap + pull (classic two-step) ─────────────────
|
|
230
|
+
try {
|
|
231
|
+
const remotePath = `/sdcard/fpqa_discovery_${tag}.png`;
|
|
232
|
+
const localPath = path.join(os.tmpdir(), `fpqa_disc_${tag}_${Date.now()}.png`);
|
|
233
|
+
execSync(`${adbS} shell screencap -p "${remotePath}"`, { stdio: 'ignore', timeout: 6000 });
|
|
234
|
+
execSync(`${adbS} pull "${remotePath}" "${localPath}"`, { stdio: 'ignore', timeout: 8000 });
|
|
235
|
+
try {
|
|
236
|
+
execSync(`${adbS} shell rm "${remotePath}"`, { stdio: 'ignore', timeout: 3000 });
|
|
237
|
+
}
|
|
238
|
+
catch { }
|
|
239
|
+
const buffer = fs.readFileSync(localPath);
|
|
240
|
+
try {
|
|
241
|
+
fs.unlinkSync(localPath);
|
|
242
|
+
}
|
|
243
|
+
catch { }
|
|
244
|
+
if (buffer.length > 1000)
|
|
245
|
+
return buffer.toString('base64');
|
|
246
|
+
}
|
|
247
|
+
catch { }
|
|
248
|
+
// ── Method 3: PARTIAL_EVIDENCE ───────────────────────────────────────────
|
|
249
|
+
// Screen was discovered via Maestro hierarchy but screenshot unavailable.
|
|
250
|
+
// Marked in the report as needing recapture before STRICT visual cases
|
|
251
|
+
// can be approved.
|
|
252
|
+
return null;
|
|
253
|
+
},
|
|
254
|
+
// ── Hierarchy helpers ────────────────────────────────────────────────────
|
|
255
|
+
/** Capture maestro hierarchy from current screen and return parsed JSON */
|
|
256
|
+
async _captureHierarchy(env, deviceId) {
|
|
257
|
+
// --device is a GLOBAL flag in Maestro — must go before the subcommand.
|
|
258
|
+
// Without it, maestro hierarchy prompts interactively and hangs in non-TTY contexts.
|
|
259
|
+
// Redirect stderr to /dev/null to suppress verbose INFO/DEBUG logs.
|
|
260
|
+
const deviceFlag = deviceId ? `--device ${deviceId} ` : '';
|
|
261
|
+
const maestro = resolveMaestro();
|
|
262
|
+
const rawOutput = execSync(`"${maestro}" ${deviceFlag}hierarchy 2>/dev/null`, {
|
|
263
|
+
encoding: 'utf8',
|
|
264
|
+
maxBuffer: 20 * 1024 * 1024, // 20 MB — hierarchy JSON can be large
|
|
265
|
+
env: { ...maestroEnvWithPath(), ...(env ?? {}) },
|
|
266
|
+
});
|
|
267
|
+
// Maestro outputs Java warnings + JSON + sometimes trailing log lines.
|
|
268
|
+
// Extract exactly the balanced JSON object by tracking brace depth.
|
|
269
|
+
const start = rawOutput.indexOf('{');
|
|
270
|
+
if (start === -1)
|
|
271
|
+
throw new Error('No JSON found in maestro hierarchy output');
|
|
272
|
+
let depth = 0;
|
|
273
|
+
let inString = false;
|
|
274
|
+
let escape = false;
|
|
275
|
+
let end = -1;
|
|
276
|
+
for (let i = start; i < rawOutput.length; i++) {
|
|
277
|
+
const ch = rawOutput[i];
|
|
278
|
+
if (escape) {
|
|
279
|
+
escape = false;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (ch === '\\' && inString) {
|
|
283
|
+
escape = true;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (ch === '"') {
|
|
287
|
+
inString = !inString;
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
if (inString)
|
|
291
|
+
continue;
|
|
292
|
+
if (ch === '{')
|
|
293
|
+
depth++;
|
|
294
|
+
else if (ch === '}') {
|
|
295
|
+
depth--;
|
|
296
|
+
if (depth === 0) {
|
|
297
|
+
end = i;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const jsonStr = end !== -1 ? rawOutput.slice(start, end + 1) : rawOutput.slice(start);
|
|
303
|
+
return JSON.parse(jsonStr);
|
|
304
|
+
},
|
|
305
|
+
/** Execute a Maestro YAML flow (written to a temp file) */
|
|
306
|
+
_runMaestroFlow(appId, steps, env) {
|
|
307
|
+
const yaml = `appId: ${appId}\n---\n${steps.join('\n')}\n`;
|
|
308
|
+
const tmpFile = path.join(os.tmpdir(), `discovery_flow_${Date.now()}.yaml`);
|
|
309
|
+
fs.writeFileSync(tmpFile, yaml);
|
|
310
|
+
try {
|
|
311
|
+
const args = ['test'];
|
|
312
|
+
if (env?.MAESTRO_DEVICE_ID) {
|
|
313
|
+
args.push('--device', env.MAESTRO_DEVICE_ID);
|
|
314
|
+
}
|
|
315
|
+
args.push(tmpFile);
|
|
316
|
+
const result = spawnSync(resolveMaestro(), args, {
|
|
317
|
+
encoding: 'utf8',
|
|
318
|
+
timeout: 120000,
|
|
319
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
320
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
321
|
+
env: { ...maestroEnvWithPath(), ...(env ?? {}) },
|
|
322
|
+
});
|
|
323
|
+
fs.unlinkSync(tmpFile);
|
|
324
|
+
const ok = result.status === 0;
|
|
325
|
+
// Filter JVM runtime warnings (sun.misc.Unsafe, jansi, etc.) — not real Maestro errors
|
|
326
|
+
const filterJvmWarnings = (text) => text.split('\n')
|
|
327
|
+
.filter(l => !/^WARNING:/.test(l.trim()) && !/^> Please consider reporting/.test(l.trim()))
|
|
328
|
+
.join('\n')
|
|
329
|
+
.trim();
|
|
330
|
+
if (!ok) {
|
|
331
|
+
console.log(chalk.red(`\n⚠️ Maestro Flow falló (código de salida: ${result.status})`));
|
|
332
|
+
const cleanStdout = filterJvmWarnings(result.stdout ?? '');
|
|
333
|
+
const cleanStderr = filterJvmWarnings(result.stderr ?? '');
|
|
334
|
+
if (cleanStdout) {
|
|
335
|
+
console.log(chalk.yellow(`Maestro stdout:\n${cleanStdout}`));
|
|
336
|
+
}
|
|
337
|
+
if (cleanStderr) {
|
|
338
|
+
console.log(chalk.yellow(`Maestro stderr:\n${cleanStderr}`));
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return {
|
|
342
|
+
ok,
|
|
343
|
+
stdout: result.stdout ?? '',
|
|
344
|
+
stderr: result.stderr ?? '',
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
catch (e) {
|
|
348
|
+
try {
|
|
349
|
+
fs.unlinkSync(tmpFile);
|
|
350
|
+
}
|
|
351
|
+
catch { }
|
|
352
|
+
console.log(chalk.red(`\n⚠️ Error de ejecución en Maestro: ${e?.message ?? e}`));
|
|
353
|
+
return { ok: false, stdout: '', stderr: e?.message ?? 'unknown error' };
|
|
354
|
+
}
|
|
355
|
+
},
|
|
356
|
+
/** Smart navigation back to parent screen */
|
|
357
|
+
async _smartBack(appId, maestroEnv, deviceId, parentUI, launchUI, loginForm, parentName) {
|
|
358
|
+
let returnedToParent = false;
|
|
359
|
+
const BACK_BUTTON_RE = /^(cerrar|volver|atr[aá]s|cancelar|close|back|cancel|btn_back|btn_close|btn_cancel)$/i;
|
|
360
|
+
for (let attempt = 0; attempt < 4; attempt++) {
|
|
361
|
+
let tappedBack = false;
|
|
362
|
+
const currentHier = await this._captureHierarchy(maestroEnv, deviceId).catch(() => null);
|
|
363
|
+
if (currentHier) {
|
|
364
|
+
const currentEls = this.extractMaestroSelectors(currentHier);
|
|
365
|
+
const backBtn = currentEls.find(e => {
|
|
366
|
+
const id = (e.id || '').toLowerCase();
|
|
367
|
+
const text = (e.text || '').toLowerCase().trim();
|
|
368
|
+
return BACK_BUTTON_RE.test(text) || BACK_BUTTON_RE.test(id);
|
|
369
|
+
});
|
|
370
|
+
if (backBtn) {
|
|
371
|
+
console.log(chalk.gray(` 🔙 Tapping back/close element "${backBtn.text || backBtn.id}" to return to parent...`));
|
|
372
|
+
const isIos = !!deviceId && /iPhone|iPad/i.test(deviceId);
|
|
373
|
+
const backSelector = buildMaestroSelector(backBtn.id, isIos);
|
|
374
|
+
this._runMaestroFlow(appId, [
|
|
375
|
+
`- tapOn:\n ${backSelector}`,
|
|
376
|
+
`- waitForAnimationToEnd:\n timeout: 1000`,
|
|
377
|
+
], maestroEnv);
|
|
378
|
+
tappedBack = true;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (!tappedBack) {
|
|
382
|
+
this._runMaestroFlow(appId, [
|
|
383
|
+
`- back`,
|
|
384
|
+
`- waitForAnimationToEnd:\n timeout: 1000`,
|
|
385
|
+
], maestroEnv);
|
|
386
|
+
}
|
|
387
|
+
await sleep(800);
|
|
388
|
+
const checkHier = await this._captureHierarchy(maestroEnv, deviceId).catch(() => null);
|
|
389
|
+
if (!checkHier)
|
|
390
|
+
continue;
|
|
391
|
+
const checkEls = this.extractMaestroSelectors(checkHier);
|
|
392
|
+
const checkUI = legacyElementsToUIElements(checkEls, '__pending__');
|
|
393
|
+
if (isSameScreen(checkUI, parentUI, 0.7)) {
|
|
394
|
+
returnedToParent = true;
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
if (loginForm && isSameScreen(checkUI, launchUI, 0.7)) {
|
|
398
|
+
console.log(chalk.yellow(` ⚠️ Volvimos a la pantalla inicial mientras navegábamos hacia atrás. BFS detenido.`));
|
|
399
|
+
returnedToParent = false;
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return returnedToParent;
|
|
404
|
+
},
|
|
405
|
+
// ── Text extraction from accessibility hierarchy ─────────────────────────
|
|
406
|
+
/**
|
|
407
|
+
* Extracts ALL visible text strings from a Maestro hierarchy JSON.
|
|
408
|
+
* Used for confidence scoring — any text returned here is "verified" (actually on screen).
|
|
409
|
+
*/
|
|
410
|
+
extractAllTexts(node) {
|
|
411
|
+
const texts = new Set();
|
|
412
|
+
const traverse = (current) => {
|
|
413
|
+
if (!current || typeof current !== 'object')
|
|
414
|
+
return;
|
|
415
|
+
const attrs = current.attributes || {};
|
|
416
|
+
const text = (attrs['text'] || '').trim();
|
|
417
|
+
// Skip: too short, pure numbers, system strings, clock times (HH:MM), battery/signal indicators
|
|
418
|
+
const isNoise = !text ||
|
|
419
|
+
text.length < 3 ||
|
|
420
|
+
text.match(/^[\d.,\s%]+$/) || // pure numbers / percentages
|
|
421
|
+
text.match(/^\d{1,2}:\d{2}(:\d{2})?$/) || // clock times like "13:38"
|
|
422
|
+
text.startsWith('android') ||
|
|
423
|
+
text.match(/^[▲▼◀▶⬆⬇⬅➡]+$/); // navigation arrows / icons only
|
|
424
|
+
if (!isNoise) {
|
|
425
|
+
texts.add(text);
|
|
426
|
+
}
|
|
427
|
+
for (const child of (current.children || [])) {
|
|
428
|
+
traverse(child);
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
traverse(node);
|
|
432
|
+
return [...texts];
|
|
433
|
+
},
|
|
434
|
+
// ── Main entry point ─────────────────────────────────────────────────────
|
|
35
435
|
async run(options) {
|
|
36
|
-
console.log(chalk.blue(`\n🔍
|
|
436
|
+
console.log(chalk.blue(`\n🔍 Iniciando Discovery autónomo para: ${options.url}`));
|
|
37
437
|
let result;
|
|
38
438
|
if (options.framework === 'maestro') {
|
|
39
439
|
result = await this.discoverMobile(options);
|
|
@@ -41,261 +441,1152 @@ export const discoveryEngine = {
|
|
|
41
441
|
else {
|
|
42
442
|
result = await this.discoverWeb(options);
|
|
43
443
|
}
|
|
44
|
-
//
|
|
444
|
+
// ── Interactive exploration loop (mobile only) ────────────────────────
|
|
445
|
+
if (options.onInteractiveLoop && result.evidence) {
|
|
446
|
+
let continueLoop = true;
|
|
447
|
+
while (continueLoop) {
|
|
448
|
+
continueLoop = await options.onInteractiveLoop(result.evidence);
|
|
449
|
+
if (continueLoop && result.evidence) {
|
|
450
|
+
// After each exploration round, rebuild the plan from updated evidence
|
|
451
|
+
result.planContent = this.buildPlanFromEvidence(result.evidence, options.url);
|
|
452
|
+
result.metrics = result.evidence.metrics;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
// Ensure final plan reflects all explorations
|
|
456
|
+
if (result.evidence) {
|
|
457
|
+
result.planContent = this.buildPlanFromEvidence(result.evidence, options.url);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
// Persist plan locally
|
|
45
461
|
const outputDir = options.outputDir || path.join(process.cwd(), 'specs');
|
|
46
462
|
if (!fs.existsSync(outputDir))
|
|
47
463
|
fs.mkdirSync(outputDir, { recursive: true });
|
|
48
464
|
const planPath = path.join(outputDir, 'discovery_plan.md');
|
|
49
465
|
fs.writeFileSync(planPath, result.planContent);
|
|
50
|
-
console.log(chalk.green(`\n✅
|
|
466
|
+
console.log(chalk.green(`\n✅ Plan de Discovery guardado: ${chalk.bold(planPath)}`));
|
|
467
|
+
// Generate HTML evidence report
|
|
468
|
+
try {
|
|
469
|
+
const { generateDiscoveryReport } = await import('./discovery-report.js');
|
|
470
|
+
const reportPath = generateDiscoveryReport(result, {
|
|
471
|
+
outputDir,
|
|
472
|
+
framework: options.framework,
|
|
473
|
+
url: options.url,
|
|
474
|
+
});
|
|
475
|
+
result.reportPath = reportPath;
|
|
476
|
+
console.log(chalk.green(`📋 Reporte de evidencias: ${chalk.bold(reportPath)}`));
|
|
477
|
+
}
|
|
478
|
+
catch (reportErr) {
|
|
479
|
+
console.log(chalk.yellow(`⚠️ Reporte no generado: ${reportErr.message}`));
|
|
480
|
+
}
|
|
51
481
|
// Sync with SaaS
|
|
52
482
|
try {
|
|
53
|
-
|
|
483
|
+
this._progress(options, 'sync', 'Sincronizando con FlowSpecQA SaaS...');
|
|
484
|
+
console.log(chalk.yellow('📡 Sincronizando con FlowSpecQA SaaS...'));
|
|
54
485
|
await FlowSpecQAApi.syncDiscoveryPlan({
|
|
486
|
+
project_id: options.projectId || 'TRUSTBANK',
|
|
487
|
+
entity_id: options.entityId || 'UNKNOWN',
|
|
55
488
|
url: options.url,
|
|
56
489
|
plan_content: result.planContent,
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
project_id: options.projectId,
|
|
60
|
-
entity_id: options.entityId,
|
|
490
|
+
framework: options.framework || 'playwright',
|
|
491
|
+
evidence_corpus: result.corpus,
|
|
61
492
|
});
|
|
62
|
-
console.log(chalk.green('✅
|
|
493
|
+
console.log(chalk.green('✅ Sincronizado. El generador de IA ahora tiene contexto UI real.'));
|
|
494
|
+
// Ingest discovery plan into Qdrant — only if this run discovered MORE screens than before
|
|
495
|
+
if (options.projectId && options.entityId) {
|
|
496
|
+
const newScreenCount = result.evidence?.screens?.filter((s) => !s.isErrorState).length ?? 0;
|
|
497
|
+
const minScreensToIngest = 2; // at least Login + one authenticated screen
|
|
498
|
+
if (newScreenCount < minScreensToIngest) {
|
|
499
|
+
console.log(chalk.yellow(`⚠️ Indexado Qdrant omitido — plan incompleto (${newScreenCount} pantalla(s) sin error states). ` +
|
|
500
|
+
`El plan anterior se conserva. Verifica credenciales con --username y --password.`));
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
503
|
+
try {
|
|
504
|
+
this._progress(options, 'sync', 'Indexando plan en memoria vectorial...');
|
|
505
|
+
const ingestResult = await FlowSpecQAApi.ingestDiscoveryPlan({
|
|
506
|
+
project_id: options.projectId,
|
|
507
|
+
entity_id: options.entityId,
|
|
508
|
+
source: 'discovery-plan',
|
|
509
|
+
});
|
|
510
|
+
console.log(chalk.green(`✅ Plan indexado en Qdrant (~${ingestResult.chunks_ingested} chunks).`));
|
|
511
|
+
}
|
|
512
|
+
catch (ingestErr) {
|
|
513
|
+
const ingestMsg = ingestErr.response?.data?.detail || ingestErr.message || String(ingestErr);
|
|
514
|
+
console.log(chalk.yellow(`⚠️ Indexado Qdrant omitido: ${ingestMsg}`));
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
63
518
|
}
|
|
64
519
|
catch (error) {
|
|
65
520
|
const errorMsg = error.response?.data?.detail || error.message || String(error);
|
|
66
|
-
console.log(chalk.red(`⚠️
|
|
67
|
-
console.log(chalk.gray(`
|
|
521
|
+
console.log(chalk.red(`⚠️ Sync omitido: ${errorMsg}`));
|
|
522
|
+
console.log(chalk.gray(` Ejecuta: flowspecqa generate --from ${planPath}\n`));
|
|
68
523
|
}
|
|
69
524
|
return result;
|
|
70
525
|
},
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
timeout: 120000,
|
|
92
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
93
|
-
env: env ?? process.env,
|
|
94
|
-
});
|
|
95
|
-
fs.unlinkSync(tmpFile);
|
|
96
|
-
return result.status === 0;
|
|
97
|
-
}
|
|
98
|
-
catch {
|
|
526
|
+
// ── Phase 13: Manual Capture (single screen, no BFS) ──────────────────────
|
|
527
|
+
/**
|
|
528
|
+
* Capture the device's current screen as a single EvidenceRecord and upload
|
|
529
|
+
* it to the SaaS as a manual capture (`capture_mode='manual'`).
|
|
530
|
+
*
|
|
531
|
+
* Used by `flowspecqa capture` to fill specific evidence gaps that the
|
|
532
|
+
* autonomous BFS couldn't reach (e.g. comprobante after a PIN/2FA flow).
|
|
533
|
+
* The caller is responsible for navigating the device to the desired
|
|
534
|
+
* screen before invoking this.
|
|
535
|
+
*
|
|
536
|
+
* Spec: DISCOVERY_SPEC.md § 13.
|
|
537
|
+
*/
|
|
538
|
+
async captureSingleManualScreen(opts) {
|
|
539
|
+
const adb = resolveAdb();
|
|
540
|
+
const adbS = opts.deviceId ? `"${adb}" -s ${opts.deviceId}` : `"${adb}"`;
|
|
541
|
+
const env = opts.deviceId ? { ...process.env, MAESTRO_DEVICE_ID: opts.deviceId } : process.env;
|
|
542
|
+
const isIos = !!opts.deviceId && /iPhone|iPad/i.test(opts.deviceId);
|
|
543
|
+
opts.onProgress?.('capture', 'Capturando estado actual del dispositivo...');
|
|
544
|
+
// 1. Wake the screen + take screenshot from current state (no relaunch)
|
|
545
|
+
if (!isIos) {
|
|
99
546
|
try {
|
|
100
|
-
|
|
547
|
+
execSync(`${adbS} shell input keyevent KEYCODE_WAKEUP`, { stdio: 'ignore' });
|
|
101
548
|
}
|
|
102
549
|
catch { }
|
|
103
|
-
|
|
550
|
+
try {
|
|
551
|
+
execSync(`${adbS} shell wm dismiss-keyguard`, { stdio: 'ignore' });
|
|
552
|
+
}
|
|
553
|
+
catch { }
|
|
554
|
+
await sleep(400);
|
|
104
555
|
}
|
|
556
|
+
const screenshot = await this._captureScreenshotMobile(isIos ? 'maestro' : adbS, 'manual', opts.deviceId, opts.appId);
|
|
557
|
+
// 2. Extract hierarchy + elements
|
|
558
|
+
const hierarchy = await this._captureHierarchy(env, opts.deviceId);
|
|
559
|
+
const elements = this.extractMaestroSelectors(hierarchy);
|
|
560
|
+
const texts = this.extractAllTexts(hierarchy);
|
|
561
|
+
const ui = legacyElementsToUIElements(elements, '__pending__');
|
|
562
|
+
// 3. Compute fingerprint (same algo as discoverMobile)
|
|
563
|
+
const fingerprintTexts = ui.map(e => e.visibleText).filter((t) => !!t);
|
|
564
|
+
const { screenFingerprint } = await import('./evidence-corpus.js');
|
|
565
|
+
const fp = screenFingerprint(ui, fingerprintTexts);
|
|
566
|
+
opts.onProgress?.('upload', 'Subiendo captura al SaaS...');
|
|
567
|
+
// 4. Upload to backend (non-fatal if the upload fails — the local
|
|
568
|
+
// capture is still useful for the operator)
|
|
569
|
+
const result = await FlowSpecQAApi.uploadManualCapture({
|
|
570
|
+
entity_id: opts.entityId,
|
|
571
|
+
label: opts.label,
|
|
572
|
+
screen_name: opts.label,
|
|
573
|
+
elements: ui,
|
|
574
|
+
texts,
|
|
575
|
+
screenshot_b64: screenshot ?? undefined,
|
|
576
|
+
screen_fingerprint: fp,
|
|
577
|
+
platform: isIos ? 'ios' : 'android',
|
|
578
|
+
project_id: opts.projectId || 'TRUSTBANK',
|
|
579
|
+
});
|
|
580
|
+
return {
|
|
581
|
+
record_id: result.record_id,
|
|
582
|
+
session_id: result.session_id,
|
|
583
|
+
evidence_quality: result.evidence_quality,
|
|
584
|
+
elements_count: ui.length,
|
|
585
|
+
has_screenshot: !!screenshot,
|
|
586
|
+
};
|
|
105
587
|
},
|
|
588
|
+
// ── Mobile Discovery ─────────────────────────────────────────────────────
|
|
106
589
|
async discoverMobile(options) {
|
|
107
590
|
const appId = options.url;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
//
|
|
114
|
-
const
|
|
115
|
-
|
|
591
|
+
let username = options.username || '';
|
|
592
|
+
let password = options.password || '';
|
|
593
|
+
// ── Platform detection ────────────────────────────────────────────────
|
|
594
|
+
// iOS device IDs are UUIDs (contain multiple '-') or Apple hardware UDIDs
|
|
595
|
+
// (hex string with exactly one '-' like '00008101-001414A93A46001E').
|
|
596
|
+
// Android serials are purely numeric or alphanumeric without '-'.
|
|
597
|
+
const deviceId = options.deviceId;
|
|
598
|
+
const isIos = !!deviceId && (
|
|
599
|
+
// UUID format: 9B9A9E1D-3A91-563C-8A8A-8C196FC121DA (4 dashes)
|
|
600
|
+
(deviceId.match(/-/g) || []).length >= 2 ||
|
|
601
|
+
// Apple hardware UDID: 00008101-001414A93A46001E (1 dash, 24+ hex chars)
|
|
602
|
+
/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{12,}$/.test(deviceId));
|
|
603
|
+
let adbS = '';
|
|
604
|
+
if (!isIos) {
|
|
605
|
+
try {
|
|
606
|
+
const adb = resolveAdb();
|
|
607
|
+
adbS = deviceId ? `"${adb}" -s ${deviceId}` : `"${adb}"`;
|
|
608
|
+
}
|
|
609
|
+
catch {
|
|
610
|
+
console.log(chalk.yellow('⚠️ adb no disponible — solo modo iOS/Maestro disponible.'));
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
const maestroEnv = deviceId
|
|
614
|
+
? { ...process.env, MAESTRO_DEVICE_ID: deviceId }
|
|
116
615
|
: process.env;
|
|
117
|
-
|
|
616
|
+
const startedAt = Date.now();
|
|
118
617
|
const allScreens = [];
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
618
|
+
const navigationSteps = [];
|
|
619
|
+
// ── Evidence Corpus (evidence-first Discovery) ─────────────────────────
|
|
620
|
+
const corpus = createCorpus(appId);
|
|
621
|
+
this._progress(options, 'start', `Iniciando Discovery mobile: ${appId}`);
|
|
622
|
+
console.log(chalk.gray(`📱 Discovery multi-pantalla${options.deviceId ? ` — dispositivo ${options.deviceId}` : ''}...`));
|
|
623
|
+
// ── Phase 11 — Baseline fetch for incremental / recapture modes ────────
|
|
624
|
+
// We fetch the baseline ONCE up-front; the BFS consults it per-screen.
|
|
625
|
+
// Spec: DISCOVERY_SPEC.md § 11.1
|
|
626
|
+
const mode = options.mode ?? 'full';
|
|
627
|
+
let baselines = null;
|
|
628
|
+
let incrementalSkipCount = 0;
|
|
629
|
+
let incrementalCaptureCount = 0;
|
|
630
|
+
if ((mode === 'incremental' || mode === 'recapture') && options.entityId) {
|
|
122
631
|
try {
|
|
123
|
-
|
|
632
|
+
const resp = await FlowSpecQAApi.getDiscoveryBaseline(options.entityId);
|
|
633
|
+
baselines = resp.baselines || [];
|
|
634
|
+
const verifiedCount = baselines.filter(b => b.status === 'verified').length;
|
|
635
|
+
const recaptureCount = baselines.filter(b => b.status === 'needs_recapture' || b.status === 'partial').length;
|
|
636
|
+
console.log(chalk.cyan(` 📚 Baseline cargado para ${options.entityId} — ${baselines.length} pantallas ` +
|
|
637
|
+
`(${verifiedCount} verified, ${recaptureCount} needs_recapture)`));
|
|
638
|
+
if (mode === 'incremental' && verifiedCount === 0) {
|
|
639
|
+
console.log(chalk.yellow(` ⚠️ Sin baselines verificados — modo incremental se comporta como full hasta que valides pantallas en la UI`));
|
|
640
|
+
}
|
|
124
641
|
}
|
|
125
|
-
catch {
|
|
126
|
-
|
|
127
|
-
|
|
642
|
+
catch (e) {
|
|
643
|
+
console.log(chalk.yellow(` ⚠️ No se pudo cargar baseline: ${e?.message || e}. Continuando en modo full.`));
|
|
644
|
+
baselines = null;
|
|
128
645
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
// ──
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
//
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
646
|
+
}
|
|
647
|
+
try {
|
|
648
|
+
// ── Phase 12 — Resume Mode short-circuit ──────────────────────────
|
|
649
|
+
// When mode='resume', the human has manually navigated past auth
|
|
650
|
+
// barriers (PIN/2FA/OTP/SSO) and the app is already in a usable
|
|
651
|
+
// state. We skip both the reset (force-stop / pm clear) AND the
|
|
652
|
+
// relaunch (monkey / launchApp clearState) because doing either
|
|
653
|
+
// would invalidate the human's manual setup.
|
|
654
|
+
// Spec: DISCOVERY_SPEC.md § 12.2
|
|
655
|
+
if (mode === 'resume') {
|
|
656
|
+
this._progress(options, 'reset', 'Modo resume — usando estado actual del dispositivo');
|
|
657
|
+
console.log(chalk.cyan(' 🤝 Modo resume: el humano ya pasó las barreras (PIN/2FA/SSO).\n' +
|
|
658
|
+
' Discovery captura el estado actual y continúa desde ahí.\n'));
|
|
659
|
+
// Wake the screen in case it dimmed during the manual setup
|
|
660
|
+
if (!isIos) {
|
|
661
|
+
try {
|
|
662
|
+
execSync(`${adbS} shell input keyevent KEYCODE_WAKEUP`, { stdio: 'ignore' });
|
|
663
|
+
}
|
|
664
|
+
catch { }
|
|
665
|
+
try {
|
|
666
|
+
execSync(`${adbS} shell wm dismiss-keyguard`, { stdio: 'ignore' });
|
|
667
|
+
}
|
|
668
|
+
catch { }
|
|
669
|
+
await sleep(500);
|
|
670
|
+
}
|
|
671
|
+
// No app reset, no launchApp — the user did that manually.
|
|
672
|
+
}
|
|
673
|
+
else {
|
|
674
|
+
// ── Wake up screen + reset app state ──────────────────────────────
|
|
675
|
+
this._progress(options, 'reset', 'Reseteando estado de la app...');
|
|
676
|
+
console.log(chalk.gray(` 🔄 Reseteando estado de la app (${isIos ? 'iOS' : 'Android'})...`));
|
|
677
|
+
if (isIos) {
|
|
678
|
+
// iOS: state reset is handled via launchApp clearState:true in the launch flow below
|
|
679
|
+
}
|
|
680
|
+
else {
|
|
681
|
+
// Android: wake screen + clear app data via ADB
|
|
682
|
+
try {
|
|
683
|
+
execSync(`${adbS} shell input keyevent KEYCODE_WAKEUP`, { stdio: 'ignore' });
|
|
684
|
+
}
|
|
685
|
+
catch { }
|
|
686
|
+
try {
|
|
687
|
+
execSync(`${adbS} shell wm dismiss-keyguard`, { stdio: 'ignore' });
|
|
688
|
+
}
|
|
689
|
+
catch { }
|
|
690
|
+
await sleep(1000);
|
|
691
|
+
try {
|
|
692
|
+
execSync(`${adbS} shell am force-stop ${appId}`, { stdio: 'ignore' });
|
|
693
|
+
}
|
|
694
|
+
catch { }
|
|
695
|
+
await sleep(800); // espera extra para que el proceso muera completamente
|
|
696
|
+
try {
|
|
697
|
+
execSync(`${adbS} shell pm clear ${appId}`, { stdio: 'ignore' });
|
|
698
|
+
}
|
|
699
|
+
catch { }
|
|
700
|
+
await sleep(2000); // tiempo extra post-clear para que Android libere recursos
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
// ─────────────────────────────────────────────────────────────────
|
|
704
|
+
// Phase 0 — GENERIC mobile Discovery (no hardcoded IDs)
|
|
705
|
+
// Spec: smartcase/DISCOVERY_SPEC.md § 9 "Fase 0"
|
|
706
|
+
// ─────────────────────────────────────────────────────────────────
|
|
707
|
+
// ── Step 1: Launch app + capture initial screen ──────────────────
|
|
708
|
+
this._progress(options, 'screen', mode === 'resume'
|
|
709
|
+
? 'Capturando estado actual del dispositivo (resume mode)...'
|
|
710
|
+
: 'Pantalla inicial — Lanzando app...');
|
|
711
|
+
console.log(chalk.gray(mode === 'resume'
|
|
712
|
+
? ' 📸 Capturando pantalla actual (sin relanzar la app)...'
|
|
713
|
+
: ' 📸 Capturando pantalla inicial...'));
|
|
714
|
+
if (mode === 'resume') {
|
|
715
|
+
// Resume mode: do NOT relaunch — capture whatever the device shows
|
|
716
|
+
}
|
|
717
|
+
else if (isIos) {
|
|
718
|
+
// iOS: launch via Maestro flow (handles clearState + permissions)
|
|
719
|
+
const maestro = resolveMaestro();
|
|
720
|
+
const deviceFlag = deviceId ? `--device ${deviceId} ` : '';
|
|
721
|
+
const launchFlow = `appId: ${appId}\n---\n- launchApp:\n clearState: true\n- waitForAnimationToEnd:\n timeout: 3000`;
|
|
722
|
+
const tmp = path.join(os.tmpdir(), `fsqa-launch-${Date.now()}.yaml`);
|
|
723
|
+
fs.writeFileSync(tmp, launchFlow);
|
|
183
724
|
try {
|
|
184
|
-
|
|
725
|
+
execSync(`"${maestro}" ${deviceFlag}test ${tmp}`, { stdio: ['ignore', 'pipe', 'pipe'], timeout: 30000 });
|
|
726
|
+
}
|
|
727
|
+
catch (err) {
|
|
728
|
+
const stderr = err.stderr?.toString() || '';
|
|
729
|
+
throw new Error(`Maestro falló: ${stderr.split('\n').slice(0, 5).join(' | ')}`);
|
|
730
|
+
}
|
|
731
|
+
finally {
|
|
732
|
+
try {
|
|
733
|
+
fs.unlinkSync(tmp);
|
|
734
|
+
}
|
|
735
|
+
catch { }
|
|
185
736
|
}
|
|
186
|
-
|
|
187
|
-
|
|
737
|
+
}
|
|
738
|
+
else {
|
|
739
|
+
execSync(`${adbS} shell monkey -p ${appId} -c android.intent.category.LAUNCHER 1`, { stdio: 'ignore' });
|
|
740
|
+
}
|
|
741
|
+
// Cold launch takes longer on first run; resume just needs a brief settle.
|
|
742
|
+
// Banking apps require extra time: SSL pinning checks, root detection, animated splash screens.
|
|
743
|
+
await sleep(mode === 'resume' ? 1500 : (isIos ? 6000 : 8000));
|
|
744
|
+
// ── Verify the app is actually in foreground before capturing ─────────
|
|
745
|
+
// If the app failed to launch (e.g. pm clear was too slow), the home screen
|
|
746
|
+
// may be visible. _ensureAppInForeground retries up to 3 times automatically.
|
|
747
|
+
if (!isIos && mode !== 'resume') {
|
|
748
|
+
const inFg = await this._ensureAppInForeground(adbS, appId, options);
|
|
749
|
+
if (!inFg) {
|
|
750
|
+
console.log(chalk.yellow(' ⚠️ Continuando Discovery sin confirmar app en primer plano — evidencia puede ser incorrecta.'));
|
|
188
751
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
752
|
+
}
|
|
753
|
+
const launchScreenshot = await this._captureScreenshotMobile(isIos ? 'maestro' : adbS, 'initial', deviceId, appId);
|
|
754
|
+
const launchHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
|
|
755
|
+
const launchElements = this.extractMaestroSelectors(launchHierarchy);
|
|
756
|
+
const launchTexts = this.extractAllTexts(launchHierarchy);
|
|
757
|
+
const launchUI = legacyElementsToUIElements(launchElements, '__pending__');
|
|
758
|
+
// Generic screen name — no app-specific assumption
|
|
759
|
+
const initialScreenName = 'Pantalla Inicial';
|
|
760
|
+
allScreens.push({
|
|
761
|
+
name: initialScreenName,
|
|
762
|
+
isErrorState: false,
|
|
763
|
+
screenshot: launchScreenshot,
|
|
764
|
+
elements: launchElements,
|
|
765
|
+
allTexts: launchTexts,
|
|
766
|
+
});
|
|
767
|
+
navigationSteps.push({ from: 'Inicio', to: initialScreenName, action: 'Lanzar app' });
|
|
768
|
+
console.log(chalk.green(` ✅ ${initialScreenName}: ${launchElements.length} elementos, ${launchScreenshot ? 'screenshot ✓' : 'sin screenshot'}`));
|
|
769
|
+
// ── EvidenceRecord ev_001 — App launch (or resume start) ─────────
|
|
770
|
+
// Phase 12: in resume mode, screen_origin reflects the human-managed
|
|
771
|
+
// setup and the result_description notes that prior steps weren't
|
|
772
|
+
// recorded. The action_type stays 'launch' (it IS the entry point of
|
|
773
|
+
// the corpus session, just not literally an app launch).
|
|
774
|
+
const ev001 = buildRecord({
|
|
775
|
+
sessionId: corpus.sessionId,
|
|
776
|
+
screen_origin: mode === 'resume' ? 'Inicio (manual)' : 'Inicio',
|
|
777
|
+
action_type: 'launch',
|
|
778
|
+
action_element: appId,
|
|
779
|
+
screenshot_before: null,
|
|
780
|
+
elements_before: [],
|
|
781
|
+
screen_destination: initialScreenName,
|
|
782
|
+
result_type: 'new_screen',
|
|
783
|
+
result_description: mode === 'resume'
|
|
784
|
+
? `Estado inicial provisto por humano (resume mode). Pasos manuales (PIN/2FA/SSO) no se registraron — Discovery captura desde la pantalla actual con ${launchElements.length} elementos.`
|
|
785
|
+
: `App lanzada — ${launchElements.length} elementos en pantalla inicial`,
|
|
786
|
+
screenshot_after: launchScreenshot,
|
|
787
|
+
elements_after: launchUI,
|
|
788
|
+
captured_by: 'autonomous',
|
|
789
|
+
capture_mode: mode === 'resume' ? 'resume' : 'autonomous',
|
|
790
|
+
});
|
|
791
|
+
addRecord(corpus, ev001);
|
|
792
|
+
// ── Step 2: Detect login form on initial screen ──────────────────
|
|
793
|
+
// Generic detection — works with any app that has 1+ EditText + 1+ Button.
|
|
794
|
+
// Phase 12: in resume mode the human is already past the login, so we
|
|
795
|
+
// skip the login form detection entirely (any EditText we see now is
|
|
796
|
+
// a regular form, e.g. transfer amount, not credentials).
|
|
797
|
+
const loginForm = detectLoginForm(launchUI);
|
|
798
|
+
// Tracking state for BFS exploration
|
|
799
|
+
let lastScreenName = initialScreenName;
|
|
800
|
+
let lastUI = launchUI;
|
|
801
|
+
let lastTexts = launchTexts;
|
|
802
|
+
let lastScreenshot = launchScreenshot;
|
|
803
|
+
let loggedIn = !loginForm; // If no login form, assume already in main flow
|
|
804
|
+
let isDummyCredentials = false;
|
|
805
|
+
if (loginForm) {
|
|
806
|
+
if (!username || !password) {
|
|
807
|
+
// If interactive callback is available, prompt the user first
|
|
808
|
+
if (options.onFormDetected) {
|
|
809
|
+
console.log(chalk.cyan('\n 🔐 Formulario de login detectado sin credenciales. Solicitando interacción con el usuario...'));
|
|
810
|
+
const credentialsForm = {
|
|
811
|
+
screenName: initialScreenName,
|
|
812
|
+
fields: [
|
|
813
|
+
{ id: loginForm.usernameId, label: loginForm.usernameLabel || 'Usuario / RUT' },
|
|
814
|
+
{ id: loginForm.passwordId, label: loginForm.passwordLabel || 'Contraseña / Clave' }
|
|
815
|
+
],
|
|
816
|
+
submitButtonId: loginForm.submitId,
|
|
817
|
+
submitButtonLabel: loginForm.submitLabel || 'Iniciar Sesión',
|
|
818
|
+
};
|
|
819
|
+
try {
|
|
820
|
+
const userCredentials = await options.onFormDetected(credentialsForm);
|
|
821
|
+
if (userCredentials && userCredentials[loginForm.usernameId] && userCredentials[loginForm.passwordId]) {
|
|
822
|
+
username = userCredentials[loginForm.usernameId];
|
|
823
|
+
password = userCredentials[loginForm.passwordId];
|
|
824
|
+
options.username = username;
|
|
825
|
+
options.password = password;
|
|
826
|
+
isDummyCredentials = false;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
catch (err) {
|
|
830
|
+
console.log(chalk.yellow(` ⚠️ Error solicitando credenciales interactivas: ${err.message}`));
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
// Fallback heuristic if credentials are still not provided (e.g. user declined or non-interactive)
|
|
834
|
+
if (!username || !password) {
|
|
835
|
+
isDummyCredentials = true;
|
|
836
|
+
const usernameLabelLower = (loginForm.usernameLabel || '').toLowerCase();
|
|
837
|
+
const passwordLabelLower = (loginForm.passwordLabel || '').toLowerCase();
|
|
838
|
+
// RUT / DNI detection -> clean numeric format (no hyphens/chars if numeric input expected)
|
|
839
|
+
username = /rut|run|dni|cedula|cédula|identi|nro|número|numero/i.test(usernameLabelLower)
|
|
840
|
+
? '123456789'
|
|
841
|
+
: 'user@example.com';
|
|
842
|
+
// PIN / Code detection -> numeric passcode
|
|
843
|
+
password = /pin|clave|passcode|codigo|código|pass|contrase|secret/i.test(passwordLabelLower)
|
|
844
|
+
? '123456'
|
|
845
|
+
: 'Password123';
|
|
846
|
+
options.username = username;
|
|
847
|
+
options.password = password;
|
|
848
|
+
console.log(chalk.yellow(` ℹ️ No se proporcionaron credenciales (--username / --password). ` +
|
|
849
|
+
`Usando credenciales ficticias heurísticas ("${username}" / "${password}") ` +
|
|
850
|
+
`para habilitar el botón y explorar los estados de error/validación.`));
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
console.log(chalk.cyan(` 🔐 Form de login detectado: ` +
|
|
854
|
+
`usuario="${loginForm.usernameId}"${loginForm.usernameLabel ? ` (${loginForm.usernameLabel})` : ''}, ` +
|
|
855
|
+
`password="${loginForm.passwordId}", ` +
|
|
856
|
+
`submit="${loginForm.submitId}"${loginForm.submitLabel ? ` (${loginForm.submitLabel})` : ''}`));
|
|
857
|
+
// ── Step 2a: Empty submit → validation error state ─────────────
|
|
858
|
+
this._progress(options, 'error_state', 'Submit vacío — capturando validaciones...');
|
|
859
|
+
console.log(chalk.gray(' 📸 Probando submit vacío para capturar errores de validación...'));
|
|
860
|
+
const emptySubmitFlow = `- tapOn:\n ${buildMaestroSelector(loginForm.submitId, isIos)}`;
|
|
861
|
+
const emptySubmit = this._runMaestroFlow(appId, [
|
|
862
|
+
emptySubmitFlow,
|
|
863
|
+
`- waitForAnimationToEnd:\n timeout: 1500`,
|
|
864
|
+
], maestroEnv).ok;
|
|
865
|
+
if (emptySubmit) {
|
|
193
866
|
await sleep(1500);
|
|
194
|
-
const
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
867
|
+
const emptyShot = await this._captureScreenshotMobile(adbS, 'empty_submit');
|
|
868
|
+
const emptyHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
|
|
869
|
+
const emptyEls = emptyHier ? this.extractMaestroSelectors(emptyHier) : [];
|
|
870
|
+
const emptyTxts = emptyHier ? this.extractAllTexts(emptyHier) : [];
|
|
871
|
+
const knownIds = new Set(launchElements.map(e => e.id));
|
|
872
|
+
const newEls = emptyEls.filter(e => !knownIds.has(e.id));
|
|
873
|
+
const newTxts = emptyTxts.filter(t => !launchTexts.includes(t));
|
|
874
|
+
if (newEls.length > 0 || newTxts.length > 0) {
|
|
875
|
+
const errName = `${initialScreenName} (Error — campos vacíos)`;
|
|
876
|
+
allScreens.push({
|
|
877
|
+
name: errName,
|
|
878
|
+
isErrorState: true,
|
|
879
|
+
screenshot: emptyShot,
|
|
880
|
+
elements: newEls,
|
|
881
|
+
allTexts: newTxts,
|
|
882
|
+
});
|
|
883
|
+
navigationSteps.push({ from: initialScreenName, to: errName, action: 'Submit vacío' });
|
|
884
|
+
const errList = newTxts.slice(0, 3).map(t => `"${t}"`).join(', ');
|
|
885
|
+
console.log(chalk.green(` ✅ Validación capturada: ${newEls.length} elementos / ${newTxts.length} textos${errList ? ` | ${errList}` : ''}`));
|
|
886
|
+
const ev002 = buildRecord({
|
|
887
|
+
sessionId: corpus.sessionId,
|
|
888
|
+
screen_origin: initialScreenName,
|
|
889
|
+
action_type: 'form_submit',
|
|
890
|
+
action_element: loginForm.submitId,
|
|
891
|
+
action_label: loginForm.submitLabel,
|
|
892
|
+
screenshot_before: launchScreenshot,
|
|
893
|
+
elements_before: legacyElementsToUIElements(launchElements, ev001.evidenceId),
|
|
894
|
+
input_data: { [loginForm.usernameId]: '', [loginForm.passwordId]: '' },
|
|
895
|
+
screen_destination: errName,
|
|
896
|
+
result_type: 'validation_state',
|
|
897
|
+
result_description: `Validación de campos vacíos — textos: ${errList || 'ninguno detectado'}`,
|
|
898
|
+
screenshot_after: emptyShot,
|
|
899
|
+
elements_after: legacyElementsToUIElements(newEls, '__pending__'),
|
|
900
|
+
captured_by: 'autonomous',
|
|
901
|
+
});
|
|
902
|
+
addRecord(corpus, ev002);
|
|
903
|
+
}
|
|
904
|
+
else {
|
|
905
|
+
console.log(chalk.yellow(' ⚠️ Submit vacío no produjo cambios visibles'));
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
// ── Step 2b: Login with credentials ────────────────────────────
|
|
909
|
+
if (username && password) {
|
|
910
|
+
this._progress(options, 'login', 'Autenticando con IDs detectados...');
|
|
911
|
+
console.log(chalk.gray(` 🔐 Login: usuario="${username}" en ${loginForm.usernameId} → submit ${loginForm.submitId}`));
|
|
912
|
+
const userSelector = buildMaestroSelector(loginForm.usernameId, isIos);
|
|
913
|
+
const passSelector = buildMaestroSelector(loginForm.passwordId, isIos);
|
|
914
|
+
const subSelector = buildMaestroSelector(loginForm.submitId, isIos);
|
|
915
|
+
this._runMaestroFlow(appId, [
|
|
916
|
+
// Do NOT use clearState: true here — the app is already at the login screen.
|
|
917
|
+
// clearState resets the entire app, takes 8+ seconds for banking apps, and
|
|
918
|
+
// triggers security checks (SSL pinning, root detection) that delay the UI.
|
|
919
|
+
// Instead, just bring the app to foreground (if it went to background) and
|
|
920
|
+
// interact with the fields that are already visible.
|
|
921
|
+
`- launchApp`,
|
|
922
|
+
`- waitForAnimationToEnd:\n timeout: 3000`,
|
|
923
|
+
// Scroll up to ensure the login form is fully visible from the top
|
|
924
|
+
`- scrollUntilVisible:\n element:\n ${userSelector.replace(/\n\s+/g, '\n ')}\n direction: UP`,
|
|
925
|
+
`- tapOn:\n ${userSelector}`,
|
|
926
|
+
`- eraseText: 50`,
|
|
927
|
+
`- inputText: "${username}"`,
|
|
928
|
+
`- waitForAnimationToEnd:\n timeout: 2000`,
|
|
929
|
+
`- hideKeyboard`,
|
|
930
|
+
// After keyboard dismissal the layout reflows — scroll to show password field
|
|
931
|
+
`- waitForAnimationToEnd:\n timeout: 2000`,
|
|
932
|
+
`- tapOn:\n ${passSelector}`,
|
|
933
|
+
`- eraseText: 50`,
|
|
934
|
+
`- inputText: "${password}"`,
|
|
935
|
+
`- waitForAnimationToEnd:\n timeout: 2000`,
|
|
936
|
+
`- hideKeyboard`,
|
|
937
|
+
`- waitForAnimationToEnd:\n timeout: 2000`,
|
|
938
|
+
`- tapOn:\n ${subSelector}`,
|
|
939
|
+
`- waitForAnimationToEnd:\n timeout: 5000`,
|
|
204
940
|
], maestroEnv);
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
941
|
+
// ── Verify app is still in foreground after login flow ─────────────
|
|
942
|
+
// launchApp clearState: true can leave the home screen if the app
|
|
943
|
+
// takes too long to initialize (SSL pinning, root detection, etc.)
|
|
944
|
+
if (!isIos) {
|
|
945
|
+
const inFgAfterLogin = await this._ensureAppInForeground(adbS, appId, options);
|
|
946
|
+
if (!inFgAfterLogin) {
|
|
947
|
+
console.log(chalk.yellow(' ⚠️ App no confirmada en primer plano post-login — el análisis de éxito puede ser incorrecto.'));
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
await sleep(3000);
|
|
951
|
+
let postLoginHier = null;
|
|
952
|
+
try {
|
|
953
|
+
postLoginHier = await this._captureHierarchy(maestroEnv, options.deviceId);
|
|
954
|
+
}
|
|
955
|
+
catch { }
|
|
956
|
+
const postLoginEls = postLoginHier ? this.extractMaestroSelectors(postLoginHier) : [];
|
|
957
|
+
const postLoginUI = legacyElementsToUIElements(postLoginEls, '__pending__');
|
|
958
|
+
const postLoginTxts = postLoginHier ? this.extractAllTexts(postLoginHier) : [];
|
|
959
|
+
// GENERIC success detection: did the screen change?
|
|
960
|
+
const change = detectScreenChange(launchUI, postLoginUI);
|
|
961
|
+
const postLoginShot = await this._captureScreenshotMobile(adbS, 'post_login');
|
|
962
|
+
if (change.changed && isDummyCredentials) {
|
|
963
|
+
// ── Dummy credentials: capture error state, then reset to initial screen ──
|
|
964
|
+
const errScreenName = `${initialScreenName} (Error — credenciales inválidas)`;
|
|
965
|
+
allScreens.push({
|
|
966
|
+
name: errScreenName,
|
|
967
|
+
isErrorState: true,
|
|
968
|
+
screenshot: postLoginShot,
|
|
969
|
+
elements: postLoginEls,
|
|
970
|
+
allTexts: postLoginTxts,
|
|
971
|
+
});
|
|
972
|
+
navigationSteps.push({ from: initialScreenName, to: errScreenName, action: 'Login con credenciales ficticias' });
|
|
973
|
+
console.log(chalk.green(` ✅ Credenciales ficticias: pantalla cambió ` +
|
|
974
|
+
`(${change.newIdsCount} nuevos elementos, similitud ${change.similarity.toFixed(2)}) — ` +
|
|
975
|
+
`capturado como estado de error. Regresando a pantalla inicial para BFS pre-auth.`));
|
|
976
|
+
const ev003 = buildRecord({
|
|
977
|
+
sessionId: corpus.sessionId,
|
|
978
|
+
screen_origin: initialScreenName,
|
|
979
|
+
action_type: 'form_submit',
|
|
980
|
+
action_element: loginForm.submitId,
|
|
981
|
+
action_label: loginForm.submitLabel,
|
|
982
|
+
screenshot_before: launchScreenshot,
|
|
983
|
+
elements_before: legacyElementsToUIElements(launchElements, ev001.evidenceId),
|
|
984
|
+
input_data: { [loginForm.usernameId]: username, [loginForm.passwordId]: '***' },
|
|
985
|
+
screen_destination: errScreenName,
|
|
986
|
+
result_type: 'validation_state',
|
|
987
|
+
result_description: `Credenciales ficticias rechazadas — ${postLoginEls.length} elementos, ${change.newIdsCount} nuevos vs pantalla inicial`,
|
|
988
|
+
screenshot_after: postLoginShot,
|
|
989
|
+
elements_after: postLoginUI,
|
|
990
|
+
captured_by: 'autonomous',
|
|
991
|
+
});
|
|
992
|
+
addRecord(corpus, ev003);
|
|
993
|
+
// Reset app state to return to the initial login screen for BFS
|
|
994
|
+
this._progress(options, 'reset', 'Regresando a pantalla inicial para exploración BFS...');
|
|
995
|
+
if (!isIos) {
|
|
996
|
+
try {
|
|
997
|
+
execSync(`${adbS} shell am force-stop ${appId}`, { stdio: 'ignore' });
|
|
998
|
+
}
|
|
999
|
+
catch { }
|
|
1000
|
+
try {
|
|
1001
|
+
execSync(`${adbS} shell pm clear ${appId}`, { stdio: 'ignore' });
|
|
225
1002
|
}
|
|
1003
|
+
catch { }
|
|
1004
|
+
await sleep(1000);
|
|
1005
|
+
execSync(`${adbS} shell monkey -p ${appId} -c android.intent.category.LAUNCHER 1`, { stdio: 'ignore' });
|
|
1006
|
+
}
|
|
1007
|
+
else {
|
|
1008
|
+
this._runMaestroFlow(appId, [
|
|
1009
|
+
`- launchApp:\n clearState: true`,
|
|
1010
|
+
`- waitForAnimationToEnd:\n timeout: 3000`,
|
|
1011
|
+
], maestroEnv);
|
|
226
1012
|
}
|
|
1013
|
+
await sleep(isIos ? 5000 : 4500);
|
|
1014
|
+
// Re-capture initial screen elements for BFS
|
|
1015
|
+
const resetHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
|
|
1016
|
+
if (resetHier) {
|
|
1017
|
+
const resetEls = this.extractMaestroSelectors(resetHier);
|
|
1018
|
+
const resetUI = legacyElementsToUIElements(resetEls, '__pending__');
|
|
1019
|
+
const resetTxts = this.extractAllTexts(resetHier);
|
|
1020
|
+
const resetShot = await this._captureScreenshotMobile(isIos ? 'maestro' : adbS, 'reset_after_dummy');
|
|
1021
|
+
lastScreenName = initialScreenName;
|
|
1022
|
+
lastUI = resetUI;
|
|
1023
|
+
lastTexts = resetTxts;
|
|
1024
|
+
lastScreenshot = resetShot;
|
|
1025
|
+
}
|
|
1026
|
+
// loggedIn stays false → BFS will explore pre-auth buttons
|
|
1027
|
+
}
|
|
1028
|
+
else if (change.changed) {
|
|
1029
|
+
loggedIn = true;
|
|
1030
|
+
const authScreenName = 'Pantalla Autenticada';
|
|
1031
|
+
allScreens.push({
|
|
1032
|
+
name: authScreenName,
|
|
1033
|
+
isErrorState: false,
|
|
1034
|
+
screenshot: postLoginShot,
|
|
1035
|
+
elements: postLoginEls,
|
|
1036
|
+
allTexts: postLoginTxts,
|
|
1037
|
+
});
|
|
1038
|
+
navigationSteps.push({ from: initialScreenName, to: authScreenName, action: 'Login con credenciales válidas' });
|
|
1039
|
+
console.log(chalk.green(` ✅ Login exitoso: pantalla cambió ` +
|
|
1040
|
+
`(${change.newIdsCount} nuevos elementos, similitud ${change.similarity.toFixed(2)}) — ` +
|
|
1041
|
+
`${postLoginEls.length} elementos en pantalla autenticada`));
|
|
1042
|
+
const ev003 = buildRecord({
|
|
1043
|
+
sessionId: corpus.sessionId,
|
|
1044
|
+
screen_origin: initialScreenName,
|
|
1045
|
+
action_type: 'form_submit',
|
|
1046
|
+
action_element: loginForm.submitId,
|
|
1047
|
+
action_label: loginForm.submitLabel,
|
|
1048
|
+
screenshot_before: launchScreenshot,
|
|
1049
|
+
elements_before: legacyElementsToUIElements(launchElements, ev001.evidenceId),
|
|
1050
|
+
input_data: { [loginForm.usernameId]: username, [loginForm.passwordId]: '***' },
|
|
1051
|
+
screen_destination: authScreenName,
|
|
1052
|
+
result_type: 'new_screen',
|
|
1053
|
+
result_description: `Login exitoso — ${postLoginEls.length} elementos visibles, ${change.newIdsCount} nuevos vs pantalla inicial`,
|
|
1054
|
+
screenshot_after: postLoginShot,
|
|
1055
|
+
elements_after: postLoginUI,
|
|
1056
|
+
captured_by: 'autonomous',
|
|
1057
|
+
});
|
|
1058
|
+
addRecord(corpus, ev003);
|
|
1059
|
+
lastScreenName = authScreenName;
|
|
1060
|
+
lastUI = postLoginUI;
|
|
1061
|
+
lastTexts = postLoginTxts;
|
|
1062
|
+
lastScreenshot = postLoginShot;
|
|
227
1063
|
}
|
|
228
1064
|
else {
|
|
229
|
-
console.log(chalk.yellow(
|
|
1065
|
+
console.log(chalk.yellow(` ⚠️ Login no produjo cambio de pantalla (similitud ${change.similarity.toFixed(2)}). ` +
|
|
1066
|
+
`Posibles causas: credenciales rechazadas, paso adicional (PIN/2FA), o app aún cargando.`));
|
|
1067
|
+
const ev003 = buildRecord({
|
|
1068
|
+
sessionId: corpus.sessionId,
|
|
1069
|
+
screen_origin: initialScreenName,
|
|
1070
|
+
action_type: 'form_submit',
|
|
1071
|
+
action_element: loginForm.submitId,
|
|
1072
|
+
action_label: loginForm.submitLabel,
|
|
1073
|
+
screenshot_before: launchScreenshot,
|
|
1074
|
+
elements_before: legacyElementsToUIElements(launchElements, ev001.evidenceId),
|
|
1075
|
+
input_data: { [loginForm.usernameId]: username, [loginForm.passwordId]: '***' },
|
|
1076
|
+
screen_destination: initialScreenName,
|
|
1077
|
+
result_type: 'partial_flow',
|
|
1078
|
+
result_description: `Login no produjo cambio — similitud ${change.similarity.toFixed(2)} (credenciales rechazadas, 2FA, o flujo adicional)`,
|
|
1079
|
+
screenshot_after: postLoginShot,
|
|
1080
|
+
elements_after: postLoginUI,
|
|
1081
|
+
captured_by: 'autonomous',
|
|
1082
|
+
});
|
|
1083
|
+
addRecord(corpus, ev003);
|
|
230
1084
|
}
|
|
231
1085
|
}
|
|
1086
|
+
}
|
|
1087
|
+
else if (mode === 'resume') {
|
|
1088
|
+
console.log(chalk.cyan(' 🤝 Modo resume: detectando posibles formularios en el estado actual...'));
|
|
1089
|
+
}
|
|
1090
|
+
else {
|
|
1091
|
+
console.log(chalk.gray(' ℹ️ No se detectó form de login (la app abre directo en su flujo principal)'));
|
|
1092
|
+
}
|
|
1093
|
+
// ── Step 3: Generic BFS over navigable buttons ───────────────────
|
|
1094
|
+
// For each clickable button on the current screen, tap it and observe
|
|
1095
|
+
// what happens. Works on both pre-auth (login screen) and post-auth
|
|
1096
|
+
// screens — rankNavigableButtons already excludes EditText and other
|
|
1097
|
+
// form fields, so only actual navigation buttons are explored.
|
|
1098
|
+
// 1. Tap → capture new screen state
|
|
1099
|
+
// 2. If we landed back on the login screen → record logout, halt BFS
|
|
1100
|
+
// 3. detectForm on the new screen
|
|
1101
|
+
// 4. If form: empty submit FIRST (capture validation evidence)
|
|
1102
|
+
// 5. Then prompt the tester via onFormDetected (real flow)
|
|
1103
|
+
// 6. Go back to parent screen (multiple `back` if needed)
|
|
1104
|
+
if (lastUI.length > 0) {
|
|
1105
|
+
const visited = new Set();
|
|
1106
|
+
const maxButtons = 8; // safety cap on BFS breadth per session
|
|
1107
|
+
// When not logged in, exclude login form elements from BFS candidates
|
|
1108
|
+
// so we don't accidentally tap the submit button without credentials.
|
|
1109
|
+
if (loginForm && !loggedIn) {
|
|
1110
|
+
visited.add(loginForm.usernameId);
|
|
1111
|
+
visited.add(loginForm.passwordId);
|
|
1112
|
+
visited.add(loginForm.submitId);
|
|
1113
|
+
}
|
|
1114
|
+
const candidates = rankNavigableButtons(lastUI, visited, appId).slice(0, maxButtons);
|
|
1115
|
+
const parentName = lastScreenName;
|
|
1116
|
+
const parentUI = lastUI;
|
|
1117
|
+
const parentShot = lastScreenshot;
|
|
1118
|
+
const parentTexts = lastTexts;
|
|
1119
|
+
// Track visited screens by element-ID fingerprint so we don't re-explore
|
|
1120
|
+
// the same form when two buttons lead to the same destination
|
|
1121
|
+
// (e.g. TRANSFERIR and PAGAR both opening "Nueva Transferencia").
|
|
1122
|
+
// We seed the set with launch + parent screens.
|
|
1123
|
+
const visitedScreens = [
|
|
1124
|
+
{ name: initialScreenName, ui: launchUI },
|
|
1125
|
+
{ name: parentName, ui: parentUI },
|
|
1126
|
+
];
|
|
1127
|
+
if (candidates.length === 0) {
|
|
1128
|
+
console.log(chalk.gray(' ℹ️ Sin botones navegables para BFS'));
|
|
1129
|
+
}
|
|
232
1130
|
else {
|
|
233
|
-
console.log(chalk.
|
|
1131
|
+
console.log(chalk.cyan(`\n 🌐 BFS sobre ${candidates.length} botón(es) en "${parentName}"${!loggedIn ? ' (pre-auth)' : ''}...`));
|
|
1132
|
+
}
|
|
1133
|
+
for (const btn of candidates) {
|
|
1134
|
+
if (visited.has(btn.elementId))
|
|
1135
|
+
continue;
|
|
1136
|
+
visited.add(btn.elementId);
|
|
1137
|
+
const btnLabel = btn.visibleText || btn.elementId;
|
|
1138
|
+
this._progress(options, 'screen', `Explorando "${btnLabel}"`);
|
|
1139
|
+
console.log(chalk.gray(`\n 👆 Tap en "${btnLabel}" (${btn.elementId})...`));
|
|
1140
|
+
const bfsSelector = buildMaestroSelector(btn.elementId, isIos);
|
|
1141
|
+
const tapResult = this._runMaestroFlow(appId, [
|
|
1142
|
+
`- tapOn:\n ${bfsSelector}`,
|
|
1143
|
+
`- waitForAnimationToEnd:\n timeout: 3000`,
|
|
1144
|
+
], maestroEnv);
|
|
1145
|
+
if (!tapResult.ok) {
|
|
1146
|
+
console.log(chalk.yellow(` ⚠️ Tap en ${btn.elementId} falló — saltando`));
|
|
1147
|
+
continue;
|
|
1148
|
+
}
|
|
1149
|
+
await sleep(1500);
|
|
1150
|
+
const tapHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
|
|
1151
|
+
if (!tapHier) {
|
|
1152
|
+
console.log(chalk.yellow(` ⚠️ Sin jerarquía post-tap — saltando`));
|
|
1153
|
+
continue;
|
|
1154
|
+
}
|
|
1155
|
+
const tapEls = this.extractMaestroSelectors(tapHier);
|
|
1156
|
+
const tapUI = legacyElementsToUIElements(tapEls, '__pending__');
|
|
1157
|
+
const tapTxts = this.extractAllTexts(tapHier);
|
|
1158
|
+
const tapChange = detectScreenChange(parentUI, tapUI);
|
|
1159
|
+
const tapShot = await this._captureScreenshotMobile(adbS, `tap_${btn.elementId}`);
|
|
1160
|
+
if (!tapChange.changed) {
|
|
1161
|
+
console.log(chalk.gray(` ℹ️ Tap no cambió la pantalla (similitud ${tapChange.similarity.toFixed(2)}) — siguiente`));
|
|
1162
|
+
continue;
|
|
1163
|
+
}
|
|
1164
|
+
// ── Logout detection ────────────────────────────────────────
|
|
1165
|
+
// If the new screen looks like the original login screen, the user
|
|
1166
|
+
// logged out. Record the navigation and halt BFS — exploring further
|
|
1167
|
+
// would re-prompt the tester for credentials they already provided.
|
|
1168
|
+
if (loginForm && isSameScreen(tapUI, launchUI, 0.7)) {
|
|
1169
|
+
console.log(chalk.cyan(` 🚪 "${btnLabel}" cerró sesión (regresó a la pantalla inicial). BFS detenido.`));
|
|
1170
|
+
navigationSteps.push({ from: parentName, to: initialScreenName, action: `Tap ${btn.elementId}` });
|
|
1171
|
+
const logoutEv = buildRecord({
|
|
1172
|
+
sessionId: corpus.sessionId,
|
|
1173
|
+
screen_origin: parentName,
|
|
1174
|
+
action_type: 'tap',
|
|
1175
|
+
action_element: btn.elementId,
|
|
1176
|
+
action_label: btn.visibleText,
|
|
1177
|
+
screenshot_before: parentShot,
|
|
1178
|
+
elements_before: parentUI,
|
|
1179
|
+
screen_destination: initialScreenName,
|
|
1180
|
+
result_type: 'session_end',
|
|
1181
|
+
result_description: `Cierre de sesión — la app regresó a la pantalla inicial (similitud ${tapChange.similarity.toFixed(2)})`,
|
|
1182
|
+
screenshot_after: tapShot,
|
|
1183
|
+
elements_after: tapUI,
|
|
1184
|
+
captured_by: 'autonomous',
|
|
1185
|
+
});
|
|
1186
|
+
addRecord(corpus, logoutEv);
|
|
1187
|
+
break; // halt BFS — re-running Discovery will re-login
|
|
1188
|
+
}
|
|
1189
|
+
// ── Duplicate-screen detection ──────────────────────────────
|
|
1190
|
+
// If tapping this button leads to a screen we've already explored
|
|
1191
|
+
// (e.g. PAGAR opens the same form as TRANSFERIR), we still want to
|
|
1192
|
+
// capture independent evidence for THIS flow — the entry button
|
|
1193
|
+
// matters even if the destination UI looks identical:
|
|
1194
|
+
// - Same screen, same data, same result → flows are equivalent
|
|
1195
|
+
// - Same screen, same data, different result → real divergence
|
|
1196
|
+
// (different backend, different validation, mock bug, etc.)
|
|
1197
|
+
// We skip the tester re-prompt (data already provided) but reuse
|
|
1198
|
+
// it to execute the form again and record the result.
|
|
1199
|
+
const duplicateOf = visitedScreens.find(s => isSameScreen(tapUI, s.ui, 0.7));
|
|
1200
|
+
if (duplicateOf) {
|
|
1201
|
+
console.log(chalk.gray(` 🔁 "${btnLabel}" lleva a "${duplicateOf.name}" (ya explorada).`));
|
|
1202
|
+
navigationSteps.push({ from: parentName, to: duplicateOf.name, action: `Tap ${btn.elementId}` });
|
|
1203
|
+
// Record the navigation itself
|
|
1204
|
+
const dupEv = buildRecord({
|
|
1205
|
+
sessionId: corpus.sessionId,
|
|
1206
|
+
screen_origin: parentName,
|
|
1207
|
+
action_type: 'tap',
|
|
1208
|
+
action_element: btn.elementId,
|
|
1209
|
+
action_label: btn.visibleText,
|
|
1210
|
+
screenshot_before: parentShot,
|
|
1211
|
+
elements_before: parentUI,
|
|
1212
|
+
screen_destination: duplicateOf.name,
|
|
1213
|
+
result_type: 'duplicate_route',
|
|
1214
|
+
result_description: `Navegación al mismo destino que otro botón — alias de "${duplicateOf.name}" (similitud ${tapChange.similarity.toFixed(2)})`,
|
|
1215
|
+
screenshot_after: tapShot,
|
|
1216
|
+
elements_after: tapUI,
|
|
1217
|
+
captured_by: 'autonomous',
|
|
1218
|
+
});
|
|
1219
|
+
addRecord(corpus, dupEv);
|
|
1220
|
+
// ── Re-execute the form using previously-captured data ──────
|
|
1221
|
+
// Look up the most recent interactive form_submit on this screen
|
|
1222
|
+
// that has real input data (not just empty validation).
|
|
1223
|
+
const screenForm = detectForm(tapUI, duplicateOf.name);
|
|
1224
|
+
const prevForm = [...corpus.records].reverse().find(r => r.screen_origin === duplicateOf.name &&
|
|
1225
|
+
r.action_type === 'form_submit' &&
|
|
1226
|
+
r.captured_by === 'interactive' &&
|
|
1227
|
+
r.input_data &&
|
|
1228
|
+
Object.values(r.input_data).some(v => v && v !== '***'));
|
|
1229
|
+
if (screenForm && prevForm && prevForm.input_data) {
|
|
1230
|
+
console.log(chalk.cyan(` 🔄 Re-ejecutando el form con los datos previamente provistos ` +
|
|
1231
|
+
`(${Object.keys(prevForm.input_data).length} campos) para capturar ` +
|
|
1232
|
+
`evidencia independiente del flujo "${btnLabel}".`));
|
|
1233
|
+
const fillSteps = [];
|
|
1234
|
+
for (const [fid, val] of Object.entries(prevForm.input_data)) {
|
|
1235
|
+
if (val === '***' || !val)
|
|
1236
|
+
continue;
|
|
1237
|
+
const selector = buildMaestroSelector(fid, isIos);
|
|
1238
|
+
fillSteps.push(`- tapOn:\n ${selector}`, `- eraseText: 50`, `- inputText: "${val}"`, `- hideKeyboard`);
|
|
1239
|
+
}
|
|
1240
|
+
const subSelectorAlias = buildMaestroSelector(screenForm.submitButtonId, isIos);
|
|
1241
|
+
fillSteps.push(`- tapOn:\n ${subSelectorAlias}`, `- waitForAnimationToEnd:\n timeout: 5000`);
|
|
1242
|
+
const formRes = this._runMaestroFlow(appId, fillSteps, maestroEnv);
|
|
1243
|
+
await sleep(2500);
|
|
1244
|
+
const aliasShot = await this._captureScreenshotMobile(adbS, `alias_${btn.elementId}`);
|
|
1245
|
+
const aliasHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
|
|
1246
|
+
const aliasEls = aliasHier ? this.extractMaestroSelectors(aliasHier) : [];
|
|
1247
|
+
const aliasUI = legacyElementsToUIElements(aliasEls, '__pending__');
|
|
1248
|
+
const aliasTxts = aliasHier ? this.extractAllTexts(aliasHier) : [];
|
|
1249
|
+
const successText = aliasTxts.find(t => /comprobante|éxito|exitosa|confirmado|listo|aprobad/i.test(t));
|
|
1250
|
+
const isSuccess = !!successText && formRes.ok;
|
|
1251
|
+
// Compare this run's result to the previous run's result.
|
|
1252
|
+
// Normalize previous result_type so legacy values still match the new enum.
|
|
1253
|
+
const prevNormalized = normalizeResultType(prevForm.result_type);
|
|
1254
|
+
const sameAsOriginal = (isSuccess && prevNormalized === 'new_screen') ||
|
|
1255
|
+
(formRes.ok && prevNormalized === 'new_screen') ||
|
|
1256
|
+
(!formRes.ok && (prevNormalized === 'partial_flow' || prevNormalized === 'error_state'));
|
|
1257
|
+
const flowTag = `[vía ${btnLabel}]`;
|
|
1258
|
+
let aliasDest;
|
|
1259
|
+
let aliasResult;
|
|
1260
|
+
let aliasDesc;
|
|
1261
|
+
if (isSuccess) {
|
|
1262
|
+
aliasDest = `${duplicateOf.name} — Éxito ${flowTag}`;
|
|
1263
|
+
aliasResult = 'new_screen';
|
|
1264
|
+
aliasDesc = `Éxito por flujo "${btnLabel}" — texto: "${successText}". ${sameAsOriginal ? 'Comportamiento idéntico al flujo original.' : 'Difiere del flujo original — revisar.'}`;
|
|
1265
|
+
console.log(chalk.green(` ✅ Flujo "${btnLabel}" exitoso ${sameAsOriginal ? '(igual al original)' : '(¡diferente al original!)'}`));
|
|
1266
|
+
}
|
|
1267
|
+
else if (!formRes.ok) {
|
|
1268
|
+
aliasDest = `${duplicateOf.name} — Submit incompleto ${flowTag}`;
|
|
1269
|
+
aliasResult = 'partial_flow';
|
|
1270
|
+
aliasDesc = `Maestro reportó fallo en flujo "${btnLabel}". Pantalla: ${aliasTxts.slice(0, 3).join(' | ') || '(sin texto)'}. ${sameAsOriginal ? 'Mismo modo de fallo que el flujo original.' : 'Modo de fallo difiere del original.'}`;
|
|
1271
|
+
console.log(chalk.yellow(` ⚠️ Flujo "${btnLabel}" falló ${sameAsOriginal ? '(igual al original)' : '(¡diferente al original!)'}`));
|
|
1272
|
+
}
|
|
1273
|
+
else {
|
|
1274
|
+
aliasDest = `${duplicateOf.name} — Resultado ${flowTag}`;
|
|
1275
|
+
aliasResult = 'new_screen';
|
|
1276
|
+
aliasDesc = `Submit del flujo "${btnLabel}" navegó. ${sameAsOriginal ? 'Comportamiento equivalente al original.' : 'Difiere del original.'}`;
|
|
1277
|
+
console.log(chalk.green(` ✅ Flujo "${btnLabel}" navegó ${sameAsOriginal ? '(igual al original)' : '(¡diferente al original!)'}`));
|
|
1278
|
+
}
|
|
1279
|
+
allScreens.push({
|
|
1280
|
+
name: aliasDest,
|
|
1281
|
+
isErrorState: aliasResult === 'partial_flow',
|
|
1282
|
+
screenshot: aliasShot,
|
|
1283
|
+
elements: aliasEls,
|
|
1284
|
+
allTexts: aliasTxts,
|
|
1285
|
+
});
|
|
1286
|
+
navigationSteps.push({ from: duplicateOf.name, to: aliasDest, action: `Tap ${screenForm.submitButtonId}` });
|
|
1287
|
+
const aliasEv = buildRecord({
|
|
1288
|
+
sessionId: corpus.sessionId,
|
|
1289
|
+
screen_origin: duplicateOf.name,
|
|
1290
|
+
action_type: 'form_submit',
|
|
1291
|
+
action_element: screenForm.submitButtonId,
|
|
1292
|
+
action_label: screenForm.submitButtonLabel,
|
|
1293
|
+
screenshot_before: tapShot,
|
|
1294
|
+
elements_before: tapUI,
|
|
1295
|
+
input_data: prevForm.input_data,
|
|
1296
|
+
screen_destination: aliasDest,
|
|
1297
|
+
result_type: aliasResult,
|
|
1298
|
+
result_description: aliasDesc,
|
|
1299
|
+
screenshot_after: aliasShot,
|
|
1300
|
+
elements_after: aliasUI,
|
|
1301
|
+
captured_by: 'autonomous', // re-run by the engine, not prompted
|
|
1302
|
+
});
|
|
1303
|
+
addRecord(corpus, aliasEv);
|
|
1304
|
+
}
|
|
1305
|
+
else if (screenForm) {
|
|
1306
|
+
console.log(chalk.gray(` ℹ️ Sin datos previos del form para reusar — saltando re-ejecución.`));
|
|
1307
|
+
}
|
|
1308
|
+
// Return to parent for the next BFS button
|
|
1309
|
+
const returnedToParent = await this._smartBack(appId, maestroEnv, options.deviceId, parentUI, launchUI, loginForm, parentName);
|
|
1310
|
+
if (!returnedToParent) {
|
|
1311
|
+
console.log(chalk.yellow(` ⚠️ No se pudo regresar a "${parentName}". BFS detenido.`));
|
|
1312
|
+
break;
|
|
1313
|
+
}
|
|
1314
|
+
continue; // next BFS button
|
|
1315
|
+
}
|
|
1316
|
+
// ── Smart screen name from visible content ──────────────────
|
|
1317
|
+
const screenName = inferScreenName(tapTxts, [...parentTexts, ...launchTexts], // exclude texts seen on parent + launch
|
|
1318
|
+
`Pantalla tras "${btnLabel}"`);
|
|
1319
|
+
// ── Phase 11 — Incremental Discovery: should we capture this screen? ──
|
|
1320
|
+
// Compute the fingerprint of the screen we just landed on, then ask
|
|
1321
|
+
// shouldCapture() whether the baseline already covers it. If yes,
|
|
1322
|
+
// we record a lightweight navigation record and skip the deep
|
|
1323
|
+
// exploration of the form / empty-submit / interactive prompt steps.
|
|
1324
|
+
// Spec: DISCOVERY_SPEC.md § 11.1 + § 11.2
|
|
1325
|
+
let skipDeepExploration = false;
|
|
1326
|
+
if (mode === 'incremental' && baselines) {
|
|
1327
|
+
const tapTextsForFp = tapEls.map(e => e.text).filter(t => !!t && t !== 'N/A');
|
|
1328
|
+
// Simple deterministic fingerprint substitute (full algo lives in
|
|
1329
|
+
// evidence-corpus; here we use the same input to stay consistent)
|
|
1330
|
+
const { screenFingerprint } = await import('./evidence-corpus.js');
|
|
1331
|
+
const currentFp = screenFingerprint(legacyElementsToUIElements(tapEls, '__pending__'), tapTextsForFp);
|
|
1332
|
+
const matchedBaseline = findBaselineForFingerprint(baselines, currentFp);
|
|
1333
|
+
const decision = shouldCapture(currentFp, matchedBaseline);
|
|
1334
|
+
if (!decision.capture) {
|
|
1335
|
+
skipDeepExploration = true;
|
|
1336
|
+
incrementalSkipCount++;
|
|
1337
|
+
console.log(chalk.cyan(` ♻️ "${screenName}" reusa baseline verified (skip deep exploration). ` +
|
|
1338
|
+
`Razón: ${decision.reason}`));
|
|
1339
|
+
navigationSteps.push({ from: parentName, to: screenName, action: `Tap ${btn.elementId} [reused]` });
|
|
1340
|
+
const reuseEv = buildRecord({
|
|
1341
|
+
sessionId: corpus.sessionId,
|
|
1342
|
+
screen_origin: parentName,
|
|
1343
|
+
action_type: 'tap',
|
|
1344
|
+
action_element: btn.elementId,
|
|
1345
|
+
action_label: btn.visibleText,
|
|
1346
|
+
screenshot_before: parentShot,
|
|
1347
|
+
elements_before: parentUI,
|
|
1348
|
+
screen_destination: screenName,
|
|
1349
|
+
result_type: 'new_screen',
|
|
1350
|
+
result_description: `Reused verified baseline (incremental mode). matched_baseline=${decision.matched_baseline_id}`,
|
|
1351
|
+
screenshot_after: tapShot,
|
|
1352
|
+
elements_after: tapUI,
|
|
1353
|
+
captured_by: 'autonomous',
|
|
1354
|
+
});
|
|
1355
|
+
addRecord(corpus, reuseEv);
|
|
1356
|
+
}
|
|
1357
|
+
else {
|
|
1358
|
+
incrementalCaptureCount++;
|
|
1359
|
+
console.log(chalk.gray(` 📸 "${screenName}" requiere captura — ${decision.reason}`));
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
if (skipDeepExploration) {
|
|
1363
|
+
// Still go back to parent for the next BFS iteration (skipping
|
|
1364
|
+
// the empty-submit + onFormDetected + recursive logic below)
|
|
1365
|
+
this._runMaestroFlow(appId, [
|
|
1366
|
+
`- back`,
|
|
1367
|
+
`- waitForAnimationToEnd:\n timeout: 1500`,
|
|
1368
|
+
], maestroEnv);
|
|
1369
|
+
await sleep(1000);
|
|
1370
|
+
continue;
|
|
1371
|
+
}
|
|
1372
|
+
allScreens.push({
|
|
1373
|
+
name: screenName,
|
|
1374
|
+
isErrorState: false,
|
|
1375
|
+
screenshot: tapShot,
|
|
1376
|
+
elements: tapEls,
|
|
1377
|
+
allTexts: tapTxts,
|
|
1378
|
+
});
|
|
1379
|
+
// Mark this new screen as visited for future BFS iterations
|
|
1380
|
+
visitedScreens.push({ name: screenName, ui: tapUI });
|
|
1381
|
+
navigationSteps.push({ from: parentName, to: screenName, action: `Tap ${btn.elementId}` });
|
|
1382
|
+
console.log(chalk.green(` ✅ Nueva pantalla "${screenName}": ${tapEls.length} elementos (${tapChange.newIdsCount} nuevos)`));
|
|
1383
|
+
const tapEv = buildRecord({
|
|
1384
|
+
sessionId: corpus.sessionId,
|
|
1385
|
+
screen_origin: parentName,
|
|
1386
|
+
action_type: 'tap',
|
|
1387
|
+
action_element: btn.elementId,
|
|
1388
|
+
action_label: btn.visibleText,
|
|
1389
|
+
screenshot_before: parentShot,
|
|
1390
|
+
elements_before: parentUI,
|
|
1391
|
+
screen_destination: screenName,
|
|
1392
|
+
result_type: 'new_screen',
|
|
1393
|
+
result_description: `Navegación: ${tapChange.newIdsCount} elementos nuevos, similitud ${tapChange.similarity.toFixed(2)}`,
|
|
1394
|
+
screenshot_after: tapShot,
|
|
1395
|
+
elements_after: tapUI,
|
|
1396
|
+
captured_by: 'autonomous',
|
|
1397
|
+
});
|
|
1398
|
+
addRecord(corpus, tapEv);
|
|
1399
|
+
// ── Step 4: Detect form on the new screen ────────────────────
|
|
1400
|
+
const screenForm = detectForm(tapUI, screenName);
|
|
1401
|
+
// ── Step 4a: Empty submit FIRST → captures validation evidence
|
|
1402
|
+
// Done before the tester prompt so validations come from a clean
|
|
1403
|
+
// form state, not after a successful real submit (which would have
|
|
1404
|
+
// already navigated us elsewhere).
|
|
1405
|
+
if (screenForm) {
|
|
1406
|
+
this._progress(options, 'error_state', `Submit vacío en "${screenName}"`);
|
|
1407
|
+
console.log(chalk.gray(` 📸 Probando validación en "${screenName}" (submit vacío)...`));
|
|
1408
|
+
const emptySelector = buildMaestroSelector(screenForm.submitButtonId, isIos);
|
|
1409
|
+
const emptyFormOk = this._runMaestroFlow(appId, [
|
|
1410
|
+
`- tapOn:\n ${emptySelector}`,
|
|
1411
|
+
`- waitForAnimationToEnd:\n timeout: 1500`,
|
|
1412
|
+
], maestroEnv).ok;
|
|
1413
|
+
if (emptyFormOk) {
|
|
1414
|
+
await sleep(1500);
|
|
1415
|
+
const errHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
|
|
1416
|
+
if (errHier) {
|
|
1417
|
+
const errEls = this.extractMaestroSelectors(errHier);
|
|
1418
|
+
const errTxts = this.extractAllTexts(errHier);
|
|
1419
|
+
const errShot = await this._captureScreenshotMobile(adbS, `err_${btn.elementId}`);
|
|
1420
|
+
const knownIds = new Set(tapEls.map(e => e.id));
|
|
1421
|
+
const newErrEls = errEls.filter(e => !knownIds.has(e.id));
|
|
1422
|
+
const newErrTxts = errTxts.filter(t => !tapTxts.includes(t));
|
|
1423
|
+
if (newErrEls.length > 0 || newErrTxts.length > 0) {
|
|
1424
|
+
const errName = `${screenName} (Error — campos vacíos)`;
|
|
1425
|
+
allScreens.push({
|
|
1426
|
+
name: errName,
|
|
1427
|
+
isErrorState: true,
|
|
1428
|
+
screenshot: errShot,
|
|
1429
|
+
elements: newErrEls,
|
|
1430
|
+
allTexts: newErrTxts,
|
|
1431
|
+
});
|
|
1432
|
+
navigationSteps.push({ from: screenName, to: errName, action: 'Submit vacío' });
|
|
1433
|
+
const inputDataEmpty = {};
|
|
1434
|
+
for (const f of screenForm.fields)
|
|
1435
|
+
inputDataEmpty[f.id] = '';
|
|
1436
|
+
const errList = newErrTxts.slice(0, 3).map(t => `"${t}"`).join(', ');
|
|
1437
|
+
console.log(chalk.green(` ✅ Validación capturada: ${newErrEls.length} elementos / ${newErrTxts.length} textos${errList ? ` | ${errList}` : ''}`));
|
|
1438
|
+
const errEv = buildRecord({
|
|
1439
|
+
sessionId: corpus.sessionId,
|
|
1440
|
+
screen_origin: screenName,
|
|
1441
|
+
action_type: 'form_submit',
|
|
1442
|
+
action_element: screenForm.submitButtonId,
|
|
1443
|
+
action_label: screenForm.submitButtonLabel,
|
|
1444
|
+
screenshot_before: tapShot,
|
|
1445
|
+
elements_before: tapUI,
|
|
1446
|
+
input_data: inputDataEmpty,
|
|
1447
|
+
screen_destination: errName,
|
|
1448
|
+
result_type: 'validation_state',
|
|
1449
|
+
result_description: `Validación: ${errList || 'sin texto detectado'}`,
|
|
1450
|
+
screenshot_after: errShot,
|
|
1451
|
+
elements_after: legacyElementsToUIElements(newErrEls, '__pending__'),
|
|
1452
|
+
captured_by: 'autonomous',
|
|
1453
|
+
});
|
|
1454
|
+
addRecord(corpus, errEv);
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
// ── Step 4b: Interactive form prompt (real data flow) ──────
|
|
1460
|
+
// Only run if we still seem to be on the form screen. If empty
|
|
1461
|
+
// submit caused navigation away (rare), we skip the prompt.
|
|
1462
|
+
if (screenForm && options.onFormDetected) {
|
|
1463
|
+
// Re-capture state to ensure we're still on the form screen
|
|
1464
|
+
const stillOnFormHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
|
|
1465
|
+
const stillOnFormEls = stillOnFormHier ? this.extractMaestroSelectors(stillOnFormHier) : [];
|
|
1466
|
+
const stillOnFormUI = legacyElementsToUIElements(stillOnFormEls, '__pending__');
|
|
1467
|
+
const stillThere = isSameScreen(stillOnFormUI, tapUI, 0.6);
|
|
1468
|
+
if (!stillThere) {
|
|
1469
|
+
console.log(chalk.gray(` ℹ️ Form ya no visible (submit vacío navegó a otra pantalla). Saltando prompt interactivo.`));
|
|
1470
|
+
}
|
|
1471
|
+
else {
|
|
1472
|
+
console.log(chalk.cyan(` 📋 Form detectado en "${screenName}": ${screenForm.fields.length} campos`));
|
|
1473
|
+
const fieldData = await options.onFormDetected(screenForm);
|
|
1474
|
+
if (fieldData) {
|
|
1475
|
+
const fillSteps = [];
|
|
1476
|
+
for (const [fid, val] of Object.entries(fieldData)) {
|
|
1477
|
+
const selector = buildMaestroSelector(fid, isIos);
|
|
1478
|
+
fillSteps.push(`- tapOn:\n ${selector}`, `- eraseText: 50`, `- inputText: "${val}"`, `- hideKeyboard`);
|
|
1479
|
+
}
|
|
1480
|
+
const submitSelector = buildMaestroSelector(screenForm.submitButtonId, isIos);
|
|
1481
|
+
fillSteps.push(`- tapOn:\n ${submitSelector}`, `- waitForAnimationToEnd:\n timeout: 5000`);
|
|
1482
|
+
const formRes = this._runMaestroFlow(appId, fillSteps, maestroEnv);
|
|
1483
|
+
await sleep(2500);
|
|
1484
|
+
const formShot = await this._captureScreenshotMobile(adbS, `form_${btn.elementId}`);
|
|
1485
|
+
const formHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
|
|
1486
|
+
const formEls = formHier ? this.extractMaestroSelectors(formHier) : [];
|
|
1487
|
+
const formUI = legacyElementsToUIElements(formEls, '__pending__');
|
|
1488
|
+
const formTxts = formHier ? this.extractAllTexts(formHier) : [];
|
|
1489
|
+
const formChange = detectScreenChange(tapUI, formUI);
|
|
1490
|
+
const successText = formTxts.find(t => /comprobante|éxito|exitosa|confirmado|listo|aprobad/i.test(t));
|
|
1491
|
+
const isSuccess = !!successText && formRes.ok;
|
|
1492
|
+
let destName;
|
|
1493
|
+
let resultType;
|
|
1494
|
+
let resultDescription;
|
|
1495
|
+
if (isSuccess) {
|
|
1496
|
+
destName = inferScreenName(formTxts, [...tapTxts, ...parentTexts], `Resultado exitoso de ${screenName}`);
|
|
1497
|
+
resultType = 'new_screen';
|
|
1498
|
+
resultDescription = `Formulario exitoso — texto detectado: "${successText}" (${formEls.length} elementos)`;
|
|
1499
|
+
console.log(chalk.green(` ✅ Form exitoso: "${destName}"`));
|
|
1500
|
+
}
|
|
1501
|
+
else if (formRes.ok && formChange.changed) {
|
|
1502
|
+
destName = inferScreenName(formTxts, [...tapTxts, ...parentTexts], `Resultado de ${screenName}`);
|
|
1503
|
+
resultType = 'new_screen';
|
|
1504
|
+
resultDescription = `Formulario navegó a otra pantalla — sin texto explícito de éxito (similitud ${formChange.similarity.toFixed(2)})`;
|
|
1505
|
+
console.log(chalk.green(` ✅ Form navegó: "${destName}" (sin marcador de éxito)`));
|
|
1506
|
+
}
|
|
1507
|
+
else if (!formRes.ok) {
|
|
1508
|
+
destName = `${screenName} (Submit incompleto)`;
|
|
1509
|
+
resultType = 'partial_flow';
|
|
1510
|
+
resultDescription = `Maestro reportó fallo en el flow. Posible PIN/2FA, validación intermedia, o paso adicional requerido. Pantalla: ${formTxts.slice(0, 3).join(' | ') || '(sin texto)'}`;
|
|
1511
|
+
console.log(chalk.yellow(` ⚠️ Form submit incompleto — Maestro reportó fallo. Pantalla: ${formTxts.slice(0, 3).join(' | ') || '(desconocida)'}`));
|
|
1512
|
+
}
|
|
1513
|
+
else {
|
|
1514
|
+
destName = `${screenName} (Sin cambio)`;
|
|
1515
|
+
resultType = 'no_observable_change';
|
|
1516
|
+
resultDescription = `Submit no produjo cambio visible (similitud ${formChange.similarity.toFixed(2)})`;
|
|
1517
|
+
console.log(chalk.yellow(` ⚠️ Submit no cambió la pantalla`));
|
|
1518
|
+
}
|
|
1519
|
+
allScreens.push({
|
|
1520
|
+
name: destName,
|
|
1521
|
+
isErrorState: resultType === 'partial_flow' || resultType === 'no_observable_change',
|
|
1522
|
+
screenshot: formShot,
|
|
1523
|
+
elements: formEls,
|
|
1524
|
+
allTexts: formTxts,
|
|
1525
|
+
});
|
|
1526
|
+
navigationSteps.push({ from: screenName, to: destName, action: `Tap ${screenForm.submitButtonId}` });
|
|
1527
|
+
const formEv = buildRecord({
|
|
1528
|
+
sessionId: corpus.sessionId,
|
|
1529
|
+
screen_origin: screenName,
|
|
1530
|
+
action_type: 'form_submit',
|
|
1531
|
+
action_element: screenForm.submitButtonId,
|
|
1532
|
+
action_label: screenForm.submitButtonLabel,
|
|
1533
|
+
screenshot_before: tapShot,
|
|
1534
|
+
elements_before: tapUI,
|
|
1535
|
+
input_data: fieldData,
|
|
1536
|
+
screen_destination: destName,
|
|
1537
|
+
result_type: resultType,
|
|
1538
|
+
result_description: resultDescription,
|
|
1539
|
+
screenshot_after: formShot,
|
|
1540
|
+
elements_after: formUI,
|
|
1541
|
+
captured_by: 'interactive',
|
|
1542
|
+
});
|
|
1543
|
+
addRecord(corpus, formEv);
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
// ── Step 5: Return to parent screen for next BFS iteration ────
|
|
1548
|
+
// Try up to 4 backs until we reach a screen similar to the parent.
|
|
1549
|
+
// Some flows (form fill → comprobante → modal) require multiple backs.
|
|
1550
|
+
const returnedToParent = await this._smartBack(appId, maestroEnv, options.deviceId, parentUI, launchUI, loginForm, parentName);
|
|
1551
|
+
if (!returnedToParent) {
|
|
1552
|
+
console.log(chalk.yellow(` ⚠️ No se pudo regresar a "${parentName}" después de 4 backs. BFS detenido.`));
|
|
1553
|
+
break;
|
|
1554
|
+
}
|
|
234
1555
|
}
|
|
235
1556
|
}
|
|
236
|
-
// ──
|
|
1557
|
+
// ── Finalize evidence corpus ───────────────────────────────────────
|
|
1558
|
+
const finalCorpus = finalizeCorpus(corpus);
|
|
1559
|
+
console.log(chalk.dim(` 📦 ${corpusSummary(finalCorpus)}`));
|
|
1560
|
+
// Phase 11 — incremental summary
|
|
1561
|
+
if (mode === 'incremental' && (incrementalSkipCount + incrementalCaptureCount) > 0) {
|
|
1562
|
+
console.log(chalk.cyan(` 📚 Modo incremental: ${incrementalSkipCount} pantalla(s) reusadas, ` +
|
|
1563
|
+
`${incrementalCaptureCount} capturadas`));
|
|
1564
|
+
}
|
|
1565
|
+
// ── Build evidence package ─────────────────────────────────────────
|
|
1566
|
+
const finishedAt = Date.now();
|
|
237
1567
|
const totalElements = allScreens.reduce((sum, s) => sum + s.elements.length, 0);
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
1. Ingresar email en \`et_username\`
|
|
261
|
-
2. Ingresar contraseña en \`et_password\`
|
|
262
|
-
3. Tap \`btn_login\`
|
|
263
|
-
4. Verificar pantalla de cuentas visible
|
|
264
|
-
|
|
265
|
-
### Flow 2 — Login con credenciales inválidas
|
|
266
|
-
1. Ingresar email incorrecto
|
|
267
|
-
2. Ingresar contraseña incorrecta
|
|
268
|
-
3. Tap \`btn_login\`
|
|
269
|
-
4. Verificar mensaje de error visible
|
|
270
|
-
|
|
271
|
-
### Flow 3 — Consulta de saldo
|
|
272
|
-
1. Login exitoso
|
|
273
|
-
2. Verificar \`tv_total_balance\` visible
|
|
274
|
-
3. Verificar \`card_checking\` y \`card_savings\` presentes
|
|
275
|
-
|
|
276
|
-
### Flow 4 — Transferencia exitosa
|
|
277
|
-
1. Login → tap \`btn_transfer\`
|
|
278
|
-
2. Ingresar monto en \`et_amount\`
|
|
279
|
-
3. Ingresar destinatario en \`et_recipient\`
|
|
280
|
-
4. Tap \`btn_confirm\`
|
|
281
|
-
5. Verificar mensaje de éxito
|
|
282
|
-
|
|
283
|
-
### Flow 5 — Transferencia con monto inválido
|
|
284
|
-
1. Login → tap \`btn_transfer\`
|
|
285
|
-
2. Dejar monto vacío
|
|
286
|
-
3. Tap \`btn_confirm\`
|
|
287
|
-
4. Verificar mensaje de error
|
|
288
|
-
|
|
289
|
-
### Flow 6 — Logout
|
|
290
|
-
1. Login exitoso
|
|
291
|
-
2. Tap \`btn_logout\`
|
|
292
|
-
3. Verificar redirección a Login screen
|
|
293
|
-
|
|
294
|
-
## UI Health
|
|
295
|
-
- Total interactive nodes detected: ${totalElements}
|
|
296
|
-
- Screens mapped: ${allScreens.map(s => s.name).join(', ')}
|
|
297
|
-
- Screen structure complexity: ${totalElements > 15 ? 'High' : totalElements > 8 ? 'Medium' : 'Low'}
|
|
298
|
-
`;
|
|
1568
|
+
const errorStates = allScreens.filter(s => s.isErrorState).length;
|
|
1569
|
+
// Collect all verified IDs and texts across all screens
|
|
1570
|
+
const verifiedIds = [...new Set(allScreens.flatMap(s => s.elements.map(e => e.id)))];
|
|
1571
|
+
const verifiedTexts = [...new Set(allScreens
|
|
1572
|
+
.filter(s => s.isErrorState) // Only error states produce truly "verified" assertion texts
|
|
1573
|
+
.flatMap(s => s.allTexts))];
|
|
1574
|
+
const evidence = {
|
|
1575
|
+
screens: allScreens,
|
|
1576
|
+
metrics: {
|
|
1577
|
+
startedAt,
|
|
1578
|
+
finishedAt,
|
|
1579
|
+
durationMs: finishedAt - startedAt,
|
|
1580
|
+
screensDiscovered: allScreens.length,
|
|
1581
|
+
errorStatesExplored: errorStates,
|
|
1582
|
+
elementsFound: totalElements,
|
|
1583
|
+
navigationSteps,
|
|
1584
|
+
},
|
|
1585
|
+
verifiedTexts,
|
|
1586
|
+
verifiedIds,
|
|
1587
|
+
};
|
|
1588
|
+
// ── Build plan content from evidence (dynamic, includes all discovered flows) ─
|
|
1589
|
+
const planContent = this.buildPlanFromEvidence(evidence, appId);
|
|
299
1590
|
return {
|
|
300
1591
|
planContent,
|
|
301
1592
|
rawScanData: {
|
|
@@ -304,23 +1595,35 @@ ${screenSections}
|
|
|
304
1595
|
appId,
|
|
305
1596
|
framework: 'maestro',
|
|
306
1597
|
},
|
|
1598
|
+
evidence,
|
|
1599
|
+
corpus: finalCorpus,
|
|
307
1600
|
};
|
|
308
1601
|
}
|
|
309
1602
|
catch (error) {
|
|
310
|
-
|
|
1603
|
+
const msg = error.message || String(error);
|
|
1604
|
+
const isMaestroHierarchy = msg.includes('maestro') && msg.includes('hierarchy');
|
|
1605
|
+
console.log(chalk.red(`\n ❌ Error en Discovery: ${msg}`));
|
|
1606
|
+
if (isMaestroHierarchy) {
|
|
1607
|
+
console.log(chalk.yellow('\n 💡 Posibles causas:'));
|
|
1608
|
+
console.log(chalk.dim(' • La pantalla del dispositivo estaba apagada o bloqueada'));
|
|
1609
|
+
console.log(chalk.dim(' • Maestro perdió conexión con el driver del dispositivo'));
|
|
1610
|
+
console.log(chalk.dim(` • Prueba: adb -s ${options.deviceId ?? '<serial>'} shell input keyevent KEYCODE_WAKEUP`));
|
|
1611
|
+
console.log(chalk.dim(' • Luego ejecuta el comando de Discovery de nuevo\n'));
|
|
1612
|
+
}
|
|
311
1613
|
return {
|
|
312
|
-
planContent: `# Discovery Plan: Mobile App (${appId})\
|
|
313
|
-
rawScanData: { error:
|
|
1614
|
+
planContent: `# Discovery Plan: Mobile App (${appId})\nEstado: FALLIDO\nError: ${msg}\n`,
|
|
1615
|
+
rawScanData: { error: msg },
|
|
314
1616
|
};
|
|
315
1617
|
}
|
|
316
1618
|
},
|
|
1619
|
+
// ── Web Discovery ────────────────────────────────────────────────────────
|
|
317
1620
|
async handleLogin(page, username, password) {
|
|
318
1621
|
try {
|
|
319
1622
|
const passwordField = page.locator('input[type="password"]').first();
|
|
320
1623
|
const count = await passwordField.count();
|
|
321
1624
|
if (count === 0)
|
|
322
1625
|
return false;
|
|
323
|
-
console.log(chalk.gray('🔐
|
|
1626
|
+
console.log(chalk.gray('🔐 Formulario de login detectado — ingresando credenciales...'));
|
|
324
1627
|
const usernameField = page.locator('input[type="email"], input[name="email"], input[name="username"], input[name="user"], ' +
|
|
325
1628
|
'input[placeholder*="correo" i], input[placeholder*="email" i], input[placeholder*="usuario" i]').first();
|
|
326
1629
|
await usernameField.fill(username);
|
|
@@ -331,30 +1634,34 @@ ${screenSections}
|
|
|
331
1634
|
await submitBtn.click();
|
|
332
1635
|
await page.waitForLoadState('networkidle', { timeout: 10000 }).catch(() => { });
|
|
333
1636
|
await page.waitForTimeout(600);
|
|
334
|
-
console.log(chalk.green('✅ Login
|
|
1637
|
+
console.log(chalk.green('✅ Login exitoso — continuando discovery en página autenticada...'));
|
|
335
1638
|
return true;
|
|
336
1639
|
}
|
|
337
1640
|
catch (err) {
|
|
338
|
-
console.log(chalk.yellow(`⚠️ Login
|
|
1641
|
+
console.log(chalk.yellow(`⚠️ Login fallido: ${err.message}`));
|
|
339
1642
|
return false;
|
|
340
1643
|
}
|
|
341
1644
|
},
|
|
342
1645
|
async discoverWeb(options) {
|
|
343
1646
|
const { chromium } = await import('playwright');
|
|
344
|
-
|
|
1647
|
+
const startedAt = Date.now();
|
|
1648
|
+
this._progress(options, 'start', 'Iniciando crawler Playwright...');
|
|
1649
|
+
console.log(chalk.gray('🌐 Iniciando Playwright Crawler autónomo...'));
|
|
345
1650
|
const browser = await chromium.launch({ headless: true });
|
|
346
|
-
const context = await browser.newContext({
|
|
347
|
-
viewport: { width: 1440, height: 900 },
|
|
348
|
-
});
|
|
1651
|
+
const context = await browser.newContext({ viewport: { width: 1440, height: 900 } });
|
|
349
1652
|
const page = await context.newPage();
|
|
1653
|
+
const webScreens = [];
|
|
350
1654
|
try {
|
|
1655
|
+
this._progress(options, 'navigate', `Navegando a ${options.url}...`);
|
|
351
1656
|
await page.goto(options.url, { waitUntil: 'networkidle' });
|
|
352
|
-
//
|
|
1657
|
+
// Handle login if credentials provided
|
|
353
1658
|
if (options.username && options.password) {
|
|
1659
|
+
this._progress(options, 'login', 'Intentando autenticación...');
|
|
354
1660
|
await this.handleLogin(page, options.username, options.password);
|
|
355
1661
|
}
|
|
356
1662
|
const title = await page.title();
|
|
357
|
-
|
|
1663
|
+
this._progress(options, 'dom', 'Extrayendo elementos del DOM...');
|
|
1664
|
+
// Phase 1: Extract DOM elements
|
|
358
1665
|
const elements = await page.evaluate(() => {
|
|
359
1666
|
// @ts-ignore — runs in browser context
|
|
360
1667
|
const interactive = Array.from(document.querySelectorAll('button, a, input, select, textarea, [role="button"], [role="link"], [role="menuitem"]'));
|
|
@@ -376,33 +1683,46 @@ ${screenSections}
|
|
|
376
1683
|
};
|
|
377
1684
|
}).filter((e) => e.visible);
|
|
378
1685
|
});
|
|
379
|
-
//
|
|
380
|
-
|
|
1686
|
+
// Phase 2: Screenshots
|
|
1687
|
+
this._progress(options, 'screenshot', 'Capturando screenshots de la página...');
|
|
1688
|
+
console.log(chalk.gray('📸 Capturando screenshots para análisis Vision AI...'));
|
|
381
1689
|
const screenshots = [];
|
|
382
|
-
// Full-page screenshot
|
|
383
1690
|
const fullPageBuffer = await page.screenshot({ fullPage: true, type: 'png' });
|
|
384
1691
|
screenshots.push(fullPageBuffer.toString('base64'));
|
|
385
|
-
//
|
|
386
|
-
// @ts-ignore — runs in browser context
|
|
1692
|
+
// @ts-ignore
|
|
387
1693
|
const pageHeight = await page.evaluate(() => document.body.scrollHeight);
|
|
388
1694
|
const viewportHeight = 900;
|
|
389
1695
|
const sections = Math.min(Math.ceil(pageHeight / viewportHeight), 4);
|
|
390
1696
|
for (let i = 1; i < sections; i++) {
|
|
391
|
-
// @ts-ignore
|
|
1697
|
+
// @ts-ignore
|
|
392
1698
|
await page.evaluate((scrollY) => window.scrollTo(0, scrollY), i * viewportHeight);
|
|
393
1699
|
await page.waitForTimeout(400);
|
|
394
1700
|
const sectionBuffer = await page.screenshot({ type: 'png' });
|
|
395
1701
|
screenshots.push(sectionBuffer.toString('base64'));
|
|
396
1702
|
}
|
|
397
|
-
// @ts-ignore
|
|
1703
|
+
// @ts-ignore
|
|
398
1704
|
await page.evaluate(() => window.scrollTo(0, 0));
|
|
1705
|
+
// Store web screenshots in evidence
|
|
1706
|
+
webScreens.push({
|
|
1707
|
+
name: title || options.url,
|
|
1708
|
+
isErrorState: false,
|
|
1709
|
+
screenshot: screenshots[0] || null,
|
|
1710
|
+
elements: elements.slice(0, 30).map(e => ({
|
|
1711
|
+
type: e.tag,
|
|
1712
|
+
text: e.text || e.placeholder || e.ariaLabel || 'N/A',
|
|
1713
|
+
id: e.testId || e.id || e.name || '',
|
|
1714
|
+
})),
|
|
1715
|
+
allTexts: [],
|
|
1716
|
+
});
|
|
399
1717
|
await browser.close();
|
|
400
|
-
console.log(chalk.gray(`
|
|
401
|
-
|
|
1718
|
+
console.log(chalk.gray(` ${screenshots.length} screenshots, ${elements.length} elementos DOM`));
|
|
1719
|
+
const finishedAt = Date.now();
|
|
1720
|
+
// Phase 3: Vision AI Analysis
|
|
402
1721
|
let planContent;
|
|
1722
|
+
this._progress(options, 'vision', 'Enviando a Vision AI para análisis semántico...');
|
|
403
1723
|
if (options.projectId && options.entityId) {
|
|
404
1724
|
try {
|
|
405
|
-
console.log(chalk.gray('🧠
|
|
1725
|
+
console.log(chalk.gray('🧠 Enviando a Vision AI para análisis semántico...'));
|
|
406
1726
|
const visionResult = await FlowSpecQAApi.analyzeVision({
|
|
407
1727
|
url: options.url,
|
|
408
1728
|
entity_id: options.entityId,
|
|
@@ -412,21 +1732,39 @@ ${screenSections}
|
|
|
412
1732
|
page_title: title,
|
|
413
1733
|
});
|
|
414
1734
|
planContent = visionResult.plan_content;
|
|
415
|
-
console.log(chalk.gray(` Vision
|
|
416
|
-
`${visionResult.screenshots_analyzed} screenshots
|
|
1735
|
+
console.log(chalk.gray(` Vision: ${visionResult.analysis_chars} chars, ` +
|
|
1736
|
+
`${visionResult.screenshots_analyzed} screenshots procesados`));
|
|
417
1737
|
}
|
|
418
1738
|
catch (visionErr) {
|
|
419
|
-
console.log(chalk.yellow(`⚠️ Vision AI
|
|
1739
|
+
console.log(chalk.yellow(`⚠️ Vision AI no disponible (${visionErr.message}), usando plan DOM...`));
|
|
420
1740
|
planContent = this._buildDomPlan(title, options.url, elements);
|
|
421
1741
|
}
|
|
422
1742
|
}
|
|
423
1743
|
else {
|
|
424
|
-
// No project/entity → DOM plan only (local use)
|
|
425
1744
|
planContent = this._buildDomPlan(title, options.url, elements);
|
|
426
1745
|
}
|
|
1746
|
+
const verifiedIds = [...new Set(elements
|
|
1747
|
+
.filter(e => e.testId || e.id || e.name)
|
|
1748
|
+
.map(e => e.testId || e.id || e.name)
|
|
1749
|
+
.filter(Boolean))];
|
|
1750
|
+
const evidence = {
|
|
1751
|
+
screens: webScreens,
|
|
1752
|
+
metrics: {
|
|
1753
|
+
startedAt,
|
|
1754
|
+
finishedAt,
|
|
1755
|
+
durationMs: finishedAt - startedAt,
|
|
1756
|
+
screensDiscovered: 1,
|
|
1757
|
+
errorStatesExplored: 0,
|
|
1758
|
+
elementsFound: elements.length,
|
|
1759
|
+
navigationSteps: [{ from: 'Inicio', to: title || options.url, action: 'Navegación directa' }],
|
|
1760
|
+
},
|
|
1761
|
+
verifiedTexts: [], // Web: texts come from Vision AI analysis
|
|
1762
|
+
verifiedIds,
|
|
1763
|
+
};
|
|
427
1764
|
return {
|
|
428
1765
|
planContent,
|
|
429
1766
|
rawScanData: { elements, url: options.url, framework: 'playwright', total: elements.length },
|
|
1767
|
+
evidence,
|
|
430
1768
|
};
|
|
431
1769
|
}
|
|
432
1770
|
catch (error) {
|
|
@@ -438,14 +1776,14 @@ ${screenSections}
|
|
|
438
1776
|
_buildDomPlan(title, url, elements) {
|
|
439
1777
|
return `# Discovery Plan: Web App (${title})
|
|
440
1778
|
URL: ${url}
|
|
441
|
-
|
|
1779
|
+
Generado: ${new Date().toISOString()}
|
|
442
1780
|
|
|
443
|
-
##
|
|
444
|
-
|
|
1781
|
+
## Resumen
|
|
1782
|
+
**${elements.length}** elementos interactivos encontrados.
|
|
445
1783
|
|
|
446
|
-
###
|
|
447
|
-
| Tag |
|
|
448
|
-
|
|
1784
|
+
### Mapa de UI Principal
|
|
1785
|
+
| Tag | Descripción | Selector sugerido | testID |
|
|
1786
|
+
|-----|-------------|-------------------|--------|
|
|
449
1787
|
${elements.slice(0, 20).map((e) => {
|
|
450
1788
|
let selector = e.tag;
|
|
451
1789
|
if (e.testId)
|
|
@@ -461,12 +1799,497 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
461
1799
|
return `| ${e.tag} | ${e.text || e.placeholder || e.ariaLabel || '-'} | \`${selector}\` | ${e.testId || '-'} |`;
|
|
462
1800
|
}).join('\n')}
|
|
463
1801
|
|
|
464
|
-
##
|
|
465
|
-
-
|
|
466
|
-
-
|
|
467
|
-
-
|
|
1802
|
+
## Estructura
|
|
1803
|
+
- ${elements.filter((e) => e.tag === 'button').length} botones
|
|
1804
|
+
- ${elements.filter((e) => e.tag === 'a').length} links
|
|
1805
|
+
- ${elements.filter((e) => e.tag === 'input').length} inputs
|
|
468
1806
|
`;
|
|
469
1807
|
},
|
|
1808
|
+
// ── Interactive Discovery helpers ────────────────────────────────────────
|
|
1809
|
+
/**
|
|
1810
|
+
* Inspect the current evidence and return elements that were seen on screen
|
|
1811
|
+
* but never explicitly interacted with during Discovery.
|
|
1812
|
+
* These become the candidates for the interactive exploration loop.
|
|
1813
|
+
*/
|
|
1814
|
+
detectUnexplored(evidence) {
|
|
1815
|
+
// Collect element IDs that were explicitly tapped during Discovery.
|
|
1816
|
+
// Matches several action formats produced by the engine:
|
|
1817
|
+
// - "Tap btn_xxx" (BFS Phase 0)
|
|
1818
|
+
// - "Tap en btn_xxx" (legacy)
|
|
1819
|
+
// - "Tap btn_xxx (label)" (variants)
|
|
1820
|
+
// - "Lanzar app" (initial launch — no element)
|
|
1821
|
+
// - "Submit vacío" (form validation — no element)
|
|
1822
|
+
// - "Login con credenciales válidas" (no element)
|
|
1823
|
+
const exploredIds = new Set();
|
|
1824
|
+
for (const step of evidence.metrics.navigationSteps) {
|
|
1825
|
+
// Match "Tap <id>" or "Tap en <id>" — first non-stopword token after Tap
|
|
1826
|
+
const tapMatch = step.action.match(/^Tap(?:\s+en)?\s+(\S+)/i);
|
|
1827
|
+
if (tapMatch)
|
|
1828
|
+
exploredIds.add(tapMatch[1]);
|
|
1829
|
+
}
|
|
1830
|
+
// Also count elements that participated in form_submit EvidenceRecords
|
|
1831
|
+
// (their submit button + each field id). Those are "interacted with".
|
|
1832
|
+
if (evidence.records) {
|
|
1833
|
+
for (const rec of evidence.records ?? []) {
|
|
1834
|
+
if (rec.action_element)
|
|
1835
|
+
exploredIds.add(rec.action_element);
|
|
1836
|
+
if (rec.input_data) {
|
|
1837
|
+
for (const fid of Object.keys(rec.input_data))
|
|
1838
|
+
exploredIds.add(fid);
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
// Login/submit elements that the generic flow always handles
|
|
1843
|
+
// (kept for legacy compat; modern flow uses detectLoginForm dynamically)
|
|
1844
|
+
const ALWAYS_HANDLED = new Set([
|
|
1845
|
+
'btn_login', 'btn_confirm', 'et_username', 'et_password',
|
|
1846
|
+
'btn_submit', 'btn_register', 'btn_back',
|
|
1847
|
+
]);
|
|
1848
|
+
const unexplored = [];
|
|
1849
|
+
const seenIds = new Set();
|
|
1850
|
+
for (const screen of evidence.screens) {
|
|
1851
|
+
if (screen.isErrorState)
|
|
1852
|
+
continue; // don't suggest exploring error screens
|
|
1853
|
+
for (const elem of screen.elements) {
|
|
1854
|
+
if (!elem.id || elem.id === 'N/A')
|
|
1855
|
+
continue;
|
|
1856
|
+
if (seenIds.has(elem.id))
|
|
1857
|
+
continue;
|
|
1858
|
+
if (exploredIds.has(elem.id))
|
|
1859
|
+
continue;
|
|
1860
|
+
if (ALWAYS_HANDLED.has(elem.id))
|
|
1861
|
+
continue;
|
|
1862
|
+
// Skip pure inputs (EditText, CheckBox, etc.) — those are filled, not navigated
|
|
1863
|
+
if (['EditText', 'CheckBox', 'RadioButton', 'Switch'].includes(elem.type))
|
|
1864
|
+
continue;
|
|
1865
|
+
// Use the same heuristic as the BFS: looksLikeButton recognizes
|
|
1866
|
+
// TextView with id `btn_*` or visible text like "Continuar" as
|
|
1867
|
+
// navigable buttons (Phase 0 — apps with custom UI like Scotiabank).
|
|
1868
|
+
// We need to convert the legacy `{ type, text, id }` shape to the
|
|
1869
|
+
// UIElement shape that looksLikeButton expects.
|
|
1870
|
+
const uiElem = {
|
|
1871
|
+
elementId: elem.id,
|
|
1872
|
+
elementType: elem.type,
|
|
1873
|
+
visibleText: elem.text !== 'N/A' ? elem.text : undefined,
|
|
1874
|
+
isInteractive: false,
|
|
1875
|
+
evidenceRef: '',
|
|
1876
|
+
};
|
|
1877
|
+
if (!looksLikeButton(uiElem))
|
|
1878
|
+
continue;
|
|
1879
|
+
seenIds.add(elem.id);
|
|
1880
|
+
unexplored.push({
|
|
1881
|
+
elementId: elem.id,
|
|
1882
|
+
elementText: elem.text === 'N/A' ? '' : elem.text,
|
|
1883
|
+
sourceScreen: screen.name,
|
|
1884
|
+
elementType: elem.type,
|
|
1885
|
+
});
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
return unexplored;
|
|
1889
|
+
},
|
|
1890
|
+
/**
|
|
1891
|
+
* Convert a navigation step action string into the Maestro YAML steps
|
|
1892
|
+
* needed to perform that action.
|
|
1893
|
+
*/
|
|
1894
|
+
_getActionYaml(action, creds, isIos = false) {
|
|
1895
|
+
if (action === 'Lanzar app') {
|
|
1896
|
+
return [
|
|
1897
|
+
`- launchApp:\n clearState: true`,
|
|
1898
|
+
`- waitForAnimationToEnd`,
|
|
1899
|
+
];
|
|
1900
|
+
}
|
|
1901
|
+
if (action === 'Login con credenciales válidas') {
|
|
1902
|
+
return [
|
|
1903
|
+
`- tapOn:\n id: "et_username"`,
|
|
1904
|
+
`- eraseText: 50`,
|
|
1905
|
+
`- inputText: "${creds.username}"`,
|
|
1906
|
+
`- waitForAnimationToEnd`,
|
|
1907
|
+
`- hideKeyboard`,
|
|
1908
|
+
`- tapOn:\n id: "et_password"`,
|
|
1909
|
+
`- eraseText: 50`,
|
|
1910
|
+
`- inputText: "${creds.password}"`,
|
|
1911
|
+
`- waitForAnimationToEnd`,
|
|
1912
|
+
`- hideKeyboard`,
|
|
1913
|
+
`- tapOn:\n id: "btn_login"`,
|
|
1914
|
+
`- waitForAnimationToEnd`,
|
|
1915
|
+
];
|
|
1916
|
+
}
|
|
1917
|
+
const tapMatch = action.match(/^Tap\s+(\S+)/i);
|
|
1918
|
+
if (tapMatch) {
|
|
1919
|
+
return [
|
|
1920
|
+
`- tapOn:\n ${buildMaestroSelector(tapMatch[1], isIos)}`,
|
|
1921
|
+
`- waitForAnimationToEnd`,
|
|
1922
|
+
];
|
|
1923
|
+
}
|
|
1924
|
+
return []; // unknown / error-state actions — skip
|
|
1925
|
+
},
|
|
1926
|
+
/**
|
|
1927
|
+
* BFS the navigation graph to build and execute the Maestro flow that
|
|
1928
|
+
* navigates from the app's initial state to `targetScreen`.
|
|
1929
|
+
* Returns true if navigation succeeded, false otherwise.
|
|
1930
|
+
*/
|
|
1931
|
+
async _navigateToScreen(targetScreen, evidence, creds, appId, maestroEnv, isIos = false) {
|
|
1932
|
+
// Build adjacency list — skip edges that lead to error states
|
|
1933
|
+
const graph = new Map();
|
|
1934
|
+
for (const step of evidence.metrics.navigationSteps) {
|
|
1935
|
+
const toScreen = evidence.screens.find(s => s.name === step.to);
|
|
1936
|
+
if (toScreen?.isErrorState)
|
|
1937
|
+
continue; // never route through error states
|
|
1938
|
+
if (!graph.has(step.from))
|
|
1939
|
+
graph.set(step.from, []);
|
|
1940
|
+
graph.get(step.from).push({ to: step.to, action: step.action });
|
|
1941
|
+
}
|
|
1942
|
+
if (targetScreen === 'Inicio')
|
|
1943
|
+
return true; // already at start
|
|
1944
|
+
// BFS from 'Inicio'
|
|
1945
|
+
const visited = new Set();
|
|
1946
|
+
const queue = [
|
|
1947
|
+
{ screen: 'Inicio', actions: [] },
|
|
1948
|
+
];
|
|
1949
|
+
while (queue.length > 0) {
|
|
1950
|
+
const { screen, actions } = queue.shift();
|
|
1951
|
+
if (screen === targetScreen) {
|
|
1952
|
+
if (actions.length === 0)
|
|
1953
|
+
return true;
|
|
1954
|
+
return this._runMaestroFlow(appId, actions, maestroEnv).ok;
|
|
1955
|
+
}
|
|
1956
|
+
if (visited.has(screen))
|
|
1957
|
+
continue;
|
|
1958
|
+
visited.add(screen);
|
|
1959
|
+
for (const edge of (graph.get(screen) || [])) {
|
|
1960
|
+
const stepYaml = this._getActionYaml(edge.action, creds, isIos);
|
|
1961
|
+
queue.push({ screen: edge.to, actions: [...actions, ...stepYaml] });
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
return false; // path not found
|
|
1965
|
+
},
|
|
1966
|
+
/**
|
|
1967
|
+
* Explore a single previously-unexplored element:
|
|
1968
|
+
* 1. Navigate to its source screen
|
|
1969
|
+
* 2. Tap the element
|
|
1970
|
+
* 3. Capture hierarchy + screenshot of the resulting state
|
|
1971
|
+
* 4. Attempt to trigger an error state on the new screen
|
|
1972
|
+
* 5. Push findings into `evidence` in-place
|
|
1973
|
+
*
|
|
1974
|
+
* Returns the new ScreenEvidence entries that were added.
|
|
1975
|
+
*/
|
|
1976
|
+
async exploreElement(element, options, evidence, corpus) {
|
|
1977
|
+
const appId = options.url;
|
|
1978
|
+
const adb = resolveAdb();
|
|
1979
|
+
const adbS = options.deviceId ? `"${adb}" -s ${options.deviceId}` : `"${adb}"`;
|
|
1980
|
+
const maestroEnv = options.deviceId
|
|
1981
|
+
? { ...process.env, MAESTRO_DEVICE_ID: options.deviceId }
|
|
1982
|
+
: process.env;
|
|
1983
|
+
const creds = { username: options.username || '', password: options.password || '' };
|
|
1984
|
+
const added = [];
|
|
1985
|
+
const isIos = !!options.deviceId && /iPhone|iPad/i.test(options.deviceId);
|
|
1986
|
+
console.log(chalk.gray(` 🔭 Navegando a "${element.sourceScreen}"...`));
|
|
1987
|
+
// 1. Navigate to the source screen from app start
|
|
1988
|
+
const navOk = await this._navigateToScreen(element.sourceScreen, evidence, creds, appId, maestroEnv, isIos);
|
|
1989
|
+
if (!navOk) {
|
|
1990
|
+
console.log(chalk.yellow(` ⚠️ No se pudo navegar a "${element.sourceScreen}"`));
|
|
1991
|
+
return [];
|
|
1992
|
+
}
|
|
1993
|
+
await sleep(2000); // wait for screen to settle
|
|
1994
|
+
// 2. Capture before state for EvidenceRecord
|
|
1995
|
+
const sourceScreenData = evidence.screens.find(s => s.name === element.sourceScreen);
|
|
1996
|
+
const screenshotBefore = await this._captureScreenshotMobile(adbS, `explore_${element.elementId}_before`);
|
|
1997
|
+
// 3. Tap the element
|
|
1998
|
+
console.log(chalk.gray(` 👆 Tap en ${element.elementId}...`));
|
|
1999
|
+
const tapOk = this._runMaestroFlow(appId, [
|
|
2000
|
+
`- tapOn:\n ${buildMaestroSelector(element.elementId, isIos)}`,
|
|
2001
|
+
`- waitForAnimationToEnd:\n timeout: 3000`,
|
|
2002
|
+
], maestroEnv).ok;
|
|
2003
|
+
if (!tapOk) {
|
|
2004
|
+
console.log(chalk.yellow(` ⚠️ Tap en ${element.elementId} falló`));
|
|
2005
|
+
return [];
|
|
2006
|
+
}
|
|
2007
|
+
await sleep(2000); // wait for the new screen to render
|
|
2008
|
+
// 4. Capture resulting state
|
|
2009
|
+
let hierarchy;
|
|
2010
|
+
try {
|
|
2011
|
+
hierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
|
|
2012
|
+
}
|
|
2013
|
+
catch {
|
|
2014
|
+
console.log(chalk.yellow(` ⚠️ No se pudo capturar jerarquía`));
|
|
2015
|
+
return [];
|
|
2016
|
+
}
|
|
2017
|
+
const screenshot = await this._captureScreenshotMobile(adbS, `explore_${element.elementId}`);
|
|
2018
|
+
const newElements = this.extractMaestroSelectors(hierarchy);
|
|
2019
|
+
const newTexts = this.extractAllTexts(hierarchy);
|
|
2020
|
+
// Decide if this looks like a new screen
|
|
2021
|
+
const sourceIds = new Set((sourceScreenData?.elements || []).map(e => e.id));
|
|
2022
|
+
const novelElements = newElements.filter(e => !sourceIds.has(e.id));
|
|
2023
|
+
if (novelElements.length < 2 && !screenshot) {
|
|
2024
|
+
console.log(chalk.yellow(` ⚠️ ${element.elementId} no abrió pantalla nueva (mismos elementos)`));
|
|
2025
|
+
// Still mark as explored by recording a navigation step with no destination screen
|
|
2026
|
+
evidence.metrics.navigationSteps.push({
|
|
2027
|
+
from: element.sourceScreen,
|
|
2028
|
+
to: `${element.sourceScreen} (sin cambio)`,
|
|
2029
|
+
action: `Tap ${element.elementId}`,
|
|
2030
|
+
});
|
|
2031
|
+
return [];
|
|
2032
|
+
}
|
|
2033
|
+
// 4. Register new screen
|
|
2034
|
+
const screenName = novelElements.length > 0
|
|
2035
|
+
? `${element.elementId} (desde ${element.sourceScreen})`
|
|
2036
|
+
: `${element.sourceScreen} — ${element.elementText || element.elementId}`;
|
|
2037
|
+
const newScreen = {
|
|
2038
|
+
name: screenName,
|
|
2039
|
+
isErrorState: false,
|
|
2040
|
+
screenshot,
|
|
2041
|
+
elements: newElements,
|
|
2042
|
+
allTexts: newTexts,
|
|
2043
|
+
};
|
|
2044
|
+
added.push(newScreen);
|
|
2045
|
+
evidence.screens.push(newScreen);
|
|
2046
|
+
evidence.metrics.navigationSteps.push({
|
|
2047
|
+
from: element.sourceScreen,
|
|
2048
|
+
to: screenName,
|
|
2049
|
+
action: `Tap ${element.elementId}`,
|
|
2050
|
+
});
|
|
2051
|
+
evidence.metrics.screensDiscovered++;
|
|
2052
|
+
evidence.metrics.elementsFound += novelElements.length;
|
|
2053
|
+
// 5. Phase 13 — Real-time Sync (Assisted/Manual mode visibility)
|
|
2054
|
+
// Upload this step to the SaaS immediately so the user sees progress in the Evidence tab.
|
|
2055
|
+
try {
|
|
2056
|
+
const ui = legacyElementsToUIElements(newElements, '__pending__');
|
|
2057
|
+
const { screenFingerprint } = await import('./evidence-corpus.js');
|
|
2058
|
+
const fp = screenFingerprint(ui, newTexts);
|
|
2059
|
+
await FlowSpecQAApi.uploadManualCapture({
|
|
2060
|
+
project_id: options.projectId || 'TRUSTBANK',
|
|
2061
|
+
entity_id: options.entityId || 'Consorcio-Discovery',
|
|
2062
|
+
label: `Step: Tap ${element.elementId} -> ${screenName}`,
|
|
2063
|
+
screen_name: screenName,
|
|
2064
|
+
elements: ui,
|
|
2065
|
+
texts: newTexts,
|
|
2066
|
+
screenshot_b64: screenshot || undefined,
|
|
2067
|
+
screen_fingerprint: fp,
|
|
2068
|
+
platform: options.deviceId?.includes('-') ? 'ios' : 'android', // Simple heuristic
|
|
2069
|
+
});
|
|
2070
|
+
}
|
|
2071
|
+
catch (err) {
|
|
2072
|
+
console.log(chalk.yellow(` ⚠️ No se pudo sincronizar evidencia con SaaS: ${err}`));
|
|
2073
|
+
}
|
|
2074
|
+
// Track verified IDs from the new screen
|
|
2075
|
+
for (const e of newElements) {
|
|
2076
|
+
if (e.id && !evidence.verifiedIds.includes(e.id))
|
|
2077
|
+
evidence.verifiedIds.push(e.id);
|
|
2078
|
+
}
|
|
2079
|
+
console.log(chalk.green(` ✅ Nueva pantalla: "${screenName}" | ${novelElements.length} elementos nuevos${screenshot ? ' | screenshot ✓' : ''}`));
|
|
2080
|
+
// ── EvidenceRecord — Tap this element ─────────────────────────────────
|
|
2081
|
+
if (corpus) {
|
|
2082
|
+
const exploreRecord = buildRecord({
|
|
2083
|
+
sessionId: corpus.sessionId,
|
|
2084
|
+
screen_origin: element.sourceScreen,
|
|
2085
|
+
action_type: 'tap',
|
|
2086
|
+
action_element: element.elementId,
|
|
2087
|
+
action_label: element.elementText || undefined,
|
|
2088
|
+
screenshot_before: screenshotBefore,
|
|
2089
|
+
elements_before: legacyElementsToUIElements(sourceScreenData?.elements || [], '__pending__'),
|
|
2090
|
+
screen_destination: screenName,
|
|
2091
|
+
result_type: 'new_screen',
|
|
2092
|
+
result_description: `Pantalla nueva: ${novelElements.length} elementos noveles`,
|
|
2093
|
+
screenshot_after: screenshot,
|
|
2094
|
+
elements_after: legacyElementsToUIElements(newElements, '__pending__'),
|
|
2095
|
+
captured_by: 'autonomous',
|
|
2096
|
+
});
|
|
2097
|
+
addRecord(corpus, exploreRecord);
|
|
2098
|
+
// Form detection on the new screen
|
|
2099
|
+
const newUIElements = legacyElementsToUIElements(newElements, exploreRecord.evidenceId);
|
|
2100
|
+
const detectedForm = detectForm(newUIElements, screenName);
|
|
2101
|
+
if (detectedForm && options.onFormDetected) {
|
|
2102
|
+
console.log(chalk.cyan(`\n 📋 Formulario en "${screenName}": ${detectedForm.fields.length} campos`));
|
|
2103
|
+
const fieldData = await options.onFormDetected(detectedForm);
|
|
2104
|
+
if (fieldData) {
|
|
2105
|
+
const fillSteps = [];
|
|
2106
|
+
for (const [fieldId, value] of Object.entries(fieldData)) {
|
|
2107
|
+
fillSteps.push(`- tapOn:\n ${buildMaestroSelector(fieldId, isIos)}`, `- eraseText: 50`, `- inputText: "${value}"`, `- hideKeyboard`);
|
|
2108
|
+
}
|
|
2109
|
+
fillSteps.push(`- tapOn:\n ${buildMaestroSelector(detectedForm.submitButtonId, isIos)}`, `- waitForAnimationToEnd:\n timeout: 5000`);
|
|
2110
|
+
const formOk = this._runMaestroFlow(appId, fillSteps, maestroEnv).ok;
|
|
2111
|
+
if (formOk) {
|
|
2112
|
+
await sleep(2500);
|
|
2113
|
+
const formResultShot = await this._captureScreenshotMobile(adbS, `explore_${element.elementId}_form`);
|
|
2114
|
+
let formHierarchy;
|
|
2115
|
+
try {
|
|
2116
|
+
formHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
|
|
2117
|
+
}
|
|
2118
|
+
catch { }
|
|
2119
|
+
const formResultElems = formHierarchy ? this.extractMaestroSelectors(formHierarchy) : [];
|
|
2120
|
+
const formResultTexts = formHierarchy ? this.extractAllTexts(formHierarchy) : [];
|
|
2121
|
+
const isSuccess = formResultTexts.some(t => /comprobante|éxito|exitosa|confirmado|ok/i.test(t));
|
|
2122
|
+
const formDest = isSuccess ? `${screenName} (Éxito)` : `${screenName} (resultado)`;
|
|
2123
|
+
const formResult = 'new_screen'; // success or generic navigation both produce a new screen
|
|
2124
|
+
const formRecord = buildRecord({
|
|
2125
|
+
sessionId: corpus.sessionId,
|
|
2126
|
+
screen_origin: screenName,
|
|
2127
|
+
action_type: 'form_submit',
|
|
2128
|
+
action_element: detectedForm.submitButtonId,
|
|
2129
|
+
action_label: detectedForm.submitButtonLabel,
|
|
2130
|
+
screenshot_before: screenshot,
|
|
2131
|
+
elements_before: newUIElements,
|
|
2132
|
+
input_data: fieldData,
|
|
2133
|
+
screen_destination: formDest,
|
|
2134
|
+
result_type: formResult,
|
|
2135
|
+
result_description: `Formulario interactivo — resultado: ${formResult}`,
|
|
2136
|
+
screenshot_after: formResultShot,
|
|
2137
|
+
elements_after: legacyElementsToUIElements(formResultElems, '__pending__'),
|
|
2138
|
+
captured_by: 'interactive',
|
|
2139
|
+
});
|
|
2140
|
+
addRecord(corpus, formRecord);
|
|
2141
|
+
const formScreen = {
|
|
2142
|
+
name: formDest,
|
|
2143
|
+
isErrorState: !isSuccess,
|
|
2144
|
+
screenshot: formResultShot,
|
|
2145
|
+
elements: formResultElems,
|
|
2146
|
+
allTexts: formResultTexts,
|
|
2147
|
+
};
|
|
2148
|
+
added.push(formScreen);
|
|
2149
|
+
evidence.screens.push(formScreen);
|
|
2150
|
+
evidence.metrics.navigationSteps.push({
|
|
2151
|
+
from: screenName, to: formDest,
|
|
2152
|
+
action: `Form submit interactivo (${detectedForm.submitButtonId})`,
|
|
2153
|
+
});
|
|
2154
|
+
console.log(chalk.green(` ✅ Formulario interactivo: "${formDest}"`));
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
// 5. Try to trigger error state on new screen (tap first submit-like button without filling fields)
|
|
2160
|
+
const submitBtn = newElements.find(e => ['btn_confirm', 'btn_submit', 'btn_pay', 'btn_send', 'btn_continue', 'btn_accept'].includes(e.id) ||
|
|
2161
|
+
(e.type === 'Button' && e.id !== element.elementId));
|
|
2162
|
+
if (submitBtn) {
|
|
2163
|
+
console.log(chalk.gray(` 📸 Intentando error state (${submitBtn.id})...`));
|
|
2164
|
+
const errorOk = this._runMaestroFlow(appId, [
|
|
2165
|
+
`- tapOn:\n ${buildMaestroSelector(submitBtn.id, isIos)}`,
|
|
2166
|
+
`- waitForAnimationToEnd:\n timeout: 1500`,
|
|
2167
|
+
], maestroEnv).ok;
|
|
2168
|
+
if (errorOk) {
|
|
2169
|
+
await sleep(1500);
|
|
2170
|
+
try {
|
|
2171
|
+
const errHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
|
|
2172
|
+
const errScreenshot = await this._captureScreenshotMobile(adbS, `explore_${element.elementId}_err`);
|
|
2173
|
+
const errElements = this.extractMaestroSelectors(errHierarchy);
|
|
2174
|
+
const errTexts = this.extractAllTexts(errHierarchy);
|
|
2175
|
+
const knownIds = new Set(newElements.map(e => e.id));
|
|
2176
|
+
const errNewElems = errElements.filter(e => !knownIds.has(e.id));
|
|
2177
|
+
const errNewTexts = errTexts.filter(t => !newTexts.includes(t));
|
|
2178
|
+
if (errNewTexts.length > 0 || errNewElems.length > 0) {
|
|
2179
|
+
const errScreenName = `${screenName} (Error)`;
|
|
2180
|
+
const errScreen = {
|
|
2181
|
+
name: errScreenName,
|
|
2182
|
+
isErrorState: true,
|
|
2183
|
+
screenshot: errScreenshot,
|
|
2184
|
+
elements: errNewElems,
|
|
2185
|
+
allTexts: errNewTexts,
|
|
2186
|
+
};
|
|
2187
|
+
added.push(errScreen);
|
|
2188
|
+
evidence.screens.push(errScreen);
|
|
2189
|
+
evidence.metrics.navigationSteps.push({
|
|
2190
|
+
from: screenName,
|
|
2191
|
+
to: errScreenName,
|
|
2192
|
+
action: `Submit vacío (${submitBtn.id})`,
|
|
2193
|
+
});
|
|
2194
|
+
evidence.metrics.errorStatesExplored++;
|
|
2195
|
+
for (const t of errNewTexts) {
|
|
2196
|
+
if (!evidence.verifiedTexts.includes(t))
|
|
2197
|
+
evidence.verifiedTexts.push(t);
|
|
2198
|
+
}
|
|
2199
|
+
const errList = errNewTexts.slice(0, 3).map(t => `"${t}"`).join(', ');
|
|
2200
|
+
console.log(chalk.green(` ✅ Error state: ${errList || 'capturado'}`));
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
catch { /* non-fatal */ }
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
return added;
|
|
2207
|
+
},
|
|
2208
|
+
/**
|
|
2209
|
+
* Build the full discovery plan markdown from an evidence package.
|
|
2210
|
+
* Called after each interactive exploration round to keep the plan current.
|
|
2211
|
+
* Also used by discoverMobile() for the initial plan generation.
|
|
2212
|
+
*/
|
|
2213
|
+
buildPlanFromEvidence(evidence, appId) {
|
|
2214
|
+
const { screens, metrics, verifiedTexts, verifiedIds } = evidence;
|
|
2215
|
+
// ── Screen sections ───────────────────────────────────────────────────
|
|
2216
|
+
const screenSections = screens.map(screen => `
|
|
2217
|
+
### Pantalla: ${screen.name}${screen.isErrorState ? ' ⚠️' : ''}
|
|
2218
|
+
| Tipo | Texto | ID |
|
|
2219
|
+
|------|-------|-----|
|
|
2220
|
+
${screen.elements.map(e => `| ${e.type} | ${e.text} | \`${e.id}\` |`).join('\n') || '| — | Sin elementos interactivos | — |'}
|
|
2221
|
+
${screen.allTexts.length > 0 ? `\n**Textos verificados en pantalla:** ${screen.allTexts.slice(0, 10).map(t => `"${t}"`).join(', ')}` : ''}
|
|
2222
|
+
`).join('\n');
|
|
2223
|
+
// ── Confidence map ────────────────────────────────────────────────────
|
|
2224
|
+
const confidenceSection = `
|
|
2225
|
+
## 🎯 Mapa de Confianza
|
|
2226
|
+
|
|
2227
|
+
### IDs verificados (confianza ALTA — elemento visto en dispositivo real)
|
|
2228
|
+
${verifiedIds.length > 0 ? verifiedIds.map(id => `- \`${id}\``).join('\n') : '- Ninguno'}
|
|
2229
|
+
|
|
2230
|
+
### Textos verificados en error states (confianza ALTA — texto real de la app)
|
|
2231
|
+
${verifiedTexts.length > 0 ? verifiedTexts.map(t => `- "${t}"`).join('\n') : '- Ninguno — revisar manualmente (Discovery Fase 2 los capturará)'}
|
|
2232
|
+
|
|
2233
|
+
### ⚠️ Textos NO verificados = Confianza BAJA
|
|
2234
|
+
Los assertVisible con textos que NO aparezcan arriba fueron inferidos por la IA.
|
|
2235
|
+
Son los candidatos más probables a fallar en la primera ejecución.
|
|
2236
|
+
`;
|
|
2237
|
+
// ── Flows — generated dynamically from navigation graph ───────────────
|
|
2238
|
+
const flows = [];
|
|
2239
|
+
let flowNum = 1;
|
|
2240
|
+
// Non-error navigation flows
|
|
2241
|
+
for (const step of metrics.navigationSteps) {
|
|
2242
|
+
const toScreen = screens.find(s => s.name === step.to);
|
|
2243
|
+
if (!toScreen || toScreen.isErrorState)
|
|
2244
|
+
continue;
|
|
2245
|
+
if (step.to.includes('sin cambio'))
|
|
2246
|
+
continue; // skip no-op taps
|
|
2247
|
+
const verifyElems = toScreen.elements.slice(0, 2).map(e => `\`${e.id}\``).join(', ');
|
|
2248
|
+
flows.push(`### Flujo ${flowNum++} — ${step.to} ✅\n` +
|
|
2249
|
+
`**Acción:** ${step.action}\n` +
|
|
2250
|
+
`**Verificar:** elementos visibles${verifyElems ? ` (${verifyElems})` : ''}`);
|
|
2251
|
+
}
|
|
2252
|
+
// Error state flows
|
|
2253
|
+
for (const screen of screens.filter(s => s.isErrorState)) {
|
|
2254
|
+
const step = metrics.navigationSteps.find(ns => ns.to === screen.name);
|
|
2255
|
+
if (!step)
|
|
2256
|
+
continue;
|
|
2257
|
+
const errorTexts = screen.allTexts.slice(0, 3).map(t => `"${t}"`).join(', ');
|
|
2258
|
+
flows.push(`### Flujo ${flowNum++} — ${screen.name} 🔴\n` +
|
|
2259
|
+
`**Acción:** ${step.action} (desde ${step.from})\n` +
|
|
2260
|
+
`**Verificar:** ${errorTexts || 'mensaje de error visible'}`);
|
|
2261
|
+
}
|
|
2262
|
+
const totalElements = screens.reduce((sum, s) => sum + s.elements.length, 0);
|
|
2263
|
+
const errorStates = screens.filter(s => s.isErrorState).length;
|
|
2264
|
+
return `# Discovery Plan: Mobile App (${appId})
|
|
2265
|
+
Generado: ${new Date().toISOString()}
|
|
2266
|
+
Duración: ${Math.round(metrics.durationMs / 1000)}s
|
|
2267
|
+
|
|
2268
|
+
## Resumen del App
|
|
2269
|
+
- **App ID**: ${appId}
|
|
2270
|
+
- **Pantallas descubiertas**: ${screens.length}
|
|
2271
|
+
- **Error states explorados**: ${errorStates}
|
|
2272
|
+
- **Elementos interactivos totales**: ${totalElements}
|
|
2273
|
+
- **Textos de error verificados**: ${verifiedTexts.length}
|
|
2274
|
+
- **Framework**: Maestro (Android native)
|
|
2275
|
+
- **Dispositivo**: Dispositivo físico (ADB)
|
|
2276
|
+
|
|
2277
|
+
## Mapa de Pantallas
|
|
2278
|
+
${screenSections}
|
|
2279
|
+
|
|
2280
|
+
${confidenceSection}
|
|
2281
|
+
|
|
2282
|
+
## Flujos de Prueba Sugeridos
|
|
2283
|
+
|
|
2284
|
+
${flows.join('\n\n')}
|
|
2285
|
+
|
|
2286
|
+
## UI Health
|
|
2287
|
+
- Total de nodos interactivos detectados: ${totalElements}
|
|
2288
|
+
- Pantallas mapeadas: ${screens.filter(s => !s.isErrorState).map(s => s.name).join(', ')}
|
|
2289
|
+
- Complejidad: ${totalElements > 15 ? 'Alta' : totalElements > 8 ? 'Media' : 'Baja'}
|
|
2290
|
+
`;
|
|
2291
|
+
},
|
|
2292
|
+
// ── Maestro element extractor ────────────────────────────────────────────
|
|
470
2293
|
extractMaestroSelectors(node) {
|
|
471
2294
|
const elements = [];
|
|
472
2295
|
const INTERACTIVE_CLASSES = [
|
|
@@ -477,34 +2300,72 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
477
2300
|
'android.widget.Switch',
|
|
478
2301
|
'android.widget.ImageButton',
|
|
479
2302
|
'android.widget.TextView',
|
|
2303
|
+
'android.view.ViewGroup', // Some apps use ViewGroups as buttons
|
|
480
2304
|
];
|
|
2305
|
+
// Known system/overlay elements that should never be explored
|
|
2306
|
+
const SYSTEM_BLACKLIST = [
|
|
2307
|
+
'trigger_container', // Samsung Edge Panels
|
|
2308
|
+
'navigation_bar_frame', // Android nav bar
|
|
2309
|
+
'status_bar_container', // Android status bar
|
|
2310
|
+
'com.android.systemui', // System UI elements
|
|
2311
|
+
];
|
|
2312
|
+
// Track the last visible text label seen during traversal.
|
|
2313
|
+
// Used to generate `below:` selectors for EditText fields that
|
|
2314
|
+
// lack their own resource-id, text, and hintText (e.g. "RUT", "Clave").
|
|
2315
|
+
let lastTextLabel = '';
|
|
2316
|
+
let editTextCount = 0;
|
|
481
2317
|
const traverse = (current) => {
|
|
482
2318
|
if (!current || typeof current !== 'object')
|
|
483
2319
|
return;
|
|
484
|
-
// maestro hierarchy JSON format: { attributes: {...}, children: [...] }
|
|
485
2320
|
const attrs = current.attributes || {};
|
|
486
2321
|
const resourceId = attrs['resource-id'] || '';
|
|
487
|
-
const text = attrs['text'] || attrs['accessibilityText'] || attrs['hintText'] || '';
|
|
2322
|
+
const text = (attrs['text'] || attrs['accessibilityText'] || attrs['hintText'] || '').trim();
|
|
488
2323
|
const cls = attrs['class'] || '';
|
|
489
2324
|
const clickable = attrs['clickable'] === 'true';
|
|
490
|
-
|
|
491
|
-
|
|
2325
|
+
const isEditText = cls.includes('EditText') || cls.includes('Input');
|
|
2326
|
+
if (isEditText) {
|
|
2327
|
+
editTextCount++;
|
|
2328
|
+
}
|
|
2329
|
+
// Non-interactive text elements (labels) update the context tracker
|
|
2330
|
+
const isTextView = cls.includes('TextView');
|
|
2331
|
+
if (isTextView && !clickable && text.length > 1) {
|
|
2332
|
+
lastTextLabel = text.substring(0, 40).trim();
|
|
2333
|
+
}
|
|
2334
|
+
const shortId = resourceId.includes('/') ? resourceId.split('/')[1] : resourceId;
|
|
2335
|
+
const isBlacklisted = SYSTEM_BLACKLIST.some(b => resourceId.includes(b));
|
|
2336
|
+
const isSystemId = resourceId.startsWith('android:id/') || resourceId.startsWith('com.android.');
|
|
492
2337
|
const isInteractive = INTERACTIVE_CLASSES.includes(cls) || clickable;
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
}
|
|
2338
|
+
const hasIdentifier = (resourceId && !isSystemId) || text.length > 1 || isEditText;
|
|
2339
|
+
if (!isBlacklisted && isInteractive && hasIdentifier) {
|
|
2340
|
+
let finalId = '';
|
|
2341
|
+
if (resourceId && !isSystemId) {
|
|
2342
|
+
finalId = shortId;
|
|
2343
|
+
}
|
|
2344
|
+
else if (text.length > 1) {
|
|
2345
|
+
finalId = `text:${text.substring(0, 64).trim()}`;
|
|
2346
|
+
}
|
|
2347
|
+
else if (isEditText && lastTextLabel) {
|
|
2348
|
+
// Use the nearest preceding label as a `below:` selector
|
|
2349
|
+
finalId = `below:${lastTextLabel}`;
|
|
2350
|
+
}
|
|
2351
|
+
else if (isEditText) {
|
|
2352
|
+
// Last resort: use index (may not work in all Maestro versions)
|
|
2353
|
+
finalId = `below:Campo ${editTextCount}`;
|
|
2354
|
+
}
|
|
2355
|
+
if (finalId) {
|
|
2356
|
+
elements.push({
|
|
2357
|
+
type: cls.split('.').pop() || cls,
|
|
2358
|
+
text: text || lastTextLabel || 'N/A',
|
|
2359
|
+
id: finalId,
|
|
2360
|
+
clickable,
|
|
2361
|
+
});
|
|
2362
|
+
}
|
|
499
2363
|
}
|
|
500
|
-
|
|
501
|
-
const children = current.children || [];
|
|
502
|
-
for (const child of children) {
|
|
2364
|
+
for (const child of (current.children || []))
|
|
503
2365
|
traverse(child);
|
|
504
|
-
}
|
|
505
2366
|
};
|
|
506
2367
|
traverse(node);
|
|
507
|
-
return elements.slice(0,
|
|
508
|
-
}
|
|
2368
|
+
return elements.slice(0, 40); // Increased limit to capture more elements
|
|
2369
|
+
},
|
|
509
2370
|
};
|
|
510
2371
|
//# sourceMappingURL=discovery.js.map
|