microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
63.87k stars 3.46k forks source link

[Feature]: Clip option on Element screenshot #31542

Open Huseyinaltas1 opened 4 days ago

Huseyinaltas1 commented 4 days ago

🚀 Feature Request

Currently clip is only exposed to page.screenhot. Would it be an option for element screenshot like in this locator function: await page.locator('.header').screenshot({ path: 'screenshot.png' , clip:{}});

Example

I have a receipt barcode validation. I need to take screenshot of the receipt in the page(not whole page) with desired portion of only barcode of receipt. I cannot take whole screenshot of page and clip because receipt is not fit in the page and you need to scroll it.

Motivation

I am on process of puppeteer migration to playwright. It is a case for puppeteer but not in playwright. It is really huge missing for our migration.

Screenshot 2024-07-03 at 8 53 20 AM
mxschmitt commented 4 days ago

What are you trying to clip out of your element screenshot? This helps us to prioritise.

Workaround would be https://github.com/microsoft/playwright/issues/2854#issuecomment-655219478

Huseyinaltas1 commented 4 days ago

@mxschmitt here is the page, and element. I want to clip only this barcode. In second picture, it is not possible because receipt longer and barcode is off the page.

Screenshot 2024-07-03 at 12 07 59 PM Screenshot 2024-07-03 at 12 43 49 PM
mxschmitt commented 4 days ago

I assume the receipt is an image? Otherwise you could mask things out or make an screenshot only of the barcode HTML element.

Sounds valid! The workaround in https://github.com/microsoft/playwright/issues/31542#issuecomment-2206882238 should work for you tho. I recommend that while this feature is not implemented yet.

Huseyinaltas1 commented 4 days ago

@mxschmitt yes this is an image on this element css: div.MuiDialog-scrollPaper> div > img