@monkeytestai/monkeytester 0.5.1 → 0.6.1
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/CHANGELOG.md +45 -0
- package/README.md +15 -3
- package/dist/automation/signatures.d.ts +70 -0
- package/dist/automation/signatures.d.ts.map +1 -0
- package/dist/automation/signatures.js +152 -0
- package/dist/automation/signatures.js.map +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +44 -9
- package/dist/cli.js.map +1 -1
- package/dist/depth.d.ts +40 -0
- package/dist/depth.d.ts.map +1 -0
- package/dist/depth.js +70 -0
- package/dist/depth.js.map +1 -0
- package/dist/explorer/index.d.ts.map +1 -1
- package/dist/explorer/index.js +102 -17
- package/dist/explorer/index.js.map +1 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/monkey.d.ts.map +1 -1
- package/dist/monkey.js +41 -14
- package/dist/monkey.js.map +1 -1
- package/dist/outcome.d.ts +48 -0
- package/dist/outcome.d.ts.map +1 -0
- package/dist/outcome.js +366 -0
- package/dist/outcome.js.map +1 -0
- package/dist/planner/focus.d.ts +13 -0
- package/dist/planner/focus.d.ts.map +1 -0
- package/dist/planner/focus.js +136 -0
- package/dist/planner/focus.js.map +1 -0
- package/dist/planner/forms.d.ts +5 -0
- package/dist/planner/forms.d.ts.map +1 -1
- package/dist/planner/forms.js +16 -5
- package/dist/planner/forms.js.map +1 -1
- package/dist/planner/index.d.ts +8 -1
- package/dist/planner/index.d.ts.map +1 -1
- package/dist/planner/index.js +46 -9
- package/dist/planner/index.js.map +1 -1
- package/dist/planner/inventory.d.ts +23 -0
- package/dist/planner/inventory.d.ts.map +1 -0
- package/dist/planner/inventory.js +203 -0
- package/dist/planner/inventory.js.map +1 -0
- package/dist/planner/verdict.d.ts +13 -0
- package/dist/planner/verdict.d.ts.map +1 -0
- package/dist/planner/verdict.js +47 -0
- package/dist/planner/verdict.js.map +1 -0
- package/dist/runner/actions.d.ts +24 -0
- package/dist/runner/actions.d.ts.map +1 -1
- package/dist/runner/actions.js +328 -53
- package/dist/runner/actions.js.map +1 -1
- package/dist/runner/flowStatus.d.ts +9 -0
- package/dist/runner/flowStatus.d.ts.map +1 -0
- package/dist/runner/flowStatus.js +16 -0
- package/dist/runner/flowStatus.js.map +1 -0
- package/dist/runner/index.d.ts.map +1 -1
- package/dist/runner/index.js +17 -2
- package/dist/runner/index.js.map +1 -1
- package/dist/runner/locate.d.ts +41 -0
- package/dist/runner/locate.d.ts.map +1 -0
- package/dist/runner/locate.js +507 -0
- package/dist/runner/locate.js.map +1 -0
- package/dist/runner/replay.d.ts.map +1 -1
- package/dist/runner/replay.js +8 -3
- package/dist/runner/replay.js.map +1 -1
- package/dist/triager/index.d.ts +16 -2
- package/dist/triager/index.d.ts.map +1 -1
- package/dist/triager/index.js +77 -3
- package/dist/triager/index.js.map +1 -1
- package/dist/types.d.ts +110 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/runner/actions.js
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { z } from 'zod';
|
|
6
6
|
import { clipText, coerceEnum, coerceToString, unwrapModelObject } from '../llm/coerce.js';
|
|
7
|
+
import { isAutomationFault } from '../automation/signatures.js';
|
|
8
|
+
import { impliedType, locateActionTarget, pageFn } from './locate.js';
|
|
7
9
|
const ACTION_TOOLS = ['click', 'fill', 'goto', 'wait', 'assert_text', 'press', 'noop'];
|
|
8
10
|
const ACTION_LOCATORS = ['css', 'role', 'text', 'placeholder', 'label', 'testid'];
|
|
9
11
|
const TOOL_ALIASES = {
|
|
@@ -75,7 +77,13 @@ export function directActionFromStep(action, skeleton) {
|
|
|
75
77
|
if (fill) {
|
|
76
78
|
const field = fill[1].trim();
|
|
77
79
|
const nameHint = action.match(/\bname\s*=\s*["“']([^"”']+)["”']/i)?.[1]?.trim();
|
|
78
|
-
const
|
|
80
|
+
const typeHint = action.match(/input\[type=(?:"|')?([a-z0-9-]+)/i)?.[1]?.toLowerCase();
|
|
81
|
+
const typed = typeHint
|
|
82
|
+
? skeleton.inputs.filter(item => (item.type || '').toLowerCase() === typeHint)
|
|
83
|
+
: [];
|
|
84
|
+
const input = (nameHint && findInput(skeleton.inputs, nameHint))
|
|
85
|
+
|| findInput(skeleton.inputs, field)
|
|
86
|
+
|| (typed.length === 1 ? typed[0] : undefined);
|
|
79
87
|
if (!input)
|
|
80
88
|
return null;
|
|
81
89
|
return fillActionForInput(input, undefined, `Field "${field}" is on the page.`);
|
|
@@ -128,34 +136,42 @@ export function groundFillAction(action, skeleton) {
|
|
|
128
136
|
if (!wanted)
|
|
129
137
|
return action;
|
|
130
138
|
if (action.locator === 'css' && (inputByNameSelector(skeleton.inputs, wanted) || /[[\]#.=]/.test(wanted))) {
|
|
131
|
-
|
|
139
|
+
const known = inputByNameSelector(skeleton.inputs, wanted);
|
|
140
|
+
if (!known)
|
|
141
|
+
return action;
|
|
142
|
+
const value = valueForInput(known, action.value);
|
|
143
|
+
return action.value === value ? action : { ...action, value };
|
|
132
144
|
}
|
|
133
145
|
const placeholderOnly = action.locator === 'placeholder'
|
|
134
146
|
? findInput(skeleton.inputs, wanted, 'placeholder')
|
|
135
147
|
: undefined;
|
|
136
|
-
if (placeholderOnly && !placeholderOnly.label && !placeholderOnly.name)
|
|
137
|
-
|
|
148
|
+
if (placeholderOnly && !placeholderOnly.label && !placeholderOnly.name) {
|
|
149
|
+
const value = valueForInput(placeholderOnly, action.value);
|
|
150
|
+
return value === action.value ? action : { ...action, value };
|
|
151
|
+
}
|
|
138
152
|
const input = placeholderOnly || findInput(skeleton.inputs, wanted);
|
|
139
|
-
if (!input
|
|
153
|
+
if (!input)
|
|
154
|
+
return action;
|
|
155
|
+
if (!input.label && !input.name && !input.id && !input.placeholder && !input.type && !input.preceding)
|
|
140
156
|
return action;
|
|
141
157
|
const grounded = fillActionForInput(input, action.value, action.rationale);
|
|
158
|
+
const withValue = action.value === grounded.value ? action : { ...action, value: grounded.value };
|
|
142
159
|
if (action.locator === grounded.locator && action.target === grounded.target)
|
|
143
|
-
return
|
|
160
|
+
return withValue;
|
|
144
161
|
const inventedPlaceholder = action.locator === 'placeholder';
|
|
145
162
|
const labelMiss = action.locator === 'label' && !(input.label && sameField(input.label, wanted) && action.target === input.label);
|
|
146
163
|
const bareCss = !action.locator || (action.locator === 'css' && !/[[\]#.=]/.test(wanted));
|
|
147
164
|
if (!inventedPlaceholder && !labelMiss && !bareCss)
|
|
148
|
-
return
|
|
165
|
+
return withValue;
|
|
149
166
|
return {
|
|
150
|
-
...
|
|
167
|
+
...withValue,
|
|
151
168
|
locator: grounded.locator,
|
|
152
169
|
target: grounded.target,
|
|
153
|
-
value: action.value ?? grounded.value,
|
|
154
170
|
rationale: grounded.rationale,
|
|
155
171
|
};
|
|
156
172
|
}
|
|
157
173
|
function fillActionForInput(input, value, rationale) {
|
|
158
|
-
const sample =
|
|
174
|
+
const sample = valueForInput(input, value);
|
|
159
175
|
if (input.label) {
|
|
160
176
|
return {
|
|
161
177
|
tool: 'fill',
|
|
@@ -174,6 +190,36 @@ function fillActionForInput(input, value, rationale) {
|
|
|
174
190
|
rationale: rationale || `Input name "${input.name}" is on the page.`,
|
|
175
191
|
};
|
|
176
192
|
}
|
|
193
|
+
if (input.id) {
|
|
194
|
+
return {
|
|
195
|
+
tool: 'fill',
|
|
196
|
+
locator: 'css',
|
|
197
|
+
target: `#${input.id.replace(/\\/g, '\\\\')}`,
|
|
198
|
+
value: sample,
|
|
199
|
+
rationale: rationale || `Input id "${input.id}" is on the page.`,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
if (input.placeholder) {
|
|
203
|
+
return {
|
|
204
|
+
tool: 'fill',
|
|
205
|
+
locator: 'placeholder',
|
|
206
|
+
target: input.placeholder,
|
|
207
|
+
value: sample,
|
|
208
|
+
rationale: rationale || `Placeholder "${input.placeholder}" is on the page.`,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
if (input.type && input.type !== 'text') {
|
|
212
|
+
const target = input.type === 'textarea' || input.type === 'select'
|
|
213
|
+
? input.type
|
|
214
|
+
: `input[type="${input.type}"]`;
|
|
215
|
+
return {
|
|
216
|
+
tool: 'fill',
|
|
217
|
+
locator: 'css',
|
|
218
|
+
target,
|
|
219
|
+
value: sample,
|
|
220
|
+
rationale: rationale || `Input type "${input.type}" is on the page.`,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
177
223
|
return {
|
|
178
224
|
tool: 'fill',
|
|
179
225
|
locator: 'placeholder',
|
|
@@ -183,8 +229,27 @@ function fillActionForInput(input, value, rationale) {
|
|
|
183
229
|
};
|
|
184
230
|
}
|
|
185
231
|
function findInput(inputs, wanted, only) {
|
|
186
|
-
const keys = only ? [only] : ['name', 'label', 'placeholder'];
|
|
187
|
-
|
|
232
|
+
const keys = only ? [only] : ['name', 'label', 'placeholder', 'id', 'preceding'];
|
|
233
|
+
const exact = inputs.find(input => keys.some(key => sameField(input[key], wanted)));
|
|
234
|
+
if (exact || only)
|
|
235
|
+
return exact;
|
|
236
|
+
const loose = inputs.filter(input => looseField(input, wanted));
|
|
237
|
+
return loose.length === 1 ? loose[0] : undefined;
|
|
238
|
+
}
|
|
239
|
+
function looseField(input, wanted) {
|
|
240
|
+
const implied = impliedType(wanted);
|
|
241
|
+
const type = (input.type || '').toLowerCase();
|
|
242
|
+
if (implied === 'number' && (type === 'number' || type === 'range'))
|
|
243
|
+
return true;
|
|
244
|
+
if (implied === 'text' && (type === 'text' || type === 'textarea' || type === 'email' || type === 'search' || type === 'tel' || type === 'url' || type === 'password'))
|
|
245
|
+
return true;
|
|
246
|
+
if (implied && implied !== 'text' && implied !== 'number' && type === implied)
|
|
247
|
+
return true;
|
|
248
|
+
const wantedTokens = wanted.toLowerCase().split(/[^a-z0-9]+/).filter(token => token.length > 1 && !['input', 'inputs', 'field', 'fields', 'box', 'the', 'control', 'element'].includes(token));
|
|
249
|
+
if (wantedTokens.length === 0)
|
|
250
|
+
return false;
|
|
251
|
+
const hay = `${input.label || ''} ${input.name || ''} ${input.placeholder || ''} ${input.preceding || ''} ${input.heading || ''} ${input.type || ''}`.toLowerCase();
|
|
252
|
+
return wantedTokens.every(token => hay.includes(token));
|
|
188
253
|
}
|
|
189
254
|
function inputByNameSelector(inputs, target) {
|
|
190
255
|
return inputs.find(input => input.name && nameSelector(input.name) === target);
|
|
@@ -196,22 +261,82 @@ function nameSelector(name) {
|
|
|
196
261
|
return `[name="${name.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"]`;
|
|
197
262
|
}
|
|
198
263
|
function sampleFieldValue(input) {
|
|
199
|
-
|
|
200
|
-
|
|
264
|
+
return valueForInput(input);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* A value Playwright can type into this control.
|
|
268
|
+
*
|
|
269
|
+
* `input[type=number]` and `tel` always get digits. `555-0100` is not a
|
|
270
|
+
* number, and Playwright throws "Cannot type text into input" if it is
|
|
271
|
+
* typed into a number field. A caller-supplied value is kept when it
|
|
272
|
+
* already matches the type, and rewritten when it does not.
|
|
273
|
+
*/
|
|
274
|
+
export function valueForInput(input, proposed) {
|
|
275
|
+
const type = (input.type || '').toLowerCase();
|
|
276
|
+
const blob = `${type} ${input.name || ''} ${input.label || ''} ${input.placeholder || ''}`.toLowerCase();
|
|
277
|
+
const given = proposed?.trim();
|
|
278
|
+
if (type === 'number' || type === 'range') {
|
|
279
|
+
return numericText(given, blob.includes('phone') || blob.includes('tel') ? '5550100' : '1');
|
|
280
|
+
}
|
|
281
|
+
if (type === 'tel')
|
|
282
|
+
return numericText(given, '5550100');
|
|
283
|
+
if (type === 'email' || (type !== 'number' && blob.includes('email'))) {
|
|
284
|
+
if (given && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(given))
|
|
285
|
+
return given;
|
|
201
286
|
return 'qa@example.com';
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
287
|
+
}
|
|
288
|
+
if (type === 'url' || blob.includes('url') || blob.includes('website')) {
|
|
289
|
+
if (given && /^https?:\/\//i.test(given))
|
|
290
|
+
return given;
|
|
205
291
|
return 'https://example.com';
|
|
206
|
-
|
|
292
|
+
}
|
|
293
|
+
if (type === 'date' || (type !== 'datetime-local' && /\bdate\b/.test(blob))) {
|
|
294
|
+
if (given && /^\d{4}-\d{2}-\d{2}$/.test(given))
|
|
295
|
+
return given;
|
|
207
296
|
return '2026-01-15';
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
297
|
+
}
|
|
298
|
+
if (type === 'datetime-local') {
|
|
299
|
+
if (given && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(given))
|
|
300
|
+
return given;
|
|
301
|
+
return '2026-01-15T09:00';
|
|
302
|
+
}
|
|
303
|
+
if (type === 'time') {
|
|
304
|
+
if (given && /^\d{2}:\d{2}/.test(given))
|
|
305
|
+
return given;
|
|
306
|
+
return '09:00';
|
|
307
|
+
}
|
|
308
|
+
if (type === 'month')
|
|
309
|
+
return given && /^\d{4}-\d{2}$/.test(given) ? given : '2026-01';
|
|
310
|
+
if (type === 'week')
|
|
311
|
+
return given && /^\d{4}-W\d{2}$/.test(given) ? given : '2026-W03';
|
|
312
|
+
if (type === 'color')
|
|
313
|
+
return given && /^#[0-9a-f]{6}$/i.test(given) ? given : '#336699';
|
|
314
|
+
if (type === 'password')
|
|
315
|
+
return given && given.length >= 8 ? given : 'Test-password-1';
|
|
316
|
+
if (type === 'search' || blob.includes('search'))
|
|
317
|
+
return given && given.length > 0 ? given : 'banana';
|
|
318
|
+
if (blob.includes('phone') || blob.includes('tel'))
|
|
319
|
+
return numericText(given, '5550100');
|
|
320
|
+
if (blob.includes('qty') || blob.includes('quantity') || blob.includes('amount'))
|
|
321
|
+
return numericText(given, '1');
|
|
322
|
+
if (blob.includes('message') || blob.includes('comment') || type === 'textarea') {
|
|
323
|
+
return given && given.length > 0 ? given : 'Hello from monkeytest';
|
|
324
|
+
}
|
|
212
325
|
if (blob.includes('name'))
|
|
213
|
-
return 'Test User';
|
|
214
|
-
return 'test';
|
|
326
|
+
return given && given.length > 0 ? given : 'Test User';
|
|
327
|
+
return given && given.length > 0 ? given : 'test';
|
|
328
|
+
}
|
|
329
|
+
/** Digits only. `555-0100` becomes `5550100`. A non-numeric word becomes `fallback`. */
|
|
330
|
+
export function numericText(proposed, fallback) {
|
|
331
|
+
if (proposed) {
|
|
332
|
+
const trimmed = proposed.trim();
|
|
333
|
+
if (/^-?\d+(\.\d+)?$/.test(trimmed))
|
|
334
|
+
return trimmed;
|
|
335
|
+
const digits = trimmed.replace(/\D/g, '');
|
|
336
|
+
if (digits)
|
|
337
|
+
return digits;
|
|
338
|
+
}
|
|
339
|
+
return fallback;
|
|
215
340
|
}
|
|
216
341
|
export async function resolveAction(llm, flow, step, skeleton) {
|
|
217
342
|
const prompt = `
|
|
@@ -276,20 +401,38 @@ export async function executeAction(page, action, timeoutMs, options) {
|
|
|
276
401
|
try {
|
|
277
402
|
switch (action.tool) {
|
|
278
403
|
case 'click': {
|
|
279
|
-
const
|
|
280
|
-
if (
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
404
|
+
const located = await elementFor(page, action, options?.located);
|
|
405
|
+
if ('reason' in located)
|
|
406
|
+
return miss(located.reason);
|
|
407
|
+
const locator = located.locator;
|
|
408
|
+
const acted = located.action;
|
|
409
|
+
const disabled = await disabledOutcome(locator, acted, options?.strictOracles);
|
|
410
|
+
if (disabled)
|
|
411
|
+
return disabled;
|
|
412
|
+
await locator.click({ timeout: timeoutMs });
|
|
286
413
|
await settleSPA(page).catch(() => { });
|
|
287
|
-
return
|
|
414
|
+
return done('passed', `Clicked ${describeTarget(acted)}.`, acted);
|
|
288
415
|
}
|
|
289
416
|
case 'fill': {
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
417
|
+
const located = await elementFor(page, action, options?.located);
|
|
418
|
+
if ('reason' in located)
|
|
419
|
+
return miss(located.reason);
|
|
420
|
+
const locator = located.locator;
|
|
421
|
+
const acted = located.action;
|
|
422
|
+
const disabled = await disabledOutcome(locator, acted, options?.strictOracles);
|
|
423
|
+
if (disabled)
|
|
424
|
+
return disabled;
|
|
425
|
+
const meta = await readFillTarget(locator);
|
|
426
|
+
if (meta?.type === 'checkbox' || meta?.type === 'radio') {
|
|
427
|
+
await locator.check({ timeout: timeoutMs });
|
|
428
|
+
return done('passed', `Checked ${describeTarget(acted)}.`, acted);
|
|
429
|
+
}
|
|
430
|
+
const safeValue = meta
|
|
431
|
+
? valueForInput(meta, action.value ?? '')
|
|
432
|
+
: (action.value ?? '');
|
|
433
|
+
const numeric = meta?.type === 'number' || meta?.type === 'tel' || meta?.type === 'range';
|
|
434
|
+
await fillWithFallback(page, locator, safeValue, timeoutMs, numeric);
|
|
435
|
+
return done('passed', `Typed "${truncate(safeValue, 40)}" into ${describeTarget(acted)}.`, acted);
|
|
293
436
|
}
|
|
294
437
|
case 'goto': {
|
|
295
438
|
const url = action.target || '';
|
|
@@ -301,9 +444,11 @@ export async function executeAction(page, action, timeoutMs, options) {
|
|
|
301
444
|
return { status: 'passed', observation: `Navigated to ${url}.` };
|
|
302
445
|
}
|
|
303
446
|
case 'wait': {
|
|
304
|
-
const
|
|
305
|
-
|
|
306
|
-
|
|
447
|
+
const located = await elementFor(page, action, options?.located);
|
|
448
|
+
if ('reason' in located)
|
|
449
|
+
return miss(located.reason);
|
|
450
|
+
await located.locator.waitFor({ state: 'visible', timeout: Math.min(timeoutMs, 1_000) });
|
|
451
|
+
return done('passed', `Confirmed ${describeTarget(located.action)} is visible.`, located.action);
|
|
307
452
|
}
|
|
308
453
|
case 'assert_text': {
|
|
309
454
|
const text = action.target || '';
|
|
@@ -321,13 +466,13 @@ export async function executeAction(page, action, timeoutMs, options) {
|
|
|
321
466
|
case 'press': {
|
|
322
467
|
const key = action.value || 'Enter';
|
|
323
468
|
if (action.target) {
|
|
324
|
-
const
|
|
325
|
-
if (
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
await locator.
|
|
469
|
+
const located = await elementFor(page, action, options?.located);
|
|
470
|
+
if ('reason' in located)
|
|
471
|
+
return miss(located.reason);
|
|
472
|
+
const disabled = await disabledOutcome(located.locator, located.action, options?.strictOracles);
|
|
473
|
+
if (disabled)
|
|
474
|
+
return disabled;
|
|
475
|
+
await located.locator.press(key, { timeout: timeoutMs });
|
|
331
476
|
}
|
|
332
477
|
else {
|
|
333
478
|
await page.keyboard.press(key);
|
|
@@ -345,10 +490,11 @@ export async function executeAction(page, action, timeoutMs, options) {
|
|
|
345
490
|
}
|
|
346
491
|
catch (err) {
|
|
347
492
|
const msg = err instanceof Error ? err.message : String(err);
|
|
348
|
-
if (isSoftAutomationMiss(msg)) {
|
|
493
|
+
if (isAutomationFault(msg) || isSoftAutomationMiss(msg)) {
|
|
349
494
|
return {
|
|
350
495
|
status: 'skipped',
|
|
351
|
-
observation:
|
|
496
|
+
observation: `Our test couldn't complete ${action.tool} on ${describeTarget(action)} (${truncate(msg, 180)}). Treated as inconclusive rather than a product bug.`,
|
|
497
|
+
error: msg,
|
|
352
498
|
};
|
|
353
499
|
}
|
|
354
500
|
return {
|
|
@@ -448,7 +594,7 @@ export async function captureLocator(page, action) {
|
|
|
448
594
|
export async function skeletonForLLM(page) {
|
|
449
595
|
const url = page.url();
|
|
450
596
|
const title = await page.title().catch(() => '');
|
|
451
|
-
const collected = await page.evaluate(() => {
|
|
597
|
+
const collected = await page.evaluate(pageFn(() => {
|
|
452
598
|
function txt(el) {
|
|
453
599
|
return (el.textContent || '').trim().replace(/\s+/g, ' ');
|
|
454
600
|
}
|
|
@@ -496,20 +642,59 @@ export async function skeletonForLLM(page) {
|
|
|
496
642
|
}
|
|
497
643
|
return '';
|
|
498
644
|
}
|
|
645
|
+
function nearestContext(el) {
|
|
646
|
+
const chunks = [];
|
|
647
|
+
let node = el.previousElementSibling;
|
|
648
|
+
let hops = 0;
|
|
649
|
+
while (node && hops < 4) {
|
|
650
|
+
const text = txt(node);
|
|
651
|
+
if (text)
|
|
652
|
+
chunks.unshift(text.slice(0, 80));
|
|
653
|
+
node = node.previousElementSibling;
|
|
654
|
+
hops += 1;
|
|
655
|
+
}
|
|
656
|
+
let heading = '';
|
|
657
|
+
let parent = el.parentElement;
|
|
658
|
+
let depth = 0;
|
|
659
|
+
while (parent && depth < 6 && !heading) {
|
|
660
|
+
let sib = parent.previousElementSibling;
|
|
661
|
+
let sibHops = 0;
|
|
662
|
+
while (sib && sibHops < 4 && !heading) {
|
|
663
|
+
if (/^h[1-6]$/i.test(sib.tagName))
|
|
664
|
+
heading = txt(sib).slice(0, 80);
|
|
665
|
+
sib = sib.previousElementSibling;
|
|
666
|
+
sibHops += 1;
|
|
667
|
+
}
|
|
668
|
+
parent = parent.parentElement;
|
|
669
|
+
depth += 1;
|
|
670
|
+
}
|
|
671
|
+
return {
|
|
672
|
+
preceding: chunks.join(' ').replace(/\s+/g, ' ').trim().slice(0, 80),
|
|
673
|
+
heading,
|
|
674
|
+
};
|
|
675
|
+
}
|
|
499
676
|
const inputs = Array.from(document.querySelectorAll('input, textarea, select'))
|
|
500
677
|
.slice(0, 25)
|
|
501
678
|
.map(el => {
|
|
502
679
|
const i = el;
|
|
503
680
|
const label = fieldLabel(i);
|
|
681
|
+
const ctx = nearestContext(i);
|
|
682
|
+
const type = i.type || i.tagName.toLowerCase();
|
|
683
|
+
if (type === 'hidden')
|
|
684
|
+
return undefined;
|
|
504
685
|
return {
|
|
505
686
|
label: label || undefined,
|
|
506
687
|
placeholder: i.placeholder || undefined,
|
|
507
688
|
name: i.name || undefined,
|
|
508
|
-
|
|
689
|
+
id: i.id || undefined,
|
|
690
|
+
type,
|
|
691
|
+
preceding: ctx.preceding || undefined,
|
|
692
|
+
heading: ctx.heading || undefined,
|
|
509
693
|
};
|
|
510
|
-
})
|
|
694
|
+
})
|
|
695
|
+
.filter((item) => Boolean(item));
|
|
511
696
|
return { headings, buttons, links, inputs };
|
|
512
|
-
});
|
|
697
|
+
}));
|
|
513
698
|
return {
|
|
514
699
|
url,
|
|
515
700
|
title,
|
|
@@ -532,6 +717,29 @@ export function describeTarget(action) {
|
|
|
532
717
|
* A missing element throws immediately (`isDisabled` defaults to no wait) and
|
|
533
718
|
* falls through to the real action.
|
|
534
719
|
*/
|
|
720
|
+
/**
|
|
721
|
+
* A disabled control is not clicked or filled. Strict oracles record it as
|
|
722
|
+
* a product bug. Otherwise the step stops immediately instead of waiting
|
|
723
|
+
* for the control to become enabled.
|
|
724
|
+
*/
|
|
725
|
+
async function disabledOutcome(locator, action, strict) {
|
|
726
|
+
let disabled = false;
|
|
727
|
+
try {
|
|
728
|
+
disabled = await locator.isDisabled();
|
|
729
|
+
}
|
|
730
|
+
catch {
|
|
731
|
+
return undefined;
|
|
732
|
+
}
|
|
733
|
+
if (!disabled)
|
|
734
|
+
return undefined;
|
|
735
|
+
if (strict)
|
|
736
|
+
return failIfDisabled(locator, action);
|
|
737
|
+
return {
|
|
738
|
+
status: 'skipped',
|
|
739
|
+
observation: `The only match for ${describeTarget(action)} is disabled, so it was not used.`,
|
|
740
|
+
error: 'locator-unresolved',
|
|
741
|
+
};
|
|
742
|
+
}
|
|
535
743
|
async function failIfDisabled(locator, action) {
|
|
536
744
|
try {
|
|
537
745
|
if (await locator.first().isDisabled()) {
|
|
@@ -547,13 +755,80 @@ async function failIfDisabled(locator, action) {
|
|
|
547
755
|
}
|
|
548
756
|
return undefined;
|
|
549
757
|
}
|
|
758
|
+
async function elementFor(page, action, located) {
|
|
759
|
+
if (located)
|
|
760
|
+
return located;
|
|
761
|
+
return locateActionTarget(page, action);
|
|
762
|
+
}
|
|
763
|
+
function miss(reason) {
|
|
764
|
+
return { status: 'skipped', observation: reason, error: 'locator-unresolved' };
|
|
765
|
+
}
|
|
766
|
+
function done(status, observation, acted) {
|
|
767
|
+
return { status, observation, ...(acted ? { resolved: acted } : {}) };
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* `input[type=number]` accepts digits via `fill`. When the browser rejects
|
|
771
|
+
* that write, click the control and type the same digits.
|
|
772
|
+
*/
|
|
773
|
+
async function fillWithFallback(page, locator, value, timeoutMs, numeric) {
|
|
774
|
+
const fillTimeout = numeric ? Math.min(timeoutMs, 2_000) : timeoutMs;
|
|
775
|
+
try {
|
|
776
|
+
await locator.fill(value, { timeout: fillTimeout });
|
|
777
|
+
if (!numeric)
|
|
778
|
+
return;
|
|
779
|
+
const actual = await locator.inputValue().catch(() => '');
|
|
780
|
+
if (actual === value)
|
|
781
|
+
return;
|
|
782
|
+
throw new Error(`fill did not set number value (got "${actual}")`);
|
|
783
|
+
}
|
|
784
|
+
catch (err) {
|
|
785
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
786
|
+
if (!numeric || !isFillRejection(msg))
|
|
787
|
+
throw err;
|
|
788
|
+
}
|
|
789
|
+
await locator.click({ timeout: Math.min(timeoutMs, 2_000) });
|
|
790
|
+
await locator.press('Control+A').catch(() => { });
|
|
791
|
+
try {
|
|
792
|
+
await locator.pressSequentially(value, { timeout: Math.min(timeoutMs, 3_000) });
|
|
793
|
+
}
|
|
794
|
+
catch {
|
|
795
|
+
await page.keyboard.insertText(value);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
function isFillRejection(msg) {
|
|
799
|
+
const lower = msg.toLowerCase();
|
|
800
|
+
return lower.includes('cannot type text')
|
|
801
|
+
|| lower.includes('not an <input')
|
|
802
|
+
|| lower.includes('not editable')
|
|
803
|
+
|| lower.includes('fill did not set')
|
|
804
|
+
|| (lower.includes('timeout') && lower.includes('locator.fill'));
|
|
805
|
+
}
|
|
550
806
|
function isSoftAutomationMiss(msg) {
|
|
551
807
|
const lower = msg.toLowerCase();
|
|
552
|
-
return lower.includes('timeout') && (lower.includes('waiting for locator')
|
|
808
|
+
return (lower.includes('timeout') && (lower.includes('waiting for locator')
|
|
553
809
|
|| lower.includes('locator.fill')
|
|
554
810
|
|| lower.includes('locator.click')
|
|
555
811
|
|| lower.includes('locator.press')
|
|
556
|
-
|| lower.includes('locator.waitfor'));
|
|
812
|
+
|| lower.includes('locator.waitfor'))) || lower.includes('waiting for getby');
|
|
813
|
+
}
|
|
814
|
+
async function readFillTarget(locator) {
|
|
815
|
+
try {
|
|
816
|
+
if (await locator.count() < 1)
|
|
817
|
+
return undefined;
|
|
818
|
+
return await locator.first().evaluate(el => {
|
|
819
|
+
const input = el;
|
|
820
|
+
const type = (input.type || el.getAttribute('type') || '').toLowerCase();
|
|
821
|
+
return {
|
|
822
|
+
type: type || el.tagName.toLowerCase(),
|
|
823
|
+
label: (el.getAttribute('aria-label') || '').trim() || undefined,
|
|
824
|
+
name: input.name || undefined,
|
|
825
|
+
placeholder: input.placeholder || undefined,
|
|
826
|
+
};
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
catch {
|
|
830
|
+
return undefined;
|
|
831
|
+
}
|
|
557
832
|
}
|
|
558
833
|
function truncate(s, n) {
|
|
559
834
|
return s.length > n ? `${s.slice(0, n - 1)}…` : s;
|