DEV Community

Styrow.dev
Styrow.dev

Posted on Originally published at styrow.dev

How can you programmatically mock a browser's Geolocation API for consistent and controlled testing scenarios in Playwri

⚠️ Stop relying on real-world GPS data in your test suites!
How do you test location-dependent UI features reliably inside CI/CD pipelines without physical movement?

📌 Problem Statement
Testing features like localized content, store finders, or map renderers using real hardware GPS is flaky, slow, and impossible on headless CI servers. You need a way to inject precise geographical coordinates programmatically.

💡 Solution & Code Walkthrough
Playwright handles this cleanly at the BrowserContext level. You must combine permission granting with coordinate injection so the browser permits access to the mocked data.

import { test, expect } from '@playwright/test';

test('mock user location to Paris', async ({ context, page }) => {
    // 1. Grant explicit geolocation permissions
    await context.grantPermissions(['geolocation']);

    // 2. Set mock latitude & longitude (e.g., Paris, France)
    await context.setGeolocation({ latitude: 48.8566, longitude: 2.3522 });

    // 3. Navigate to your target web app
    await page.goto('https://example.com/map-dashboard');

    // 4. Assert location-based UI renders correctly
    await expect(page.locator('#location-display')).toContainText('Paris');
});
Enter fullscreen mode Exit fullscreen mode

🔑 Key Takeaways
❌ Don't set geolocation globally without context isolation, or it may bleed into parallel tests.
✅ Do apply context.grantPermissions(['geolocation']) prior to navigating to your application page.
• Isolate mocks per test inside independent browser contexts for maximum predictability.

❓ Quick Summary Q&A
Q: Why isn't my mock working?
A: You likely forgot to grant permissions using context.grantPermissions(['geolocation']). Browsers block geolocation calls by default.

TAGS: playwright, typescript, testing, automation, qa

────────────────────────────────────────
🚀 Master automation on the go! Download our app for daily coding challenges, interview prep, and instant cheatsheets:
• App Store: ────────────────────────────────────────

📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
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_20261203

🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261203&mt=8

────────────────────────────────────────
• Google Play: ────────────────────────────────────────

📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
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_20261203

🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261203&mt=8

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

Top comments (0)