DEV Community

Styrow.dev
Styrow.dev

Posted on Originally published at styrow.dev

How would you simulate and test features requiring browser permissions (e.g., Geolocation, Camera/Microphone) using Play

πŸ”₯ 90% of Playwright engineers miss these permission hacks for geolocation testingβ€”here’s how to ace them in your next interview scenario.

πŸ“Œ Problem Statement

Testing location‑based or media‑access features in web apps is hard because you can’t manually grant permissions each run. You need a reproducible, automated way to grant/deny geolocation, camera, or microphone permissions and optionally mock sensor data.

πŸ’‘ Solution & Code Walkthrough

Playwright lets you configure permissions at the browserContext level. Grant the permission and supply mock coordinates or device constraints directly when creating the context.

import { chromium, Browser, BrowserContext, Page } from '@playwright/test';

(async () => {
  let browser: Browser;
  let context: BrowserContext;
  let page: Page;

  try {
    // Launch browser
    browser = await chromium.launch();
    // Create a context with geolocation granted and mocked coordinates
    context = await browser.newContext({
      permissions: ['geolocation'],                // βœ… grant permission
      geolocation: { latitude: 34.052235, longitude: -118.243683 }, // Los Angeles
    });
    page = await context.newPage();

    await page.goto('https://maps.google.com');
    // Example assertion: check map center uses mocked location
    const mapCenter = await page.evaluate(() => {
      const el = document.querySelector('.map-center');
      return el?.textContent;
    });
    console.log('Map center:', mapCenter);
  } finally {
    await context?.close();
    await browser?.close();
  }
})();
Enter fullscreen mode Exit fullscreen mode

For camera/microphone, replace permissions: ['camera'] (or microphone) and optionally set recordVideoSize or use page.evaluate to trigger getUserMedia.

πŸ”‘ Key Takeaways

β€’ Use browser.newContext({ permissions: ['geolocation'] }) to grant access.

β€’ Provide mock coordinates via geolocation option for deterministic tests.

❌ Avoid hard‑coding credentials inside the contextβ€”keep them in test fixtures.

βœ… Store permission configs in a reusable testFixtures.ts for DRY code.

❓ Quick Summary Q&A

β€’ Q: Do I need separate contexts for each permission?

A: Yesβ€”each context is isolated, preventing cross‑test bleed.

β€’ Q: Can I deny a permission?

A: Yes, pass permissions: [] or permissions: ['geolocation'] with deny flag in older Playwright versions.

TAGS: playwright, javascript, testing, automation, browser-permissions

────────────────────────────────────────
────────────────────────────────────────

πŸ“² 𝐅𝐑𝐄𝐄 πŒπŽππˆπ‹π„ 𝐀𝐏𝐏 β€” πŸ”πŸŽπŸŽ+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:

πŸ€– 𝐆𝐨𝐨𝐠π₯𝐞 𝐏π₯𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐒𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20260930

🍎 𝐀𝐩𝐩 π’π­π¨π«πž (π’πŽπ’):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20260930&mt=8

────────────────────────────────────────

Top comments (0)