playwright geolocation not applied in test: how to fix
Makes Playwright geolocation permissions actually take effect. Use when the page still sees the default location. Not for IP-based geolocation, which this cannot change.
TL;DR
Set the geolocation coordinates and grant the geolocation permission when the browser context is created; granting permission after the page is open is too late. Both options belong in the same newContext call.
The query
playwright geolocation not applied in test: how to fixUse this when
- the page ignores mocked coordinates
- a permission prompt appears despite grants
- location tests flake
Not for
- IP-based geolocation
- timezone mocking (a separate option)
- real-device GPS
Steps
- Move geolocation into context creation: browser.newContext with geolocation coordinates and permissions ['geolocation'] together. Expected output: one newContext call carries both options.
- Remove any later permission grants for geolocation in the test. Expected output: no grantPermissions call for geolocation after page creation.
- Have the page query its geolocation and assert the returned coordinates. Expected output: the coordinates match what the context was given.
- If tests share a context, give geolocation tests their own context so coordinates do not bleed across tests. Expected output: each geolocation test creates a fresh context.
- Re-run and confirm the app behaves as if at the mocked location. Expected output: location-dependent UI shows the mocked area.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_I7GyRPm4RzEIPlxW8OfddQ
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.