Unable to mock the location of automated browser: pytest, selenium

106 Views Asked by At

I am using selenium, pytest, splinter and gherkins,

I have a test case where I want to mock the browser actual location and timezone.

I tried many approach like, executing js scripts and emulating dev tools approach. But nothing works for me. Does anyone have idea or example where they have done so?

1

There are 1 best solutions below

1
Teejay Bruno On BEST ANSWER

The following code successfully sets location and timezone for the web driver --

from selenium import webdriver

class Params:
    LOCATION = {
            "latitude": 25.2048,
            "longitude": 55.2708,
            "accuracy": 100,
        }

    TIMEZONE = {
        "timezoneId": "America/New_York"
        }

driver = webdriver.Chrome()
driver.execute_cdp_cmd("Emulation.setGeolocationOverride", Params.LOCATION)
driver.execute_cdp_cmd("Emulation.setTimezoneOverride", Params.TIMEZONE)

driver.get("https://my-location.org/") # Verify location
driver.get("https://webbrowsertools.com/timezone/") # Verify timezone