Browser Configuration

Back

Loading concept...

๐ŸŽญ 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! ๐ŸŽฌโœจ

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.