← Back to library
Playwrightnetworkadvancedv1.20+

Mock browser geolocation

Inject fake GPS coordinates before the page loads to test location-aware features.

1test('shows the correct city for mocked coordinates', async ({ browser }) => {
2  const context = await browser.newContext({
3    geolocation: { latitude: 48.8566, longitude: 2.3522 }, // Paris
4    permissions: ['geolocation'], // grant without a browser prompt
5  });
6  const page = await context.newPage();
7
8  await page.goto('/store-finder');
9  await page.getByRole('button', { name: 'Use my location' }).click();
10
11  // The app should resolve the mocked coordinates to Paris
12  await expect(page.getByTestId('detected-city')).toHaveText('Paris');
13  await context.close();
14});
#geolocation#mock#context#permissions#GPS