Location-aware browser tests need a predictable position when an app changes content by region, distance, delivery area, or store availability. Playwright can supply fixed coordinates to the browser context so every run sees the same location instead of the developer machine's real position.

Playwright Test can apply geolocation through test.use() in a spec file so only the matching suite receives the override. The browser context also needs the geolocation permission before page code that calls navigator.geolocation.getCurrentPosition() can receive the configured coordinates.

Use representative coordinates rather than real user locations, and keep the origin under test on HTTPS or localhost because browser geolocation is a secure-context API. A file-scoped test.use() setting keeps the location override limited to the tests that need it.

Steps to set Playwright geolocation:

  1. Choose the coordinates that match the location-aware behavior under test.

    The sample coordinates use San Francisco as neutral data. Replace them with a city, store area, or test fixture location that belongs to the app behavior being checked.

  2. Create tests/geolocation.spec.ts with file-scoped geolocation settings and a coordinate assertion.
    tests/geolocation.spec.ts
    import { test, expect } from '@playwright/test';
     
    const sanFrancisco = {
      latitude: 37.7749,
      longitude: -122.4194,
    };
     
    test.use({
      geolocation: sanFrancisco,
      permissions: ['geolocation'],
    });
     
    test('page receives configured coordinates', async ({ page }) => {
      await page.goto('https://example.com/');
     
      const coords = await page.evaluate(() =>
        new Promise<{ latitude: number; longitude: number; accuracy: number }>((resolve, reject) => {
          navigator.geolocation.getCurrentPosition(
            position => resolve({
              latitude: position.coords.latitude,
              longitude: position.coords.longitude,
              accuracy: position.coords.accuracy,
            }),
            reject,
          );
        }),
      );
     
      expect(coords.latitude).toBeCloseTo(37.7749, 4);
      expect(coords.longitude).toBeCloseTo(-122.4194, 4);
    });

    Use the real app URL in place of https://example.com/. Local app pages served from http://127.0.0.1 or http://localhost can also use browser geolocation during tests. Move the same geolocation and permissions values into a project use block only when an entire browser project should share the location.

  3. Run only the geolocation spec.
    $ npx playwright test tests/geolocation.spec.ts --reporter=line
    
    Running 1 test using 1 worker
    
    [1/1] tests/geolocation.spec.ts:13:5 › page receives configured coordinates
      1 passed (932ms)