Skip to content

Commit

Permalink
chore: improve playwright test performance slightly by avoiding base6…
Browse files Browse the repository at this point in the history
…4 conversion (#2064)

improve playwright test performance slightly by avoiding base64
conversion
  • Loading branch information
JoCa96 authored Dec 4, 2024
1 parent a2cffa8 commit 23cc217
Showing 1 changed file with 30 additions and 13 deletions.
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

0 comments on commit 23cc217

Please sign in to comment.