← Back to library
Cypressnetworkadvancedv10+
Mock browser geolocation
Stub navigator.geolocation.getCurrentPosition before page load to test location-aware UI.
1it('shows Paris when location is mocked', () => {
2 cy.visit('/store-finder', {
3 onBeforeLoad(win) {
4 // Replace the real API with a stub before the page initialises
5 cy.stub(win.navigator.geolocation, 'getCurrentPosition').callsFake((cb) => {
6 cb({ coords: { latitude: 48.8566, longitude: 2.3522 } }); // Paris
7 });
8 },
9 });
10
11 cy.get('[data-testid="use-location-btn"]').click();
12 cy.get('[data-testid="detected-city"]').should('have.text', 'Paris');
13});#geolocation#stub#mock#navigator#location