fake geolocation

selenium 4. chrome dev tools

Posted on February 5, 2020

Sometimes our test scenarios require to fake geolocation. Now it’s a piece of cake with Selenium 4.

Here’s a simple test that opens Google Maps and changes geolocation:

Fake location

    private DevTools chromeDevTools;
    private final String mapsUrl = "https://www.google.com/maps/";
    
    @BeforeEach
    public void setup() {
        open(mapsUrl);
        ChromeDriver driver = (ChromeDriver) getWebDriver();
        chromeDevTools = driver.getDevTools();
        chromeDevTools.createSession();
    }

    @Test
    public void fakeLocationTest() {
        Number latitude = 37.422290;
        Number longitude = -122.08405;

        $("h1").shouldBe(visible);

        chromeDevTools.send(
                Emulation.setGeolocationOverride(Optional.of(latitude), Optional.of(longitude), Optional.of(100)));

        sleep(2000);
        open(mapsUrl);
        sleep(2000);

        $("#widget-mylocation").click();
        waitForUrlChanged(4000);

        assertAll(() -> {
            assertTrue(getWebDriver().getCurrentUrl().contains(longitude.toString()),
                    "Opened url does not contain fake longitude");
            assertTrue(getWebDriver().getCurrentUrl().contains(latitude.toString()),
                    "Opened url does not contain fake latitude");
        });

    }

I noticed that it takes some time to apply geolocation changes. That’s why there should be some delays like sleep(2000);.

Full code example is here.