Skip to content

Commit ef50b1b

Browse files
authored
Wait for editor canvas content before screenshot
1 parent bcb945d commit ef50b1b

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

packages/runtime-playground/src/editor-command-runners.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ export async function runEditorOpenCommand({
536536
let viewport: BrowserProbeViewport | null = null
537537
let editorState: EditorStateSnapshot | undefined
538538
let editorValidity: EditorValidityArtifact | undefined
539+
let editorCanvasReadiness: BrowserEditorCanvasProbeSummary | undefined
539540
let authSummary: BrowserProbeAuthSummary | undefined
540541
let pendingError: Error | undefined
541542
let artifact: BrowserArtifact | undefined
@@ -576,8 +577,11 @@ export async function runEditorOpenCommand({
576577
const waitStartedAtMs = Date.now()
577578
try {
578579
await waitForAnyVisibleSelector(page, target.waitSelector, waitTimeoutMs)
580+
editorCanvasReadiness = await waitForEditorOpenCanvasReadiness(page, target.waitSelector, waitTimeoutMs)
579581
finalUrl = page.url()
580-
stepRecords.push(browserStepRecord(1, { kind: "waitFor", selector: target.waitSelector }, "ok", waitStartedAt, waitStartedAtMs, finalUrl, {}))
582+
stepRecords.push(browserStepRecord(1, { kind: "waitFor", selector: target.waitSelector }, "ok", waitStartedAt, waitStartedAtMs, finalUrl, {
583+
...(editorCanvasReadiness ? { editorCanvas: editorCanvasReadiness } : {}),
584+
} as never))
581585
} catch (error) {
582586
const serialized = serializeBrowserError("probe-error", error)
583587
errors.push(serialized)
@@ -600,7 +604,16 @@ export async function runEditorOpenCommand({
600604
htmlSha256 = sha256(Buffer.from(html, "utf8"))
601605
}
602606
if (capture.has("screenshot")) {
603-
await artifactSession.writeGenerated("screenshot", "editor-screenshot.png", (path) => page.screenshot({ path, fullPage: true }).then(() => undefined))
607+
await artifactSession.writeGenerated("screenshot", "editor-screenshot.png", async (path) => {
608+
if (editorCanvasReadiness?.ready) {
609+
const frame = await resolveEditorCanvasFrame(page, target.waitSelector)
610+
if (frame) {
611+
await frame.locator(EDITOR_CANVAS_DEFAULT_LAYOUT_SELECTOR).first().screenshot({ path, timeout: waitTimeoutMs })
612+
return
613+
}
614+
}
615+
await page.screenshot({ path, fullPage: true })
616+
})
604617
screenshotSha256 = await fileSha256(screenshotPath)
605618
}
606619
} finally {
@@ -648,6 +661,7 @@ export async function runEditorOpenCommand({
648661
screenshot: capture.has("screenshot"),
649662
...(editorSummary ? { editor: editorSummary } : {}),
650663
...(editorValidity ? { editorValidity: editorValidity.summary } : {}),
664+
...(editorCanvasReadiness ? { editorCanvas: editorCanvasReadiness } : {}),
651665
viewport,
652666
},
653667
}
@@ -695,6 +709,26 @@ export async function runEditorOpenCommand({
695709
}
696710
}
697711

712+
async function waitForEditorOpenCanvasReadiness(page: import("playwright").Page, waitSelector: string, timeoutMs: number): Promise<BrowserEditorCanvasProbeSummary | undefined> {
713+
if (!waitSelector.includes("editor-canvas")) {
714+
return undefined
715+
}
716+
717+
const probe = await waitForEditorCanvasProbe(page, {
718+
blockSelector: EDITOR_CANVAS_DEFAULT_BLOCK_SELECTOR,
719+
iframeSelector: waitSelector,
720+
layoutSelector: EDITOR_CANVAS_DEFAULT_LAYOUT_SELECTOR,
721+
selectorGroups: editorCanvasSelectorGroups([], EDITOR_CANVAS_DEFAULT_LAYOUT_SELECTOR, EDITOR_CANVAS_DEFAULT_BLOCK_SELECTOR),
722+
startedAtMs: Date.now(),
723+
timeoutMs,
724+
})
725+
if (!probe.summary.ready) {
726+
throw new Error(`Editor canvas was not ready: ${probe.summary.diagnostics.map((diagnostic) => diagnostic.code).join(", ") || "not-ready"}`)
727+
}
728+
729+
return probe.summary
730+
}
731+
698732
export async function runEditorActionsCommand({
699733
artifactRoot,
700734
runPlaygroundCommand,

0 commit comments

Comments
 (0)