Created
December 22, 2024 08:57
-
-
Save naramdash/85658718a3d58d52ab0f6cb5c5399476 to your computer and use it in GitHub Desktop.
take cropped screenshot with playwright
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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