@flowspec-qa/runner-core 0.3.0 → 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.map +1 -1
- package/dist/discovery-report.js +669 -49
- package/dist/discovery-report.js.map +1 -1
- package/dist/discovery.d.ts +99 -5
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +1552 -263
- 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.map +1 -1
- package/dist/executor.js +49 -184
- package/dist/executor.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -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 +14 -4
- package/dist/suite-manager.js.map +1 -1
- package/package.json +1 -1
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,6 +29,61 @@ 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.
|
|
@@ -44,13 +101,137 @@ export const discoveryEngine = {
|
|
|
44
101
|
* returns it as a base64-encoded PNG string.
|
|
45
102
|
* Returns null silently if ADB capture fails (non-fatal).
|
|
46
103
|
*/
|
|
47
|
-
|
|
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) ─────────────────
|
|
48
230
|
try {
|
|
49
231
|
const remotePath = `/sdcard/fpqa_discovery_${tag}.png`;
|
|
50
|
-
const localPath = path.join(os.tmpdir(), `
|
|
232
|
+
const localPath = path.join(os.tmpdir(), `fpqa_disc_${tag}_${Date.now()}.png`);
|
|
51
233
|
execSync(`${adbS} shell screencap -p "${remotePath}"`, { stdio: 'ignore', timeout: 6000 });
|
|
52
234
|
execSync(`${adbS} pull "${remotePath}" "${localPath}"`, { stdio: 'ignore', timeout: 8000 });
|
|
53
|
-
// Clean up remote file
|
|
54
235
|
try {
|
|
55
236
|
execSync(`${adbS} shell rm "${remotePath}"`, { stdio: 'ignore', timeout: 3000 });
|
|
56
237
|
}
|
|
@@ -60,11 +241,15 @@ export const discoveryEngine = {
|
|
|
60
241
|
fs.unlinkSync(localPath);
|
|
61
242
|
}
|
|
62
243
|
catch { }
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
catch {
|
|
66
|
-
return null;
|
|
244
|
+
if (buffer.length > 1000)
|
|
245
|
+
return buffer.toString('base64');
|
|
67
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;
|
|
68
253
|
},
|
|
69
254
|
// ── Hierarchy helpers ────────────────────────────────────────────────────
|
|
70
255
|
/** Capture maestro hierarchy from current screen and return parsed JSON */
|
|
@@ -73,14 +258,48 @@ export const discoveryEngine = {
|
|
|
73
258
|
// Without it, maestro hierarchy prompts interactively and hangs in non-TTY contexts.
|
|
74
259
|
// Redirect stderr to /dev/null to suppress verbose INFO/DEBUG logs.
|
|
75
260
|
const deviceFlag = deviceId ? `--device ${deviceId} ` : '';
|
|
76
|
-
const
|
|
261
|
+
const maestro = resolveMaestro();
|
|
262
|
+
const rawOutput = execSync(`"${maestro}" ${deviceFlag}hierarchy 2>/dev/null`, {
|
|
77
263
|
encoding: 'utf8',
|
|
78
264
|
maxBuffer: 20 * 1024 * 1024, // 20 MB — hierarchy JSON can be large
|
|
79
|
-
env: env ??
|
|
265
|
+
env: { ...maestroEnvWithPath(), ...(env ?? {}) },
|
|
80
266
|
});
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
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);
|
|
84
303
|
return JSON.parse(jsonStr);
|
|
85
304
|
},
|
|
86
305
|
/** Execute a Maestro YAML flow (written to a temp file) */
|
|
@@ -89,24 +308,100 @@ export const discoveryEngine = {
|
|
|
89
308
|
const tmpFile = path.join(os.tmpdir(), `discovery_flow_${Date.now()}.yaml`);
|
|
90
309
|
fs.writeFileSync(tmpFile, yaml);
|
|
91
310
|
try {
|
|
92
|
-
const
|
|
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, {
|
|
93
317
|
encoding: 'utf8',
|
|
94
318
|
timeout: 120000,
|
|
95
319
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
96
|
-
maxBuffer: 20 * 1024 * 1024,
|
|
97
|
-
env: env ??
|
|
320
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
321
|
+
env: { ...maestroEnvWithPath(), ...(env ?? {}) },
|
|
98
322
|
});
|
|
99
323
|
fs.unlinkSync(tmpFile);
|
|
100
|
-
|
|
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
|
+
};
|
|
101
346
|
}
|
|
102
|
-
catch {
|
|
347
|
+
catch (e) {
|
|
103
348
|
try {
|
|
104
349
|
fs.unlinkSync(tmpFile);
|
|
105
350
|
}
|
|
106
351
|
catch { }
|
|
107
|
-
|
|
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' };
|
|
108
354
|
}
|
|
109
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
|
+
},
|
|
110
405
|
// ── Text extraction from accessibility hierarchy ─────────────────────────
|
|
111
406
|
/**
|
|
112
407
|
* Extracts ALL visible text strings from a Maestro hierarchy JSON.
|
|
@@ -177,6 +472,7 @@ export const discoveryEngine = {
|
|
|
177
472
|
framework: options.framework,
|
|
178
473
|
url: options.url,
|
|
179
474
|
});
|
|
475
|
+
result.reportPath = reportPath;
|
|
180
476
|
console.log(chalk.green(`📋 Reporte de evidencias: ${chalk.bold(reportPath)}`));
|
|
181
477
|
}
|
|
182
478
|
catch (reportErr) {
|
|
@@ -187,14 +483,38 @@ export const discoveryEngine = {
|
|
|
187
483
|
this._progress(options, 'sync', 'Sincronizando con FlowSpecQA SaaS...');
|
|
188
484
|
console.log(chalk.yellow('📡 Sincronizando con FlowSpecQA SaaS...'));
|
|
189
485
|
await FlowSpecQAApi.syncDiscoveryPlan({
|
|
486
|
+
project_id: options.projectId || 'TRUSTBANK',
|
|
487
|
+
entity_id: options.entityId || 'UNKNOWN',
|
|
190
488
|
url: options.url,
|
|
191
489
|
plan_content: result.planContent,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
project_id: options.projectId,
|
|
195
|
-
entity_id: options.entityId,
|
|
490
|
+
framework: options.framework || 'playwright',
|
|
491
|
+
evidence_corpus: result.corpus,
|
|
196
492
|
});
|
|
197
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
|
+
}
|
|
198
518
|
}
|
|
199
519
|
catch (error) {
|
|
200
520
|
const errorMsg = error.response?.data?.detail || error.message || String(error);
|
|
@@ -203,26 +523,26 @@ export const discoveryEngine = {
|
|
|
203
523
|
}
|
|
204
524
|
return result;
|
|
205
525
|
},
|
|
206
|
-
// ──
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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) {
|
|
211
539
|
const adb = resolveAdb();
|
|
212
|
-
const adbS =
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const navigationSteps = [];
|
|
219
|
-
this._progress(options, 'start', `Iniciando Discovery mobile: ${appId}`);
|
|
220
|
-
console.log(chalk.gray(`📱 Discovery multi-pantalla${options.deviceId ? ` — dispositivo ${options.deviceId}` : ''}...`));
|
|
221
|
-
try {
|
|
222
|
-
// ── Wake up screen + reset app state ──────────────────────────────
|
|
223
|
-
this._progress(options, 'reset', 'Reseteando estado de la app...');
|
|
224
|
-
console.log(chalk.gray(' 🔄 Reseteando estado de la app...'));
|
|
225
|
-
// Wake the screen — Maestro hierarchy fails silently when screen is off/locked
|
|
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) {
|
|
226
546
|
try {
|
|
227
547
|
execSync(`${adbS} shell input keyevent KEYCODE_WAKEUP`, { stdio: 'ignore' });
|
|
228
548
|
}
|
|
@@ -231,227 +551,1017 @@ export const discoveryEngine = {
|
|
|
231
551
|
execSync(`${adbS} shell wm dismiss-keyguard`, { stdio: 'ignore' });
|
|
232
552
|
}
|
|
233
553
|
catch { }
|
|
234
|
-
await sleep(
|
|
554
|
+
await sleep(400);
|
|
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
|
+
};
|
|
587
|
+
},
|
|
588
|
+
// ── Mobile Discovery ─────────────────────────────────────────────────────
|
|
589
|
+
async discoverMobile(options) {
|
|
590
|
+
const appId = options.url;
|
|
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) {
|
|
235
605
|
try {
|
|
236
|
-
|
|
606
|
+
const adb = resolveAdb();
|
|
607
|
+
adbS = deviceId ? `"${adb}" -s ${deviceId}` : `"${adb}"`;
|
|
237
608
|
}
|
|
238
|
-
catch {
|
|
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 }
|
|
615
|
+
: process.env;
|
|
616
|
+
const startedAt = Date.now();
|
|
617
|
+
const allScreens = [];
|
|
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) {
|
|
239
631
|
try {
|
|
240
|
-
|
|
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
|
+
}
|
|
241
641
|
}
|
|
242
|
-
catch {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
//
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const loginErrorScreenshot = await this._captureScreenshotMobile(adbS, 'login_error');
|
|
273
|
-
const loginErrorHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
|
|
274
|
-
const loginErrorAllElems = this.extractMaestroSelectors(loginErrorHierarchy);
|
|
275
|
-
const loginErrorTexts = this.extractAllTexts(loginErrorHierarchy);
|
|
276
|
-
const knownLoginIds = new Set(loginElements.map(e => e.id));
|
|
277
|
-
const loginErrorNew = loginErrorAllElems.filter(e => !knownLoginIds.has(e.id));
|
|
278
|
-
// New error texts = texts that weren't on the clean login screen
|
|
279
|
-
const loginNewTexts = loginErrorTexts.filter(t => !loginTexts.includes(t));
|
|
280
|
-
allScreens.push({
|
|
281
|
-
name: 'Login (Error — campos vacíos)',
|
|
282
|
-
isErrorState: true,
|
|
283
|
-
screenshot: loginErrorScreenshot,
|
|
284
|
-
elements: loginErrorNew,
|
|
285
|
-
allTexts: loginNewTexts,
|
|
286
|
-
});
|
|
287
|
-
navigationSteps.push({ from: 'Login', to: 'Login (Error — campos vacíos)', action: 'Submit vacío' });
|
|
288
|
-
const errList = loginNewTexts.slice(0, 3).map(t => `"${t}"`).join(', ');
|
|
289
|
-
console.log(chalk.green(` ✅ Login error state: ${loginErrorNew.length} elementos nuevos${errList ? ` | Textos: ${errList}` : ''}`));
|
|
642
|
+
catch (e) {
|
|
643
|
+
console.log(chalk.yellow(` ⚠️ No se pudo cargar baseline: ${e?.message || e}. Continuando en modo full.`));
|
|
644
|
+
baselines = null;
|
|
645
|
+
}
|
|
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.
|
|
290
672
|
}
|
|
291
673
|
else {
|
|
292
|
-
|
|
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
|
+
}
|
|
293
702
|
}
|
|
294
|
-
//
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
let postLoginHierarchy;
|
|
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);
|
|
316
724
|
try {
|
|
317
|
-
|
|
725
|
+
execSync(`"${maestro}" ${deviceFlag}test ${tmp}`, { stdio: ['ignore', 'pipe', 'pipe'], timeout: 30000 });
|
|
318
726
|
}
|
|
319
|
-
catch {
|
|
320
|
-
|
|
727
|
+
catch (err) {
|
|
728
|
+
const stderr = err.stderr?.toString() || '';
|
|
729
|
+
throw new Error(`Maestro falló: ${stderr.split('\n').slice(0, 5).join(' | ')}`);
|
|
321
730
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
731
|
+
finally {
|
|
732
|
+
try {
|
|
733
|
+
fs.unlinkSync(tmp);
|
|
734
|
+
}
|
|
735
|
+
catch { }
|
|
736
|
+
}
|
|
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.'));
|
|
751
|
+
}
|
|
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) {
|
|
328
866
|
await sleep(1500);
|
|
329
|
-
const
|
|
330
|
-
const
|
|
331
|
-
const
|
|
332
|
-
const
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
allTexts: accountsTexts,
|
|
339
|
-
});
|
|
340
|
-
navigationSteps.push({ from: 'Login', to: 'Cuentas', action: 'Login con credenciales válidas' });
|
|
341
|
-
console.log(chalk.green(` ✅ Cuentas: ${accountsElements.length} elementos, ${accountsScreenshot ? 'screenshot ✓' : 'sin screenshot'}`));
|
|
342
|
-
// ── Screen 3: Transfer ───────────────────────────────────────
|
|
343
|
-
this._progress(options, 'screen', 'Pantalla 3 — Transferencia');
|
|
344
|
-
console.log(chalk.gray(' 📸 Pantalla 3 — Transferencia...'));
|
|
345
|
-
const navSuccess = this._runMaestroFlow(appId, [
|
|
346
|
-
`- tapOn:\n id: "btn_transfer"`,
|
|
347
|
-
`- waitForAnimationToEnd`,
|
|
348
|
-
], maestroEnv);
|
|
349
|
-
if (navSuccess) {
|
|
350
|
-
await sleep(1200);
|
|
351
|
-
const transferScreenshot = await this._captureScreenshotMobile(adbS, 'transfer');
|
|
352
|
-
const transferHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
|
|
353
|
-
const transferElements = this.extractMaestroSelectors(transferHierarchy);
|
|
354
|
-
const transferTexts = this.extractAllTexts(transferHierarchy);
|
|
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)`;
|
|
355
876
|
allScreens.push({
|
|
356
|
-
name:
|
|
357
|
-
isErrorState:
|
|
358
|
-
screenshot:
|
|
359
|
-
elements:
|
|
360
|
-
allTexts:
|
|
877
|
+
name: errName,
|
|
878
|
+
isErrorState: true,
|
|
879
|
+
screenshot: emptyShot,
|
|
880
|
+
elements: newEls,
|
|
881
|
+
allTexts: newTxts,
|
|
361
882
|
});
|
|
362
|
-
navigationSteps.push({ from:
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
isErrorState: true,
|
|
383
|
-
screenshot: transferErrorScreenshot,
|
|
384
|
-
elements: transferErrorNew,
|
|
385
|
-
allTexts: transferNewTexts,
|
|
386
|
-
});
|
|
387
|
-
navigationSteps.push({ from: 'Transferencia', to: 'Transferencia (Error — monto vacío)', action: 'Submit vacío' });
|
|
388
|
-
const errList = transferNewTexts.slice(0, 3).map(t => `"${t}"`).join(', ');
|
|
389
|
-
console.log(chalk.green(` ✅ Transfer error state: ${transferErrorNew.length} elementos${errList ? ` | Textos: ${errList}` : ''}`));
|
|
390
|
-
}
|
|
391
|
-
else {
|
|
392
|
-
console.log(chalk.yellow(' ⚠️ No se pudo provocar error de transfer vacío'));
|
|
393
|
-
}
|
|
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);
|
|
394
903
|
}
|
|
395
904
|
else {
|
|
396
|
-
console.log(chalk.yellow(' ⚠️
|
|
397
|
-
}
|
|
398
|
-
// ── Error state: Credenciales inválidas ───────────────────────
|
|
399
|
-
this._progress(options, 'error_state', 'Credenciales inválidas — capturando mensaje real...');
|
|
400
|
-
console.log(chalk.gray(' 📸 Error state: Credenciales inválidas...'));
|
|
401
|
-
// Force a clean state via ADB before the Maestro flow — avoids capturing
|
|
402
|
-
// stale Transfer-screen elements if the previous error-state flow left the
|
|
403
|
-
// device on that screen and launchApp:clearState takes time.
|
|
404
|
-
try {
|
|
405
|
-
execSync(`${adbS} shell am force-stop ${appId}`, { stdio: 'ignore' });
|
|
406
|
-
execSync(`${adbS} shell pm clear ${appId}`, { stdio: 'ignore' });
|
|
905
|
+
console.log(chalk.yellow(' ⚠️ Submit vacío no produjo cambios visibles'));
|
|
407
906
|
}
|
|
408
|
-
|
|
409
|
-
|
|
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);
|
|
410
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.
|
|
411
921
|
`- launchApp`,
|
|
412
922
|
`- waitForAnimationToEnd:\n timeout: 3000`,
|
|
413
|
-
|
|
414
|
-
`-
|
|
415
|
-
`-
|
|
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`,
|
|
416
929
|
`- hideKeyboard`,
|
|
417
|
-
|
|
418
|
-
`-
|
|
419
|
-
`-
|
|
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`,
|
|
420
936
|
`- hideKeyboard`,
|
|
421
|
-
`-
|
|
422
|
-
`-
|
|
937
|
+
`- waitForAnimationToEnd:\n timeout: 2000`,
|
|
938
|
+
`- tapOn:\n ${subSelector}`,
|
|
939
|
+
`- waitForAnimationToEnd:\n timeout: 5000`,
|
|
423
940
|
], maestroEnv);
|
|
424
|
-
|
|
425
|
-
|
|
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;
|
|
426
952
|
try {
|
|
427
|
-
|
|
953
|
+
postLoginHier = await this._captureHierarchy(maestroEnv, options.deviceId);
|
|
428
954
|
}
|
|
429
955
|
catch { }
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
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' });
|
|
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);
|
|
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;
|
|
448
1025
|
}
|
|
1026
|
+
// loggedIn stays false → BFS will explore pre-auth buttons
|
|
449
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;
|
|
1063
|
+
}
|
|
1064
|
+
else {
|
|
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);
|
|
1084
|
+
}
|
|
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'));
|
|
450
1129
|
}
|
|
451
1130
|
else {
|
|
452
|
-
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
|
+
}
|
|
453
1555
|
}
|
|
454
1556
|
}
|
|
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
|
+
}
|
|
455
1565
|
// ── Build evidence package ─────────────────────────────────────────
|
|
456
1566
|
const finishedAt = Date.now();
|
|
457
1567
|
const totalElements = allScreens.reduce((sum, s) => sum + s.elements.length, 0);
|
|
@@ -486,6 +1596,7 @@ export const discoveryEngine = {
|
|
|
486
1596
|
framework: 'maestro',
|
|
487
1597
|
},
|
|
488
1598
|
evidence,
|
|
1599
|
+
corpus: finalCorpus,
|
|
489
1600
|
};
|
|
490
1601
|
}
|
|
491
1602
|
catch (error) {
|
|
@@ -701,14 +1812,35 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
701
1812
|
* These become the candidates for the interactive exploration loop.
|
|
702
1813
|
*/
|
|
703
1814
|
detectUnexplored(evidence) {
|
|
704
|
-
// Collect element IDs that were explicitly tapped
|
|
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)
|
|
705
1823
|
const exploredIds = new Set();
|
|
706
1824
|
for (const step of evidence.metrics.navigationSteps) {
|
|
707
|
-
|
|
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);
|
|
708
1827
|
if (tapMatch)
|
|
709
1828
|
exploredIds.add(tapMatch[1]);
|
|
710
1829
|
}
|
|
711
|
-
//
|
|
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)
|
|
712
1844
|
const ALWAYS_HANDLED = new Set([
|
|
713
1845
|
'btn_login', 'btn_confirm', 'et_username', 'et_password',
|
|
714
1846
|
'btn_submit', 'btn_register', 'btn_back',
|
|
@@ -727,11 +1859,22 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
727
1859
|
continue;
|
|
728
1860
|
if (ALWAYS_HANDLED.has(elem.id))
|
|
729
1861
|
continue;
|
|
730
|
-
//
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
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))
|
|
735
1878
|
continue;
|
|
736
1879
|
seenIds.add(elem.id);
|
|
737
1880
|
unexplored.push({
|
|
@@ -748,7 +1891,7 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
748
1891
|
* Convert a navigation step action string into the Maestro YAML steps
|
|
749
1892
|
* needed to perform that action.
|
|
750
1893
|
*/
|
|
751
|
-
_getActionYaml(action, creds) {
|
|
1894
|
+
_getActionYaml(action, creds, isIos = false) {
|
|
752
1895
|
if (action === 'Lanzar app') {
|
|
753
1896
|
return [
|
|
754
1897
|
`- launchApp:\n clearState: true`,
|
|
@@ -763,7 +1906,7 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
763
1906
|
`- waitForAnimationToEnd`,
|
|
764
1907
|
`- hideKeyboard`,
|
|
765
1908
|
`- tapOn:\n id: "et_password"`,
|
|
766
|
-
`- eraseText:
|
|
1909
|
+
`- eraseText: 50`,
|
|
767
1910
|
`- inputText: "${creds.password}"`,
|
|
768
1911
|
`- waitForAnimationToEnd`,
|
|
769
1912
|
`- hideKeyboard`,
|
|
@@ -774,7 +1917,7 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
774
1917
|
const tapMatch = action.match(/^Tap\s+(\S+)/i);
|
|
775
1918
|
if (tapMatch) {
|
|
776
1919
|
return [
|
|
777
|
-
`- tapOn:\n
|
|
1920
|
+
`- tapOn:\n ${buildMaestroSelector(tapMatch[1], isIos)}`,
|
|
778
1921
|
`- waitForAnimationToEnd`,
|
|
779
1922
|
];
|
|
780
1923
|
}
|
|
@@ -785,7 +1928,7 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
785
1928
|
* navigates from the app's initial state to `targetScreen`.
|
|
786
1929
|
* Returns true if navigation succeeded, false otherwise.
|
|
787
1930
|
*/
|
|
788
|
-
async _navigateToScreen(targetScreen, evidence, creds, appId, maestroEnv) {
|
|
1931
|
+
async _navigateToScreen(targetScreen, evidence, creds, appId, maestroEnv, isIos = false) {
|
|
789
1932
|
// Build adjacency list — skip edges that lead to error states
|
|
790
1933
|
const graph = new Map();
|
|
791
1934
|
for (const step of evidence.metrics.navigationSteps) {
|
|
@@ -808,13 +1951,13 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
808
1951
|
if (screen === targetScreen) {
|
|
809
1952
|
if (actions.length === 0)
|
|
810
1953
|
return true;
|
|
811
|
-
return this._runMaestroFlow(appId, actions, maestroEnv);
|
|
1954
|
+
return this._runMaestroFlow(appId, actions, maestroEnv).ok;
|
|
812
1955
|
}
|
|
813
1956
|
if (visited.has(screen))
|
|
814
1957
|
continue;
|
|
815
1958
|
visited.add(screen);
|
|
816
1959
|
for (const edge of (graph.get(screen) || [])) {
|
|
817
|
-
const stepYaml = this._getActionYaml(edge.action, creds);
|
|
1960
|
+
const stepYaml = this._getActionYaml(edge.action, creds, isIos);
|
|
818
1961
|
queue.push({ screen: edge.to, actions: [...actions, ...stepYaml] });
|
|
819
1962
|
}
|
|
820
1963
|
}
|
|
@@ -830,7 +1973,7 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
830
1973
|
*
|
|
831
1974
|
* Returns the new ScreenEvidence entries that were added.
|
|
832
1975
|
*/
|
|
833
|
-
async exploreElement(element, options, evidence) {
|
|
1976
|
+
async exploreElement(element, options, evidence, corpus) {
|
|
834
1977
|
const appId = options.url;
|
|
835
1978
|
const adb = resolveAdb();
|
|
836
1979
|
const adbS = options.deviceId ? `"${adb}" -s ${options.deviceId}` : `"${adb}"`;
|
|
@@ -839,26 +1982,30 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
839
1982
|
: process.env;
|
|
840
1983
|
const creds = { username: options.username || '', password: options.password || '' };
|
|
841
1984
|
const added = [];
|
|
1985
|
+
const isIos = !!options.deviceId && /iPhone|iPad/i.test(options.deviceId);
|
|
842
1986
|
console.log(chalk.gray(` 🔭 Navegando a "${element.sourceScreen}"...`));
|
|
843
1987
|
// 1. Navigate to the source screen from app start
|
|
844
|
-
const navOk = await this._navigateToScreen(element.sourceScreen, evidence, creds, appId, maestroEnv);
|
|
1988
|
+
const navOk = await this._navigateToScreen(element.sourceScreen, evidence, creds, appId, maestroEnv, isIos);
|
|
845
1989
|
if (!navOk) {
|
|
846
1990
|
console.log(chalk.yellow(` ⚠️ No se pudo navegar a "${element.sourceScreen}"`));
|
|
847
1991
|
return [];
|
|
848
1992
|
}
|
|
849
1993
|
await sleep(2000); // wait for screen to settle
|
|
850
|
-
// 2.
|
|
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
|
|
851
1998
|
console.log(chalk.gray(` 👆 Tap en ${element.elementId}...`));
|
|
852
1999
|
const tapOk = this._runMaestroFlow(appId, [
|
|
853
|
-
`- tapOn:\n
|
|
2000
|
+
`- tapOn:\n ${buildMaestroSelector(element.elementId, isIos)}`,
|
|
854
2001
|
`- waitForAnimationToEnd:\n timeout: 3000`,
|
|
855
|
-
], maestroEnv);
|
|
2002
|
+
], maestroEnv).ok;
|
|
856
2003
|
if (!tapOk) {
|
|
857
2004
|
console.log(chalk.yellow(` ⚠️ Tap en ${element.elementId} falló`));
|
|
858
2005
|
return [];
|
|
859
2006
|
}
|
|
860
2007
|
await sleep(2000); // wait for the new screen to render
|
|
861
|
-
//
|
|
2008
|
+
// 4. Capture resulting state
|
|
862
2009
|
let hierarchy;
|
|
863
2010
|
try {
|
|
864
2011
|
hierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
|
|
@@ -871,7 +2018,6 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
871
2018
|
const newElements = this.extractMaestroSelectors(hierarchy);
|
|
872
2019
|
const newTexts = this.extractAllTexts(hierarchy);
|
|
873
2020
|
// Decide if this looks like a new screen
|
|
874
|
-
const sourceScreenData = evidence.screens.find(s => s.name === element.sourceScreen);
|
|
875
2021
|
const sourceIds = new Set((sourceScreenData?.elements || []).map(e => e.id));
|
|
876
2022
|
const novelElements = newElements.filter(e => !sourceIds.has(e.id));
|
|
877
2023
|
if (novelElements.length < 2 && !screenshot) {
|
|
@@ -904,21 +2050,121 @@ ${elements.slice(0, 20).map((e) => {
|
|
|
904
2050
|
});
|
|
905
2051
|
evidence.metrics.screensDiscovered++;
|
|
906
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
|
+
}
|
|
907
2074
|
// Track verified IDs from the new screen
|
|
908
2075
|
for (const e of newElements) {
|
|
909
2076
|
if (e.id && !evidence.verifiedIds.includes(e.id))
|
|
910
2077
|
evidence.verifiedIds.push(e.id);
|
|
911
2078
|
}
|
|
912
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
|
+
}
|
|
913
2159
|
// 5. Try to trigger error state on new screen (tap first submit-like button without filling fields)
|
|
914
2160
|
const submitBtn = newElements.find(e => ['btn_confirm', 'btn_submit', 'btn_pay', 'btn_send', 'btn_continue', 'btn_accept'].includes(e.id) ||
|
|
915
2161
|
(e.type === 'Button' && e.id !== element.elementId));
|
|
916
2162
|
if (submitBtn) {
|
|
917
2163
|
console.log(chalk.gray(` 📸 Intentando error state (${submitBtn.id})...`));
|
|
918
2164
|
const errorOk = this._runMaestroFlow(appId, [
|
|
919
|
-
`- tapOn:\n
|
|
2165
|
+
`- tapOn:\n ${buildMaestroSelector(submitBtn.id, isIos)}`,
|
|
920
2166
|
`- waitForAnimationToEnd:\n timeout: 1500`,
|
|
921
|
-
], maestroEnv);
|
|
2167
|
+
], maestroEnv).ok;
|
|
922
2168
|
if (errorOk) {
|
|
923
2169
|
await sleep(1500);
|
|
924
2170
|
try {
|
|
@@ -1054,29 +2300,72 @@ ${flows.join('\n\n')}
|
|
|
1054
2300
|
'android.widget.Switch',
|
|
1055
2301
|
'android.widget.ImageButton',
|
|
1056
2302
|
'android.widget.TextView',
|
|
2303
|
+
'android.view.ViewGroup', // Some apps use ViewGroups as buttons
|
|
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
|
|
1057
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;
|
|
1058
2317
|
const traverse = (current) => {
|
|
1059
2318
|
if (!current || typeof current !== 'object')
|
|
1060
2319
|
return;
|
|
1061
2320
|
const attrs = current.attributes || {};
|
|
1062
2321
|
const resourceId = attrs['resource-id'] || '';
|
|
1063
|
-
const text = attrs['text'] || attrs['accessibilityText'] || attrs['hintText'] || '';
|
|
2322
|
+
const text = (attrs['text'] || attrs['accessibilityText'] || attrs['hintText'] || '').trim();
|
|
1064
2323
|
const cls = attrs['class'] || '';
|
|
1065
2324
|
const clickable = attrs['clickable'] === 'true';
|
|
1066
|
-
const
|
|
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.');
|
|
1067
2337
|
const isInteractive = INTERACTIVE_CLASSES.includes(cls) || clickable;
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
}
|
|
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
|
+
}
|
|
1074
2363
|
}
|
|
1075
2364
|
for (const child of (current.children || []))
|
|
1076
2365
|
traverse(child);
|
|
1077
2366
|
};
|
|
1078
2367
|
traverse(node);
|
|
1079
|
-
return elements.slice(0,
|
|
2368
|
+
return elements.slice(0, 40); // Increased limit to capture more elements
|
|
1080
2369
|
},
|
|
1081
2370
|
};
|
|
1082
2371
|
//# sourceMappingURL=discovery.js.map
|