Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve playwright test performance slightly by avoiding base64 conversion #2064

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions packages/sit-onyx/src/playwright/screenshots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type TestArgs = Parameters<Parameters<typeof test>[2]>[0];
export const executeMatrixScreenshotTest = async <TColumn extends string, TRow extends string>(
options: MatrixScreenshotTestOptions<TColumn, TRow>,
) => {
test(`${options.name}`, async ({ mount, page, browserName, makeAxeBuilder }) => {
test(`${options.name}`, async ({ mount, page, browserName, makeAxeBuilder, context }) => {
// limit the max timeout per permutation
const timeoutPerScreenshot = 25 * 1000;
test.setTimeout(options.columns.length * options.rows.length * timeoutPerScreenshot);
Expand Down Expand Up @@ -93,18 +93,14 @@ export const executeMatrixScreenshotTest = async <TColumn extends string, TRow e

const id = `${row}-${column}`;

return (
<img
width={box?.width}
height={box?.height}
style={{ gridArea: id }}
src={`data:image/png;base64,${Buffer.from(screenshot).toString("base64")}`}
alt={id}
/>
);
return {
box,
id,
screenshot,
};
};

const screenshots: JSX.Element[] = [];
const screenshotMap = new Map<string, Awaited<ReturnType<typeof getScreenshot>>>();

for (const row of options.rows) {
for (const column of options.columns) {
Expand All @@ -122,11 +118,32 @@ export const executeMatrixScreenshotTest = async <TColumn extends string, TRow e
</div>
);

const screenshot = await getScreenshot(wrappedElement, column, row);
screenshots.push(screenshot);
const data = await getScreenshot(wrappedElement, column, row);
screenshotMap.set(data.id, data);
}
}

await context.route("/_playwright-matrix-screenshot*", (route, request) => {
const url = new URL(request.url());
const wantedId = url.searchParams.get("id") ?? "";

return route.fulfill({
status: 200,
contentType: "image/png",
body: screenshotMap.get(wantedId)?.screenshot,
});
});

const screenshots = Array.from(screenshotMap.values()).map(({ box, id }) => (
<img
width={box?.width}
height={box?.height}
style={{ gridArea: id }}
src={`/_playwright-matrix-screenshot?id=${id}`}
alt={id}
/>
));

const component = await mount(
<ScreenshotMatrix
columns={options.columns}
Expand Down
Loading