@cutleryapp/agent 1.0.41 → 1.0.43
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/mcp-executor.js +104 -34
- package/package.json +1 -1
package/dist/mcp-executor.js
CHANGED
|
@@ -175,7 +175,14 @@ class TestExecutor {
|
|
|
175
175
|
await page.fill(fieldLabel, value);
|
|
176
176
|
}
|
|
177
177
|
else {
|
|
178
|
-
|
|
178
|
+
// Check if it's a date/birth field and use date-aware fill
|
|
179
|
+
const isDateField = /date|birth|dob/i.test(fieldLabel);
|
|
180
|
+
if (isDateField) {
|
|
181
|
+
await tryFillDate(page, fieldLabel, value);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
await tryFill(page, fieldLabel, value);
|
|
185
|
+
}
|
|
179
186
|
}
|
|
180
187
|
handled = true;
|
|
181
188
|
}
|
|
@@ -441,10 +448,23 @@ class TestExecutor {
|
|
|
441
448
|
}
|
|
442
449
|
}
|
|
443
450
|
catch (err) {
|
|
444
|
-
//
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
451
|
+
// AI last-resort recovery — only when Playwright strategies all failed
|
|
452
|
+
if (process.env.OPENAI_API_KEY) {
|
|
453
|
+
console.log(` 🤖 Playwright failed, trying AI: ${err.message.split('\n')[0]}`);
|
|
454
|
+
try {
|
|
455
|
+
await aiSingleShot(page, raw);
|
|
456
|
+
}
|
|
457
|
+
catch (aiErr) {
|
|
458
|
+
console.log(` ⚠️ AI recovery also failed: ${aiErr.message.split('\n')[0]}`);
|
|
459
|
+
stepError = err.message;
|
|
460
|
+
result.success = false;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
console.log(` ⚠️ Step failed: ${err.message.split('\n')[0]}`);
|
|
465
|
+
stepError = err.message;
|
|
466
|
+
result.success = false;
|
|
467
|
+
}
|
|
448
468
|
}
|
|
449
469
|
// Screenshot on failure, on the last step, or every 5 steps — not every step
|
|
450
470
|
let screenshotB64 = "";
|
|
@@ -1176,43 +1196,93 @@ async function tryClickScoped(page, nameRe, target, scope) {
|
|
|
1176
1196
|
catch { }
|
|
1177
1197
|
return false;
|
|
1178
1198
|
}
|
|
1199
|
+
/** Parse a date string in any common format and return {day, month, year} */
|
|
1200
|
+
function parseDate(value) {
|
|
1201
|
+
// Formats: DD/MM/YYYY, MM/DD/YYYY, YYYY-MM-DD, DD-MM-YYYY, DD Mon YYYY, etc.
|
|
1202
|
+
const patterns = [
|
|
1203
|
+
/^(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})$/, // DD/MM/YYYY or MM/DD/YYYY
|
|
1204
|
+
/^(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})$/, // YYYY-MM-DD
|
|
1205
|
+
];
|
|
1206
|
+
for (const p of patterns) {
|
|
1207
|
+
const m = value.match(p);
|
|
1208
|
+
if (m) {
|
|
1209
|
+
const [, a, b, c] = m;
|
|
1210
|
+
// Heuristic: if first group is 4 digits → YYYY-MM-DD
|
|
1211
|
+
const [year, month, day] = a.length === 4 ? [a, b, c] : [c, b, a];
|
|
1212
|
+
const iso = `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`;
|
|
1213
|
+
return { iso, day: day.padStart(2, '0'), month: month.padStart(2, '0'), year };
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
return null;
|
|
1217
|
+
}
|
|
1218
|
+
/** Fill a date field — handles input[type=date], calendar pickers, and plain text inputs */
|
|
1219
|
+
async function tryFillDate(page, label, value) {
|
|
1220
|
+
const labelRe = new RegExp(escapeRegex(label), 'i');
|
|
1221
|
+
const parsed = parseDate(value);
|
|
1222
|
+
// Strategy 1: input[type="date"] — needs YYYY-MM-DD format
|
|
1223
|
+
if (parsed) {
|
|
1224
|
+
const dateInputSelectors = [
|
|
1225
|
+
`input[type="date"]`,
|
|
1226
|
+
`input[type="datetime-local"]`,
|
|
1227
|
+
];
|
|
1228
|
+
for (const sel of dateInputSelectors) {
|
|
1229
|
+
try {
|
|
1230
|
+
const loc = page.locator(sel).first();
|
|
1231
|
+
if (await loc.isVisible({ timeout: 400 })) {
|
|
1232
|
+
await loc.fill(parsed.iso, { timeout: 1000 });
|
|
1233
|
+
return;
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
catch { /* try next */ }
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
// Strategy 2: plain text input — type the value as-is, dismiss any calendar that opens
|
|
1240
|
+
try {
|
|
1241
|
+
const input = await Promise.any([
|
|
1242
|
+
page.getByLabel(labelRe).first().elementHandle({ timeout: 500 }),
|
|
1243
|
+
page.getByPlaceholder(labelRe).first().elementHandle({ timeout: 500 }),
|
|
1244
|
+
page.getByRole('textbox', { name: labelRe }).first().elementHandle({ timeout: 500 }),
|
|
1245
|
+
]);
|
|
1246
|
+
if (input) {
|
|
1247
|
+
await input.click();
|
|
1248
|
+
await page.keyboard.press('Escape'); // close any calendar that opened
|
|
1249
|
+
await page.waitForTimeout(150);
|
|
1250
|
+
await input.click({ clickCount: 3 }); // select all existing text
|
|
1251
|
+
await page.keyboard.type(value, { delay: 30 });
|
|
1252
|
+
await page.keyboard.press('Escape'); // close calendar again after typing
|
|
1253
|
+
await page.keyboard.press('Tab'); // commit the value
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
catch { /* fall through */ }
|
|
1258
|
+
// Strategy 3: fallback to regular fill
|
|
1259
|
+
await tryFill(page, label, value);
|
|
1260
|
+
}
|
|
1179
1261
|
async function tryFill(page, label, value) {
|
|
1180
1262
|
const labelRe = new RegExp(escapeRegex(label), "i");
|
|
1181
1263
|
const variants = labelVariants(label);
|
|
1182
1264
|
const attrContains = (attr) => variants.map(v => `input[${attr}*="${cssEscape(v)}" i], textarea[${attr}*="${cssEscape(v)}" i], [contenteditable="true"][${attr}*="${cssEscape(v)}" i]`).join(", ");
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
() => page.
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
catch { /* all 3 failed, try phase 2 */ }
|
|
1198
|
-
// Phase 2: attribute-based selectors covering name/id/data-test variants (300ms each)
|
|
1199
|
-
const exactAttrs = variants.flatMap(v => [
|
|
1200
|
-
`input[name="${cssEscape(v)}" i]`, `input[id="${cssEscape(v)}" i]`,
|
|
1201
|
-
`textarea[name="${cssEscape(v)}" i]`, `textarea[id="${cssEscape(v)}" i]`,
|
|
1202
|
-
]).join(", ");
|
|
1203
|
-
const phase2 = [
|
|
1204
|
-
() => page.locator(exactAttrs).first().fill(value),
|
|
1205
|
-
() => page.locator(`${attrContains("name")}, ${attrContains("id")}`).first().fill(value),
|
|
1206
|
-
() => page.locator(attrContains("data-test")).first().fill(value),
|
|
1207
|
-
() => page.locator(attrContains("aria-label")).first().fill(value),
|
|
1208
|
-
() => page.locator(attrContains("placeholder")).first().fill(value),
|
|
1265
|
+
// Sequential strategies — do NOT run concurrently (concurrent fills can cross-contaminate fields)
|
|
1266
|
+
// Each uses a short timeout since if the element exists, Playwright resolves near-instantly.
|
|
1267
|
+
const strategies = [
|
|
1268
|
+
['getByLabel', () => page.getByLabel(labelRe).first().fill(value)],
|
|
1269
|
+
['getByPlaceholder', () => page.getByPlaceholder(labelRe).first().fill(value)],
|
|
1270
|
+
['getByRole', () => page.getByRole("textbox", { name: labelRe }).first().fill(value)],
|
|
1271
|
+
['id/name exact', () => page.locator(variants.flatMap(v => [
|
|
1272
|
+
`input[name="${cssEscape(v)}" i]`, `input[id="${cssEscape(v)}" i]`,
|
|
1273
|
+
`textarea[name="${cssEscape(v)}" i]`, `textarea[id="${cssEscape(v)}" i]`,
|
|
1274
|
+
]).join(", ")).first().fill(value)],
|
|
1275
|
+
['id/name contains', () => page.locator(`${attrContains("name")}, ${attrContains("id")}`).first().fill(value)],
|
|
1276
|
+
['data-test', () => page.locator(attrContains("data-test")).first().fill(value)],
|
|
1277
|
+
['aria-label', () => page.locator(attrContains("aria-label")).first().fill(value)],
|
|
1278
|
+
['placeholder attr', () => page.locator(attrContains("placeholder")).first().fill(value)],
|
|
1209
1279
|
];
|
|
1210
|
-
for (const fn of
|
|
1280
|
+
for (const [, fn] of strategies) {
|
|
1211
1281
|
try {
|
|
1212
|
-
await
|
|
1282
|
+
await Promise.race([fn(), new Promise((_, r) => setTimeout(() => r(new Error('t')), 400))]);
|
|
1213
1283
|
return;
|
|
1214
1284
|
}
|
|
1215
|
-
catch { /* next */ }
|
|
1285
|
+
catch { /* next strategy */ }
|
|
1216
1286
|
}
|
|
1217
1287
|
throw new Error(`Could not find input field: "${label}"`);
|
|
1218
1288
|
}
|