๐ธ Playwright Media Capture: Your Camera for the Web
The Story of the Web Detective
Imagine youโre a detective investigating a mysterious website. You need evidence! Screenshots are like taking photos of crime scenes, and videos are like security camera footage. Playwright gives you a super-powered camera that can capture anything on a webpage.
๐ฏ What Youโll Learn
- Taking screenshots (quick photos)
- Element screenshots (zooming in on one thing)
- Full page screenshots (the entire scroll)
- Screenshot options (camera settings)
- Recording videos (security camera mode)
- Video configuration (adjusting your camera)
๐ท Taking Screenshots
Think of this like taking a photo with your phone. One click, and you capture whatโs on screen!
The Simplest Screenshot
// Take a photo of what you see
await page.screenshot({
path: 'my-screenshot.png'
});
Thatโs it! You just took a picture of the webpage.
Why Use Screenshots?
- Debugging: โWhat did the page look like when it broke?โ
- Testing: โDoes this button look right?โ
- Documentation: โHereโs proof it worked!โ
graph TD A[๐ Webpage] --> B[๐ธ Take Screenshot] B --> C[๐พ Save as Image] C --> D[๐ Review Later]
๐ Element Screenshots
Sometimes you donโt want a photo of the whole room. You want a close-up of just the cookie jar!
Capturing One Element
// Find the element first
const button = page.locator('button.submit');
// Take a photo of JUST that button
await button.screenshot({
path: 'just-the-button.png'
});
Real Example
// Screenshot the login form only
const loginForm = page.locator('#login-form');
await loginForm.screenshot({
path: 'login-form.png'
});
When to Use Element Screenshots
| Situation | Why Element Screenshot? |
|---|---|
| Button looks weird | Isolate just the button |
| Form validation | Capture error messages |
| Logo testing | Check brand consistency |
๐ Full Page Screenshots
Regular screenshots only capture whatโs visible. But webpages scroll! Full page screenshots are like unrolling a treasure map to see everything.
Capture Everything
await page.screenshot({
path: 'entire-page.png',
fullPage: true // The magic word!
});
Visual Comparison
graph TD subgraph Regular Screenshot A[๐ฑ Visible Area Only] end subgraph Full Page Screenshot B[๐ Everything!] B --> B1[Header] B --> B2[Content] B --> B3[Footer] B --> B4[Hidden stuff below] end
When to Use Full Page
- Long articles or blog posts
- Product listing pages
- Documentation pages
- Any page with scroll
โ๏ธ Screenshot Options
Your camera has settings! Letโs explore them.
All the Options
await page.screenshot({
path: 'photo.png',
fullPage: true,
type: 'png',
quality: 80,
omitBackground: true,
clip: { x: 0, y: 0, width: 300, height: 200 }
});
Option Breakdown
| Option | What It Does | Example |
|---|---|---|
path |
Where to save | 'screenshots/test.png' |
type |
Image format | 'png' or 'jpeg' |
quality |
JPEG quality (0-100) | 80 |
fullPage |
Capture entire scroll | true |
omitBackground |
Transparent background | true |
clip |
Capture specific area | {x, y, width, height} |
PNG vs JPEG
// PNG: Perfect quality, larger file
await page.screenshot({
path: 'crisp.png',
type: 'png'
});
// JPEG: Smaller file, slight blur
await page.screenshot({
path: 'small.jpeg',
type: 'jpeg',
quality: 75
});
Clipping (Cropping)
// Only capture a 200x200 box
await page.screenshot({
path: 'cropped.png',
clip: {
x: 100, // Start 100px from left
y: 50, // Start 50px from top
width: 200, // 200px wide
height: 200 // 200px tall
}
});
๐ฌ Recording Videos
Screenshots are photos. Videos capture movement! Perfect for seeing how a test runs.
Enable Video Recording
const context = await browser.newContext({
recordVideo: {
dir: './videos/' // Folder to save videos
}
});
Complete Example
const browser = await chromium.launch();
// Create context WITH video recording
const context = await browser.newContext({
recordVideo: { dir: './videos/' }
});
const page = await context.newPage();
await page.goto('https://example.com');
await page.click('button');
// IMPORTANT: Close to save the video!
await context.close();
Getting the Video Path
const page = await context.newPage();
// ... do stuff ...
// Get the video file path
const videoPath = await page.video().path();
console.log('Video saved at:', videoPath);
await context.close();
graph TD A[๐ฌ Start Recording] --> B[๐ Open Page] B --> C[๐ฑ๏ธ Do Actions] C --> D[๐ Close Context] D --> E[๐พ Video Saved!]
๐๏ธ Video Configuration
Control your video camera settings!
Size Options
const context = await browser.newContext({
recordVideo: {
dir: './videos/',
size: { width: 1280, height: 720 }
}
});
Configuration Table
| Setting | What It Does | Default |
|---|---|---|
dir |
Save location | Required! |
size.width |
Video width | Viewport width |
size.height |
Video height | Viewport height |
Smart Sizing
// Match viewport for perfect quality
const context = await browser.newContext({
viewport: { width: 1280, height: 720 },
recordVideo: {
dir: './videos/',
size: { width: 1280, height: 720 }
}
});
Smaller Videos for Faster Tests
// Smaller = faster processing
const context = await browser.newContext({
recordVideo: {
dir: './videos/',
size: { width: 640, height: 480 }
}
});
๐ฏ Quick Reference
Screenshots
// Basic screenshot
await page.screenshot({ path: 'shot.png' });
// Full page
await page.screenshot({
path: 'full.png',
fullPage: true
});
// Element only
await page.locator('#elem').screenshot({
path: 'element.png'
});
Videos
// Enable recording
const context = await browser.newContext({
recordVideo: { dir: './videos/' }
});
// Always close to save!
await context.close();
๐ Remember
- Screenshots = Quick photos for debugging
- Element screenshots = Focus on one thing
- Full page = Capture everything, even hidden
- Video = Record the whole journey
- Always close context to save videos!
Youโre now a media capture master! Go capture those bugs! ๐๐ธ