Frames and Files
Geolocation in Playwright
Geolocation in Playwright: definition, latitude/longitude setup, permission requirements, examples, mistakes, and practice.
Definition and Brief Explanation
Definition: Geolocation in Playwright sets latitude, longitude, and required permissions for a browser context.
Explanation: Geolocation testing is useful for maps, delivery apps, store locators, pricing by region, and location-based permissions. The context must grant Geolocation permission and define coordinates before the page uses location APIs.
Why It Matters
- It validates location-based features such as maps, delivery zones, nearby stores, or regional content.
- It removes the need for manual browser location prompts.
- It lets tests run the same location scenario in CI repeatedly.
- It pairs with permissions to test realistic browser behavior.
How It Works
- Create a context with latitude and longitude.
- Grant geolocation permission for the site.
- Open the page that reads location.
- Assert the map, address, store list, delivery option, or location message.
Syntax and Examples
Example 1: Set location
test.use({ Geolocation: { latitude: 12.9716, longitude: 77.5946 }, permissions: ['Geolocation'] });
Explanation: Simulates location and grants location permission.
Common Mistakes
- Setting coordinates but forgetting geolocation permission.
- Using real device location instead of controlled test coordinates.
- Sharing one context across tests with different locations.
- Asserting only that the page loaded, not that location changed behavior.
Interview Notes
- What two settings are needed for geolocation?
- How do you test different cities?
- Why does geolocation belong to context?
- What should you assert after setting coordinates?
Practice Task
Test one location where service is available and one where it is unavailable. Assert the message or options shown for each location.