Skip to content

Instantly share code, notes, and snippets.

@naramdash
Created December 22, 2024 08:57
Show Gist options
  • Save naramdash/85658718a3d58d52ab0f6cb5c5399476 to your computer and use it in GitHub Desktop.
Save naramdash/85658718a3d58d52ab0f6cb5c5399476 to your computer and use it in GitHub Desktop.
take cropped screenshot with playwright
import { test, expect } from "@playwright/test";
import { writeFileSync } from "node:fs";
import sharp from "sharp";
test("take shots", async ({ page }) => {
await page.goto("https://playwright.dev/");
const imageBuffer = await page.screenshot();
const base = sharp(imageBuffer)
.extract({
top: 10,
left: 10,
width: 100,
height: 100,
})
.png();
const file = await base.toFile("./cropped.png");
const base64 =
"data:image/png;base64," + (await base.toBuffer()).toString("base64");
writeFileSync("./base64.json", JSON.stringify({ base64 }));
console.log(base64);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment