Skip to content

Commit 66d760b

Browse files
Normalize waitForLoadState targets in replay scripts
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 28bb1dd commit 66d760b

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/agent/shared/action-cache-script.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ const normalizeWaitMs = (value: unknown): number => {
3838
return parsed >= 0 ? parsed : 1000;
3939
};
4040

41+
const normalizeWaitUntil = (value: unknown): "domcontentloaded" | "load" | "networkidle" => {
42+
const parsed = asNonEmptyTrimmedString(value);
43+
if (parsed === "load" || parsed === "networkidle") {
44+
return parsed;
45+
}
46+
return "domcontentloaded";
47+
};
48+
4149
export function createScriptFromActionCache(
4250
params: CreateScriptFromActionCacheParams
4351
): string {
@@ -111,8 +119,7 @@ ${indent}await page.waitForTimeout(${waitMs});`;
111119
const actionParams = isRecord(step.actionParams)
112120
? step.actionParams
113121
: undefined;
114-
const waitUntil =
115-
asNonEmptyTrimmedString(step.arguments?.[0]) ?? "domcontentloaded";
122+
const waitUntil = normalizeWaitUntil(step.arguments?.[0]);
116123
const timeoutMs = asNumber(step.arguments?.[1] ?? actionParams?.timeout);
117124
if (typeof timeoutMs === "number" && Number.isFinite(timeoutMs)) {
118125
return `${indent}// Step ${step.stepIndex}

src/agent/shared/action-cache.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,28 @@ describe("action cache helpers", () => {
213213
expect(script).toContain('await page.waitForLoadState("domcontentloaded");');
214214
});
215215

216+
it("normalizes unsupported waitForLoadState targets to domcontentloaded", () => {
217+
const waitEntry: ActionCacheEntry = {
218+
stepIndex: 14,
219+
instruction: "wait unsupported",
220+
elementId: null,
221+
method: null,
222+
arguments: ["interactive"],
223+
actionType: "waitForLoadState",
224+
success: true,
225+
message: "ok",
226+
frameIndex: null,
227+
xpath: null,
228+
};
229+
230+
const script = createScriptFromActionCache({
231+
steps: [waitEntry],
232+
});
233+
234+
expect(script).toContain('await page.waitForLoadState("domcontentloaded");');
235+
expect(script).not.toContain('await page.waitForLoadState("interactive");');
236+
});
237+
216238
it("skips helper generation when xpath is missing", () => {
217239
const actElementEntry: ActionCacheEntry = {
218240
stepIndex: 3,

0 commit comments

Comments
 (0)