agentpath 0.0.0-stage → 0.1.0-alpha.2
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/LICENSE.md +105 -0
- package/README.md +101 -2
- package/THIRD_PARTY_NOTICES +12 -0
- package/apps/demo-site/dist/site.js +92 -0
- package/apps/studio/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/apps/studio/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/apps/studio/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/apps/studio/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/apps/studio/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/apps/studio/dist/assets/index-DH4wJPHm.js +67 -0
- package/apps/studio/dist/assets/index-DrnNh2ip.css +1 -0
- package/apps/studio/dist/index.html +14 -0
- package/audit.md +173 -0
- package/autonomous-mode.md +104 -0
- package/ci.md +118 -0
- package/coding-agents.md +143 -0
- package/concepts.md +145 -0
- package/contracts.md +356 -0
- package/examples/tasks/ambiguous.json +98 -0
- package/examples/tasks/assertion-failure.json +98 -0
- package/examples/tasks/broken.json +98 -0
- package/examples/tasks/fixed.json +98 -0
- package/examples/tasks/recovery.json +19 -0
- package/images/compare-recovery-desktop.jpg +0 -0
- package/images/compare-variants-desktop.jpg +0 -0
- package/images/overview-desktop.jpg +0 -0
- package/images/run-autonomous-passed-desktop.jpg +0 -0
- package/images/run-failed-desktop.jpg +0 -0
- package/images/run-failed-mobile.jpg +0 -0
- package/images/run-missing-artifact-desktop.jpg +0 -0
- package/images/task-editor-laptop.jpg +0 -0
- package/installation.md +102 -0
- package/ledger.md +62 -0
- package/licenses/-fontsource-variable-geist-mono.txt +93 -0
- package/licenses/-fontsource-variable-geist.txt +93 -0
- package/licenses/lucide-react.txt +43 -0
- package/licenses/react-dom.txt +21 -0
- package/licenses/react.txt +21 -0
- package/licenses/scheduler.txt +21 -0
- package/licenses/tailwindcss.txt +21 -0
- package/licenses/zod.txt +21 -0
- package/package.json +165 -4
- package/packages/cli/bin/agentpath.js +39 -0
- package/packages/cli/dist/audit-rerun.js +384 -0
- package/packages/cli/dist/audit.js +387 -0
- package/packages/cli/dist/change-summaries.js +87 -0
- package/packages/cli/dist/ci-report.js +88 -0
- package/packages/cli/dist/demo.js +88 -0
- package/packages/cli/dist/doctor.js +265 -0
- package/packages/cli/dist/explain.js +89 -0
- package/packages/cli/dist/git.js +7 -0
- package/packages/cli/dist/inspect.js +118 -0
- package/packages/cli/dist/ledger.js +509 -0
- package/packages/cli/dist/local.js +102 -0
- package/packages/cli/dist/main.js +708 -0
- package/packages/cli/dist/mcp.js +231 -0
- package/packages/cli/dist/paths.js +6 -0
- package/packages/cli/dist/playwright-fixture.js +185 -0
- package/packages/cli/dist/playwright-reporter.js +49 -0
- package/packages/cli/dist/tasks.js +37 -0
- package/packages/cli/dist/test-source.js +283 -0
- package/packages/cli/dist/watch.js +96 -0
- package/packages/contracts/dist/agent-settings.js +18 -0
- package/packages/contracts/dist/agent.js +175 -0
- package/packages/contracts/dist/audit.js +115 -0
- package/packages/contracts/dist/guidance.js +339 -0
- package/packages/contracts/dist/index.js +14 -0
- package/packages/contracts/dist/inspect.js +63 -0
- package/packages/contracts/dist/ledger.js +80 -0
- package/packages/contracts/dist/local.js +171 -0
- package/packages/contracts/dist/outcomes.js +94 -0
- package/packages/contracts/dist/platform.js +105 -0
- package/packages/contracts/dist/playwright-live.js +76 -0
- package/packages/contracts/dist/policy.js +93 -0
- package/packages/contracts/dist/run.js +178 -0
- package/packages/contracts/dist/task-issues.js +39 -0
- package/packages/contracts/dist/task.js +185 -0
- package/packages/contracts/dist/watch.js +57 -0
- package/packages/core/dist/action-feedback.js +103 -0
- package/packages/core/dist/actions.js +160 -0
- package/packages/core/dist/agent-loop.js +122 -0
- package/packages/core/dist/artifacts.js +156 -0
- package/packages/core/dist/assertions.js +120 -0
- package/packages/core/dist/autonomous.js +1 -0
- package/packages/core/dist/browser.js +196 -0
- package/packages/core/dist/compare.js +141 -0
- package/packages/core/dist/control-state.js +62 -0
- package/packages/core/dist/diagnostics.js +134 -0
- package/packages/core/dist/evidence-diff.js +93 -0
- package/packages/core/dist/evidence.js +203 -0
- package/packages/core/dist/index.js +22 -0
- package/packages/core/dist/inspect.js +257 -0
- package/packages/core/dist/integrity.js +176 -0
- package/packages/core/dist/liveness.js +114 -0
- package/packages/core/dist/local-files.js +61 -0
- package/packages/core/dist/locators.js +214 -0
- package/packages/core/dist/model-provider.js +88 -0
- package/packages/core/dist/observation.js +257 -0
- package/packages/core/dist/playwright-live.js +519 -0
- package/packages/core/dist/playwright-results.js +543 -0
- package/packages/core/dist/policy.js +182 -0
- package/packages/core/dist/privacy.js +93 -0
- package/packages/core/dist/recovery.js +54 -0
- package/packages/core/dist/retention.js +204 -0
- package/packages/core/dist/run-index.js +177 -0
- package/packages/core/dist/run-plan.js +28 -0
- package/packages/core/dist/run-repository.js +518 -0
- package/packages/core/dist/runner.js +913 -0
- package/packages/core/dist/storage.js +63 -0
- package/packages/core/dist/task-fingerprint.js +36 -0
- package/packages/core/dist/test-change-rerun.js +84 -0
- package/packages/core/dist/test-changes.js +896 -0
- package/packages/core/dist/timing.js +41 -0
- package/packages/core/dist/watch.js +165 -0
- package/packages/server/dist/index.js +265 -0
- package/packages/server/dist/task-store.js +103 -0
- package/quickstart.md +85 -0
- package/security.md +136 -0
- package/try-on-your-repo.md +54 -0
- package/understanding-results.md +159 -0
- package/writing-tasks.md +196 -0
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
import { FAILED_REQUEST_LIMIT, FAILED_REQUEST_PATH_LIMIT, LIVE_LABEL_LIMIT, LIVE_STEP_LIMIT, NEAR_MATCH_LIMIT, PLAYWRIGHT_LIVE_VERSION, semanticRoleSchema } from "#agentpath/contracts";
|
|
2
|
+
import { boundedText, lateWindow, visibleTextMatches } from "./evidence.js";
|
|
3
|
+
import { findNearMatches, NAME_LIMIT, similarValues, snapshotTargets } from "./locators.js";
|
|
4
|
+
import { Deadline } from "./timing.js";
|
|
5
|
+
const roles = new Set(semanticRoleSchema.options);
|
|
6
|
+
const TEXT_METHODS = new Set(["getByText", "getByLabel", "getByPlaceholder", "getByAltText", "getByTitle"]);
|
|
7
|
+
const ROLE_OPTIONS = new Set(["name", "exact", "checked", "disabled", "expanded", "includeHidden", "level", "pressed", "selected"]);
|
|
8
|
+
class ParseError extends Error {
|
|
9
|
+
}
|
|
10
|
+
/** Parses the locator chain Playwright prints in its errors, such as `locator('#main').getByRole('button', { name: 'Save' }).first()`. */
|
|
11
|
+
export function parseLocatorChain(text) {
|
|
12
|
+
if (text.length > 2_000)
|
|
13
|
+
return { reason: "The locator is longer than AgentPath reads." };
|
|
14
|
+
let position = 0;
|
|
15
|
+
const peek = () => text[position];
|
|
16
|
+
const space = () => { while (position < text.length && /\s/.test(text[position]))
|
|
17
|
+
position++; };
|
|
18
|
+
const expect = (character) => {
|
|
19
|
+
space();
|
|
20
|
+
if (text[position] !== character)
|
|
21
|
+
throw new ParseError(`expected ${character}`);
|
|
22
|
+
position++;
|
|
23
|
+
};
|
|
24
|
+
const string = () => {
|
|
25
|
+
const quote = text[position];
|
|
26
|
+
position++;
|
|
27
|
+
let value = "";
|
|
28
|
+
while (position < text.length && text[position] !== quote) {
|
|
29
|
+
let character = text[position++];
|
|
30
|
+
if (character === "\\") {
|
|
31
|
+
const next = text[position++];
|
|
32
|
+
if (next === undefined)
|
|
33
|
+
throw new ParseError("unterminated string");
|
|
34
|
+
if (next === "u" && /^[0-9a-fA-F]{4}$/.test(text.slice(position, position + 4))) {
|
|
35
|
+
character = String.fromCharCode(parseInt(text.slice(position, position + 4), 16));
|
|
36
|
+
position += 4;
|
|
37
|
+
}
|
|
38
|
+
else
|
|
39
|
+
character = next === "n" ? "\n" : next === "t" ? "\t" : next === "r" ? "\r" : next;
|
|
40
|
+
}
|
|
41
|
+
value += character;
|
|
42
|
+
}
|
|
43
|
+
if (text[position] !== quote)
|
|
44
|
+
throw new ParseError("unterminated string");
|
|
45
|
+
position++;
|
|
46
|
+
return value;
|
|
47
|
+
};
|
|
48
|
+
const regex = () => {
|
|
49
|
+
position++;
|
|
50
|
+
let body = "";
|
|
51
|
+
let inClass = false;
|
|
52
|
+
while (position < text.length) {
|
|
53
|
+
const character = text[position];
|
|
54
|
+
if (character === "\\") {
|
|
55
|
+
body += character + (text[position + 1] ?? "");
|
|
56
|
+
position += 2;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (character === "[")
|
|
60
|
+
inClass = true;
|
|
61
|
+
else if (character === "]")
|
|
62
|
+
inClass = false;
|
|
63
|
+
else if (character === "/" && !inClass)
|
|
64
|
+
break;
|
|
65
|
+
body += character;
|
|
66
|
+
position++;
|
|
67
|
+
}
|
|
68
|
+
if (text[position] !== "/")
|
|
69
|
+
throw new ParseError("unterminated regular expression");
|
|
70
|
+
position++;
|
|
71
|
+
const flags = /^[dgimsuyv]*/.exec(text.slice(position))[0];
|
|
72
|
+
position += flags.length;
|
|
73
|
+
try {
|
|
74
|
+
return new RegExp(body, flags);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
throw new ParseError("invalid regular expression");
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const value = () => {
|
|
81
|
+
space();
|
|
82
|
+
const character = peek();
|
|
83
|
+
if (character === "'" || character === "\"")
|
|
84
|
+
return string();
|
|
85
|
+
if (character === "/")
|
|
86
|
+
return regex();
|
|
87
|
+
if (character === "{")
|
|
88
|
+
return object();
|
|
89
|
+
const word = /^(?:-?\d+(?:\.\d+)?|true|false)/.exec(text.slice(position))?.[0];
|
|
90
|
+
if (!word)
|
|
91
|
+
throw new ParseError("unsupported argument");
|
|
92
|
+
position += word.length;
|
|
93
|
+
return word === "true" ? true : word === "false" ? false : Number(word);
|
|
94
|
+
};
|
|
95
|
+
const object = () => {
|
|
96
|
+
expect("{");
|
|
97
|
+
const result = {};
|
|
98
|
+
space();
|
|
99
|
+
if (peek() === "}") {
|
|
100
|
+
position++;
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
for (;;) {
|
|
104
|
+
space();
|
|
105
|
+
const key = /^[A-Za-z]+/.exec(text.slice(position))?.[0];
|
|
106
|
+
if (!key)
|
|
107
|
+
throw new ParseError("unsupported option");
|
|
108
|
+
position += key.length;
|
|
109
|
+
expect(":");
|
|
110
|
+
result[key] = value();
|
|
111
|
+
space();
|
|
112
|
+
if (peek() === ",") {
|
|
113
|
+
position++;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
expect("}");
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const steps = [];
|
|
121
|
+
try {
|
|
122
|
+
space();
|
|
123
|
+
if (text.startsWith("page.", position))
|
|
124
|
+
position += 5;
|
|
125
|
+
for (;;) {
|
|
126
|
+
space();
|
|
127
|
+
const start = position;
|
|
128
|
+
const method = /^[A-Za-z]+/.exec(text.slice(position))?.[0];
|
|
129
|
+
if (!method)
|
|
130
|
+
throw new ParseError("expected a locator method");
|
|
131
|
+
position += method.length;
|
|
132
|
+
expect("(");
|
|
133
|
+
const args = [];
|
|
134
|
+
space();
|
|
135
|
+
if (peek() !== ")") {
|
|
136
|
+
for (;;) {
|
|
137
|
+
args.push(value());
|
|
138
|
+
space();
|
|
139
|
+
if (peek() === ",") {
|
|
140
|
+
position++;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
expect(")");
|
|
147
|
+
steps.push({ method, args, code: text.slice(start, position) });
|
|
148
|
+
if (steps.length > LIVE_STEP_LIMIT)
|
|
149
|
+
return { reason: "The locator chain has more steps than AgentPath reads." };
|
|
150
|
+
space();
|
|
151
|
+
if (position >= text.length)
|
|
152
|
+
break;
|
|
153
|
+
expect(".");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
// A nested locator (`filter({ has: … })`, `and(…)`, `or(…)`) arrives here as an unsupported argument.
|
|
158
|
+
return { reason: `The locator could not be read (${error instanceof ParseError ? error.message : "unexpected text"}).` };
|
|
159
|
+
}
|
|
160
|
+
for (const step of steps) {
|
|
161
|
+
const problem = checkStep(step);
|
|
162
|
+
if (problem)
|
|
163
|
+
return { reason: problem };
|
|
164
|
+
}
|
|
165
|
+
return { steps };
|
|
166
|
+
}
|
|
167
|
+
const isText = (value) => typeof value === "string" || value instanceof RegExp;
|
|
168
|
+
const isOptions = (value) => typeof value === "object" && value !== null && !(value instanceof RegExp);
|
|
169
|
+
function onlyKeys(options, allowed) {
|
|
170
|
+
if (options === undefined)
|
|
171
|
+
return true;
|
|
172
|
+
return isOptions(options) && Object.entries(options).every(([key, value]) => allowed.has(key) && !isOptions(value));
|
|
173
|
+
}
|
|
174
|
+
/** Checks one step against the methods and arguments AgentPath rebuilds; returns why not, if it is unsupported. */
|
|
175
|
+
function checkStep({ method, args }) {
|
|
176
|
+
const [first, second] = args;
|
|
177
|
+
const ok = (() => {
|
|
178
|
+
if (method === "locator")
|
|
179
|
+
return typeof first === "string" && args.length <= 2 && onlyKeys(second, new Set(["hasText", "hasNotText"]));
|
|
180
|
+
if (method === "getByRole")
|
|
181
|
+
return typeof first === "string" && args.length <= 2 && onlyKeys(second, ROLE_OPTIONS);
|
|
182
|
+
if (TEXT_METHODS.has(method))
|
|
183
|
+
return isText(first) && args.length <= 2 && onlyKeys(second, new Set(["exact"]));
|
|
184
|
+
if (method === "getByTestId")
|
|
185
|
+
return isText(first) && args.length === 1;
|
|
186
|
+
if (method === "first" || method === "last" || method === "contentFrame")
|
|
187
|
+
return args.length === 0;
|
|
188
|
+
if (method === "nth")
|
|
189
|
+
return args.length === 1 && typeof first === "number" && Number.isInteger(first);
|
|
190
|
+
if (method === "filter")
|
|
191
|
+
return args.length === 1 && onlyKeys(first, new Set(["hasText", "hasNotText", "visible"]));
|
|
192
|
+
return false;
|
|
193
|
+
})();
|
|
194
|
+
return ok ? undefined : `The locator step ${method}(…) is not one AgentPath rebuilds.`;
|
|
195
|
+
}
|
|
196
|
+
const isFrame = (scope) => typeof scope.owner === "function" && typeof scope.count !== "function";
|
|
197
|
+
function applyStep(scope, { method, args }) {
|
|
198
|
+
const target = scope;
|
|
199
|
+
// The method name was checked against a fixed list in checkStep; arguments are parsed literals only.
|
|
200
|
+
if (method === "contentFrame")
|
|
201
|
+
return scope.contentFrame();
|
|
202
|
+
if ((method === "first" || method === "last" || method === "nth" || method === "filter") && scope.goto) {
|
|
203
|
+
throw new Error("positional step on a page");
|
|
204
|
+
}
|
|
205
|
+
return target[method].apply(scope, args);
|
|
206
|
+
}
|
|
207
|
+
async function countOf(scope, deadline) {
|
|
208
|
+
if (isFrame(scope))
|
|
209
|
+
return deadline.observe(() => scope.owner().count());
|
|
210
|
+
if (scope.goto)
|
|
211
|
+
return undefined;
|
|
212
|
+
return deadline.observe(() => scope.count());
|
|
213
|
+
}
|
|
214
|
+
const VALUE_METHODS = ["getByPlaceholder", "getByTestId", "getByTitle", "getByAltText", "getByText"];
|
|
215
|
+
/** A step that names a value with a string: the value-based near-match search applies to it. */
|
|
216
|
+
export function valueTarget(step) {
|
|
217
|
+
const [first, second] = step.args;
|
|
218
|
+
if (!VALUE_METHODS.includes(step.method) || typeof first !== "string" || !first.trim())
|
|
219
|
+
return undefined;
|
|
220
|
+
return { method: step.method, value: first.slice(0, NAME_LIMIT), exact: isOptions(second) && second.exact === true };
|
|
221
|
+
}
|
|
222
|
+
/** Values of the given kind on the elements inside a scope. A fixed, read-only inspection; bounded. */
|
|
223
|
+
async function valuesIn(page, scope, method, testIdAttribute, deadline) {
|
|
224
|
+
const collect = (roots, options) => {
|
|
225
|
+
const normalize = (value) => value.replace(/\s+/g, " ").trim();
|
|
226
|
+
const found = [];
|
|
227
|
+
for (const root of roots.slice(0, 10)) {
|
|
228
|
+
const elements = [root, ...Array.from(root.querySelectorAll("*")).slice(0, 5_000)];
|
|
229
|
+
for (const element of elements) {
|
|
230
|
+
if (["SCRIPT", "STYLE", "NOSCRIPT", "TEMPLATE"].includes(element.tagName))
|
|
231
|
+
continue;
|
|
232
|
+
const visible = element.checkVisibility;
|
|
233
|
+
if (visible && !visible.call(element))
|
|
234
|
+
continue;
|
|
235
|
+
let value = null;
|
|
236
|
+
if (options.method === "getByPlaceholder")
|
|
237
|
+
value = element.getAttribute("placeholder");
|
|
238
|
+
else if (options.method === "getByTestId")
|
|
239
|
+
value = element.getAttribute(options.attribute);
|
|
240
|
+
else if (options.method === "getByTitle")
|
|
241
|
+
value = element.getAttribute("title");
|
|
242
|
+
else if (options.method === "getByAltText")
|
|
243
|
+
value = ["IMG", "AREA", "INPUT"].includes(element.tagName) ? element.getAttribute("alt") : null;
|
|
244
|
+
else if (Array.from(element.childNodes).some((node) => node.nodeType === Node.TEXT_NODE && normalize(node.textContent ?? ""))) {
|
|
245
|
+
const text = normalize(element.textContent ?? "");
|
|
246
|
+
value = text.length <= 80 ? text : null;
|
|
247
|
+
}
|
|
248
|
+
if (value && normalize(value) && !found.includes(normalize(value)))
|
|
249
|
+
found.push(normalize(value));
|
|
250
|
+
if (found.length >= 500)
|
|
251
|
+
return found;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return found;
|
|
255
|
+
};
|
|
256
|
+
const options = { method, attribute: testIdAttribute };
|
|
257
|
+
try {
|
|
258
|
+
let values;
|
|
259
|
+
if (scope.goto)
|
|
260
|
+
values = await deadline.observe(() => page.locator("body").evaluateAll(collect, options));
|
|
261
|
+
else if (isFrame(scope))
|
|
262
|
+
values = await deadline.observe(() => scope.locator("body").evaluateAll(collect, options));
|
|
263
|
+
else
|
|
264
|
+
values = await deadline.observe(() => scope.evaluateAll(collect, options));
|
|
265
|
+
return values.map((value) => boundedText(value, NAME_LIMIT).text).filter(Boolean);
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return [];
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/** The comparable AgentPath target of a step: getByRole with a string name, or getByLabel with a string. */
|
|
272
|
+
export function comparableTarget(step) {
|
|
273
|
+
const [first, second] = step.args;
|
|
274
|
+
const exact = isOptions(second) && second.exact === true;
|
|
275
|
+
if (step.method === "getByRole" && typeof first === "string" && roles.has(first) && isOptions(second) && typeof second.name === "string" && second.name) {
|
|
276
|
+
const extra = Object.keys(second).filter((key) => key !== "name" && key !== "exact");
|
|
277
|
+
if (extra.length)
|
|
278
|
+
return undefined;
|
|
279
|
+
return { by: "role", role: first, name: second.name.slice(0, NAME_LIMIT), exact };
|
|
280
|
+
}
|
|
281
|
+
if (step.method === "getByLabel" && typeof first === "string" && first)
|
|
282
|
+
return { by: "label", label: first.slice(0, NAME_LIMIT), exact };
|
|
283
|
+
return undefined;
|
|
284
|
+
}
|
|
285
|
+
const stripAnsi = (value) => value.replace(/\u001b\[[0-9;]*m/g, "");
|
|
286
|
+
/** The locator line and failed action from a Playwright error message. */
|
|
287
|
+
export function failedLocatorLine(message) {
|
|
288
|
+
const text = stripAnsi(message);
|
|
289
|
+
const line = /(?:waiting for |Locator: )(\S.*)$/m.exec(text)?.[1]?.trim();
|
|
290
|
+
const action = /\blocator\.([A-Za-z]+):/.exec(text)?.[1] ?? (/expect\(/.test(text) ? "expect" : undefined);
|
|
291
|
+
const strict = /strict mode violation/i.test(text);
|
|
292
|
+
// A missing target: an expect that found no element, or an action that timed out before the locator resolved.
|
|
293
|
+
const missing = !strict && (/element\(s\) not found/i.test(text) ||
|
|
294
|
+
(/Timeout \d+ms exceeded/.test(text) && /\blocator\.[A-Za-z]+:/.test(text) && !/locator resolved to|resolved to \d+ element/.test(text)));
|
|
295
|
+
const timeout = /Timeout:? (\d+)ms/.exec(text)?.[1];
|
|
296
|
+
return { ...(line ? { line } : {}), ...(action ? { action } : {}), missing, strict, ...(timeout ? { timeoutMs: Number(timeout) } : {}) };
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Records failed requests to the hosts of the page's own document loads while the test runs, as the trace
|
|
300
|
+
* reader does: HTTP 400 and above, or a network failure that is not an abort. Paths exclude the query.
|
|
301
|
+
*/
|
|
302
|
+
export function watchFailedRequests(page) {
|
|
303
|
+
const documentHosts = new Set();
|
|
304
|
+
const seen = [];
|
|
305
|
+
const parse = (value) => { try {
|
|
306
|
+
return new URL(value);
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
return undefined;
|
|
310
|
+
} };
|
|
311
|
+
const add = (request, status) => {
|
|
312
|
+
const url = parse(request.url());
|
|
313
|
+
if (url && seen.length < 500)
|
|
314
|
+
seen.push({ url, method: request.method(), resourceType: request.resourceType(), ...(status ? { status } : {}) });
|
|
315
|
+
};
|
|
316
|
+
page.on("response", (response) => {
|
|
317
|
+
const request = response.request();
|
|
318
|
+
if (request.resourceType() === "document") {
|
|
319
|
+
const url = parse(request.url());
|
|
320
|
+
if (url)
|
|
321
|
+
documentHosts.add(url.host);
|
|
322
|
+
}
|
|
323
|
+
if (response.status() >= 400)
|
|
324
|
+
add(request, response.status());
|
|
325
|
+
});
|
|
326
|
+
page.on("requestfailed", (request) => {
|
|
327
|
+
const reason = request.failure()?.errorText ?? "";
|
|
328
|
+
if (!reason || /ERR_ABORTED|ERR_BLOCKED_BY_CLIENT/.test(reason))
|
|
329
|
+
return;
|
|
330
|
+
add(request);
|
|
331
|
+
});
|
|
332
|
+
return () => {
|
|
333
|
+
const failed = new Map();
|
|
334
|
+
for (const entry of seen) {
|
|
335
|
+
if (!documentHosts.has(entry.url.host))
|
|
336
|
+
continue;
|
|
337
|
+
const method = entry.method.slice(0, 16);
|
|
338
|
+
const path = entry.url.pathname.slice(0, FAILED_REQUEST_PATH_LIMIT) || "/";
|
|
339
|
+
const key = `${method} ${entry.url.host}${path} ${entry.status ?? "network"}`;
|
|
340
|
+
const known = failed.get(key);
|
|
341
|
+
if (known)
|
|
342
|
+
known.count++;
|
|
343
|
+
else
|
|
344
|
+
failed.set(key, { host: entry.url.host, path, method, resourceType: entry.resourceType.slice(0, 32),
|
|
345
|
+
...(entry.status ? { status: Math.min(entry.status, 599) } : { failure: "network" }), count: 1 });
|
|
346
|
+
}
|
|
347
|
+
return [...failed.values()].slice(0, FAILED_REQUEST_LIMIT);
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
/** Own texts of <label> elements that label no control. A fixed, read-only inspection. */
|
|
351
|
+
async function detachedLabels(page, deadline) {
|
|
352
|
+
try {
|
|
353
|
+
const texts = await deadline.observe(() => page.evaluate((limit) => {
|
|
354
|
+
const normalize = (value) => value.replace(/\s+/g, " ").trim();
|
|
355
|
+
const found = [];
|
|
356
|
+
for (const element of Array.from(document.querySelectorAll("label"))) {
|
|
357
|
+
if (element.control !== null)
|
|
358
|
+
continue;
|
|
359
|
+
const own = normalize(Array.from(element.childNodes).filter((node) => node.nodeType === Node.TEXT_NODE).map((node) => node.textContent ?? "").join(" "));
|
|
360
|
+
if (own && !found.includes(own))
|
|
361
|
+
found.push(own);
|
|
362
|
+
if (found.length >= limit)
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
return found;
|
|
366
|
+
}, LIVE_LABEL_LIMIT));
|
|
367
|
+
return texts.map((text) => boundedText(text, NAME_LIMIT).text).filter(Boolean);
|
|
368
|
+
}
|
|
369
|
+
catch {
|
|
370
|
+
return [];
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
async function nearMatchesIn(page, scope, target, deadline) {
|
|
374
|
+
const snapshots = [];
|
|
375
|
+
try {
|
|
376
|
+
if (scope.goto)
|
|
377
|
+
snapshots.push(await deadline.observe(() => page.locator("body").ariaSnapshot({ timeout: deadline.remaining() })));
|
|
378
|
+
else if (isFrame(scope))
|
|
379
|
+
snapshots.push(await deadline.observe(() => scope.locator("body").ariaSnapshot({ timeout: deadline.remaining() })));
|
|
380
|
+
else {
|
|
381
|
+
const locator = scope;
|
|
382
|
+
const count = Math.min(await deadline.observe(() => locator.count()), 10);
|
|
383
|
+
for (let index = 0; index < count; index++) {
|
|
384
|
+
snapshots.push(await deadline.observe(() => locator.nth(index).ariaSnapshot({ timeout: deadline.remaining() })));
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
// Near matches are optional context; a slow or changing page records what was read.
|
|
390
|
+
}
|
|
391
|
+
const inspection = target.by === "role" ? { by: "role", role: target.role, name: target.name } : { by: "label", label: target.label };
|
|
392
|
+
let matches = findNearMatches(inspection, snapshots.flatMap((snapshot) => snapshotTargets(snapshot)));
|
|
393
|
+
if (target.by === "role" && scope.goto && matches.length < NEAR_MATCH_LIMIT) {
|
|
394
|
+
const shown = await visibleTextMatches(page, target.role, target.name, deadline);
|
|
395
|
+
matches = [...shown.map((entry) => ({ ...entry, relation: "visible-text-matches" })),
|
|
396
|
+
...matches.filter((match) => !shown.some((entry) => entry.role === match.role && entry.name === match.name))].slice(0, NEAR_MATCH_LIMIT);
|
|
397
|
+
}
|
|
398
|
+
return matches;
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Observes a failed test's page. `errors` are the messages of Playwright's test errors; `failedRequests` comes
|
|
402
|
+
* from `watchFailedRequests`. Bounded by `budgetMs` overall; it never throws.
|
|
403
|
+
*/
|
|
404
|
+
export async function observeLiveFailure(page, status, errors, failedRequests, options = {}) {
|
|
405
|
+
const budgetMs = options.budgetMs ?? 8_000;
|
|
406
|
+
const signal = new AbortController().signal;
|
|
407
|
+
const overall = new Deadline(budgetMs, signal);
|
|
408
|
+
const quick = () => new Deadline(Math.min(1_500, Math.max(1, overall.remaining() - 100)), signal);
|
|
409
|
+
const finding = { version: PLAYWRIGHT_LIVE_VERSION, observedAt: options.observedAt ?? "page-teardown", status };
|
|
410
|
+
try {
|
|
411
|
+
const requests = failedRequests();
|
|
412
|
+
if (requests.length)
|
|
413
|
+
finding.failedRequests = requests;
|
|
414
|
+
}
|
|
415
|
+
catch { /* none recorded */ }
|
|
416
|
+
if (options.otherPages) {
|
|
417
|
+
// The error does not say which page the locator ran on; comparing this one could name a look-alike from it.
|
|
418
|
+
finding.otherPages = true;
|
|
419
|
+
finding.reason = "The test used more than one page, and the error does not say which one the locator ran on, so no page is compared.";
|
|
420
|
+
finding.lateCheck = { outcome: "not-run", reason: "The failed locator may belong to another page." };
|
|
421
|
+
return finding;
|
|
422
|
+
}
|
|
423
|
+
try {
|
|
424
|
+
const located = errors.map(failedLocatorLine).find((entry) => entry.line);
|
|
425
|
+
if (located?.action)
|
|
426
|
+
finding.action = located.action.slice(0, 32);
|
|
427
|
+
if (!located?.line) {
|
|
428
|
+
finding.reason = status === "timedOut"
|
|
429
|
+
? "The test timed out; Playwright reports the pending locator only after the page is closed."
|
|
430
|
+
: "The error names no locator.";
|
|
431
|
+
finding.lateCheck = { outcome: "not-run", reason: "No locator was known while the page was open." };
|
|
432
|
+
const labels = await detachedLabels(page, quick());
|
|
433
|
+
if (labels.length)
|
|
434
|
+
finding.detachedLabels = labels;
|
|
435
|
+
return finding;
|
|
436
|
+
}
|
|
437
|
+
const parsed = parseLocatorChain(located.line);
|
|
438
|
+
if ("reason" in parsed) {
|
|
439
|
+
finding.reason = parsed.reason.slice(0, 300);
|
|
440
|
+
finding.lateCheck = { outcome: "not-run", reason: "The locator could not be rebuilt." };
|
|
441
|
+
return finding;
|
|
442
|
+
}
|
|
443
|
+
const steps = [];
|
|
444
|
+
const scopes = [page];
|
|
445
|
+
let stoppedAt;
|
|
446
|
+
for (const [index, step] of parsed.steps.entries()) {
|
|
447
|
+
const scope = applyStep(scopes.at(-1), step);
|
|
448
|
+
scopes.push(scope);
|
|
449
|
+
let count;
|
|
450
|
+
try {
|
|
451
|
+
count = await countOf(scope, quick());
|
|
452
|
+
}
|
|
453
|
+
catch { /* unknown */ }
|
|
454
|
+
steps.push({ code: step.code.slice(0, 1_000), ...(count === undefined ? {} : { count: Math.min(count, 100_000) }) });
|
|
455
|
+
if (count === 0 && stoppedAt === undefined)
|
|
456
|
+
stoppedAt = index;
|
|
457
|
+
}
|
|
458
|
+
finding.chain = { text: located.line.slice(0, 2_000), steps, ...(stoppedAt === undefined ? {} : { stoppedAt }) };
|
|
459
|
+
if (stoppedAt !== undefined) {
|
|
460
|
+
const target = comparableTarget(parsed.steps[stoppedAt]);
|
|
461
|
+
if (target) {
|
|
462
|
+
finding.target = target;
|
|
463
|
+
const matches = await nearMatchesIn(page, scopes[stoppedAt], target, new Deadline(Math.min(2_500, Math.max(1, overall.remaining() - 100)), signal));
|
|
464
|
+
if (matches.length)
|
|
465
|
+
finding.nearMatches = matches;
|
|
466
|
+
}
|
|
467
|
+
const value = target ? undefined : valueTarget(parsed.steps[stoppedAt]);
|
|
468
|
+
if (value)
|
|
469
|
+
finding.valueTarget = value;
|
|
470
|
+
// Short texts on a whole page are too many, and of any kind, to compare without a role: a page-wide
|
|
471
|
+
// getByText is stated as facts only. Inside a resolved scope the candidates are that scope's texts.
|
|
472
|
+
if (value && !(value.method === "getByText" && stoppedAt === 0)) {
|
|
473
|
+
const values = await valuesIn(page, scopes[stoppedAt], value.method, options.testIdAttribute ?? "data-testid", quick());
|
|
474
|
+
const similar = similarValues(value.value, values);
|
|
475
|
+
if (similar.length)
|
|
476
|
+
finding.similarValues = similar;
|
|
477
|
+
}
|
|
478
|
+
if (target?.by === "label" || parsed.steps[stoppedAt].method === "getByLabel") {
|
|
479
|
+
const labels = await detachedLabels(page, quick());
|
|
480
|
+
if (labels.length)
|
|
481
|
+
finding.detachedLabels = labels;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
// The late check repeats only a lookup that found nothing; a failed state or text check is not repeated.
|
|
485
|
+
const full = scopes.at(-1);
|
|
486
|
+
if (!located.missing || isFrame(full) || full.goto) {
|
|
487
|
+
finding.lateCheck = { outcome: "not-run", reason: located.strict ? "The locator matched more than one element." : "The failure was not a missing element." };
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
let windowMs = 0;
|
|
491
|
+
try {
|
|
492
|
+
windowMs = lateWindow(located.timeoutMs ?? 5_000, overall.remaining());
|
|
493
|
+
}
|
|
494
|
+
catch { /* no time left */ }
|
|
495
|
+
if (windowMs <= 0)
|
|
496
|
+
finding.lateCheck = { outcome: "not-run", reason: "No time was left in the teardown budget." };
|
|
497
|
+
else {
|
|
498
|
+
const started = performance.now();
|
|
499
|
+
const deadline = new Deadline(windowMs, signal);
|
|
500
|
+
const locator = full;
|
|
501
|
+
let passedAfter;
|
|
502
|
+
try {
|
|
503
|
+
do {
|
|
504
|
+
if (await deadline.observe(() => locator.count()) > 0 && await deadline.observe(() => locator.first().isVisible())) {
|
|
505
|
+
passedAfter = Math.round(performance.now() - started);
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
} while (await deadline.pause());
|
|
509
|
+
}
|
|
510
|
+
catch { /* window over or page gone */ }
|
|
511
|
+
finding.lateCheck = passedAfter === undefined ? { outcome: "still-failing", windowMs } : { outcome: "passed-later", afterMs: passedAfter, windowMs };
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
catch {
|
|
516
|
+
finding.reason ??= "The page could not be observed.";
|
|
517
|
+
}
|
|
518
|
+
return finding;
|
|
519
|
+
}
|