๐ญ Playwright Browser Configuration
Making Your Browser Pretend to Be Anyone, Anywhere!
๐ The Story: Your Browser is an Actor
Imagine your browser is like an actor on stage. This actor can:
- Pretend to be in Paris (even though youโre at home!)
- Speak French (locale)
- Wear a dark costume (dark mode)
- Hide backstage (headless mode)
Playwright lets you direct this actor. You tell it where to go, what language to speak, and how to look. Letโs learn how! ๐ฌ
๐ Geolocation Emulation
โPretend Youโre Somewhere Else!โ
What is it? Your browser can fake its location. Want to test a website as if youโre in Tokyo? Easy!
Simple Example:
// Tell the browser:
// "You are now in Paris!"
await context.grantPermissions(['geolocation']);
const context = await browser.newContext({
geolocation: {
latitude: 48.8566, // Paris
longitude: 2.3522
}
});
Why use it?
- Test location-based features
- See what users in other countries see
- Check maps and delivery apps
๐ Locale and Timezone
โSpeak the Language, Live the Time!โ
Locale = What language and format your browser uses Timezone = What clock your browser shows
Simple Example:
const context = await browser.newContext({
locale: 'fr-FR', // French
timezoneId: 'Europe/Paris' // Paris time
});
What changes?
| Setting | Effect |
|---|---|
locale: 'fr-FR' |
Dates show as โ15 janvierโ |
locale: 'en-US' |
Dates show as โJanuary 15โ |
timezoneId |
new Date() uses that zone |
๐จ Color Scheme Emulation
โLight Mode or Dark Mode? You Choose!โ
Websites can look different in dark mode. Test both without changing your computer settings!
Simple Example:
// Test dark mode
const darkContext = await browser.newContext({
colorScheme: 'dark'
});
// Test light mode
const lightContext = await browser.newContext({
colorScheme: 'light'
});
Options:
'light'- Bright background'dark'- Dark background'no-preference'- System default
๐ Permissions Handling
โMay I Use Your Camera?โ
Websites ask for permissions. Playwright can say YES or NO automatically!
Simple Example:
const context = await browser.newContext({
permissions: ['geolocation', 'notifications']
});
// Or grant later:
await context.grantPermissions(['camera', 'microphone']);
// Or deny:
await context.clearPermissions();
Common Permissions:
'geolocation'- Location'notifications'- Alerts'camera'- Webcam'microphone'- Audio
๐ป Headless Mode
โThe Invisible Browser!โ
Headless = Browser runs invisibly (no window pops up)
graph TD A["Your Test Code"] --> B{Headless?} B -->|Yes| C["๐ฒ No Window<br>Fast & Quiet"] B -->|No| D["๐ฅ๏ธ Window Opens<br>You Can Watch"]
Simple Example:
// Invisible (default)
const browser = await chromium.launch({
headless: true
});
// No window appears!
// Tests run super fast!
Why use headless?
- โก Faster - No graphics to draw
- ๐ค CI/CD - Perfect for servers
- ๐พ Less memory - Lighter on resources
๐๏ธ Headed Mode
โI Want to SEE Whatโs Happening!โ
Sometimes you WANT to watch. Debug mode!
Simple Example:
// Show me the browser!
const browser = await chromium.launch({
headless: false // Window pops up!
});
When to use headed mode?
- ๐ Debugging - Watch your test run
- ๐ธ Screenshots - See exact moment
- ๐ Finding bugs - See what went wrong
๐ Browser Channel Selection
โWhich Chrome Do You Want?โ
Playwright can use different versions of Chrome!
Simple Example:
// Use your installed Chrome
const browser = await chromium.launch({
channel: 'chrome'
});
// Or use Chrome Beta
const browserBeta = await chromium.launch({
channel: 'chrome-beta'
});
Available Channels:
| Channel | What It Is |
|---|---|
'chrome' |
Regular Chrome |
'chrome-beta' |
Beta version |
'msedge' |
Microsoft Edge |
'msedge-beta' |
Edge Beta |
๐ก๏ธ Proxy Configuration
โGo Through a Secret Tunnel!โ
A proxy is like a middleman. Your browser talks to the proxy, and the proxy talks to websites.
graph TD A["Your Browser"] -->|Through| B["๐ก๏ธ Proxy Server"] B -->|Hidden| C["๐ Website"] C -->|Response| B B -->|Back| A
Simple Example:
const browser = await chromium.launch({
proxy: {
server: 'http://myproxy.com:8080',
username: 'user', // Optional
password: 'pass' // Optional
}
});
Why use a proxy?
- ๐ Privacy - Hide your real IP
- ๐ Testing regions - Access different content
- ๐ข Corporate - Required by some networks
๐ฏ Putting It All Together
Hereโs a complete example with EVERYTHING:
const { chromium } = require('playwright');
const browser = await chromium.launch({
headless: false, // Watch it run
channel: 'chrome', // Use Chrome
proxy: {
server: 'http://proxy:8080'
}
});
const context = await browser.newContext({
geolocation: {
latitude: 35.6762,
longitude: 139.6503 // Tokyo!
},
locale: 'ja-JP', // Japanese
timezoneId: 'Asia/Tokyo', // Tokyo time
colorScheme: 'dark', // Dark mode
permissions: ['geolocation']
});
const page = await context.newPage();
// Now test as a Japanese user in Tokyo! ๐ฏ๐ต
๐ You Did It!
Now you know how to make your browser:
- ๐ Fake its location (Geolocation)
- ๐ Speak any language (Locale/Timezone)
- ๐จ Switch light/dark mode (Color Scheme)
- ๐ Auto-accept permissions (Permissions)
- ๐ป Run invisibly (Headless)
- ๐๏ธ Show itself (Headed)
- ๐ Use different browsers (Channel)
- ๐ก๏ธ Go through tunnels (Proxy)
Youโre now a Playwright director! ๐ฌโจ
