Geolocation Testing

Introduction

Many modern web applications provide location-based features such as nearby stores, weather updates, local search results, delivery availability, and region-specific content. These applications often use the browser’s geolocation API to determine the user’s current location.

During automation testing, relying on a tester’s actual GPS location is neither practical nor reliable. Chrome DevTools Protocol (CDP) allows Selenium to override the browser’s geolocation, enabling automation scripts to simulate different locations without requiring a physical GPS device.

In this tutorial, you’ll learn how to perform geolocation testing using Selenium with Python, understand how geolocation override works, and explore practical examples, real-world scenarios, common mistakes, and best practices.


What is Geolocation Testing?

Geolocation Testing is the process of verifying how a web application behaves for users in different geographic locations.

Instead of using the computer’s actual location, Selenium can simulate any location by providing:

  • Latitude

  • Longitude

  • Accuracy

This allows automation scripts to test location-based functionality from anywhere in the world.

Example

Actual Location

  Bangalore

        │

        ▼

CDP Geolocation Override

New Delhi │ ▼ Website Behaves as if User is in New Delhi

Why Perform Geolocation Testing?

Geolocation testing helps you:

  • Test location-based features.

  • Verify regional content.

  • Test nearby store locators.

  • Validate delivery availability.

  • Verify location-specific permissions.

  • Test maps and navigation.

  • Improve application reliability.


How Selenium Performs Geolocation Testing

Selenium communicates with Chrome DevTools Protocol (CDP) using the execute_cdp_cmd() method.

The CDP command Emulation.setGeolocationOverride replaces the browser’s actual location with a simulated one.

Example:

driver.execute_cdp_cmd(
    "Emulation.setGeolocationOverride",
    {
        "latitude": 28.6139,
        "longitude": 77.2090,
        "accuracy": 100
    }
)

After the location is overridden, every webpage that requests the browser’s location receives the simulated coordinates instead of the real device location.


Example

from selenium import webdriver


# Topic: 37. Chrome DevTools Protocol (CDP) - Geolocation Testing
# Practice site: https://www.testmuai.com/selenium-playground/geolocation-testing
# Run: pytest -s 37_examples/test_06_geolocation_testing.py
#
# CDP can override the browser geolocation so tests can verify location-based
# behavior without relying on the real device GPS.


def test_geolocation_testing():
    driver = webdriver.Chrome()

    try:
        driver.execute_cdp_cmd(
            "Emulation.setGeolocationOverride",
            {
                "latitude": 28.6139,
                "longitude": 77.2090,
                "accuracy": 100,
            },
        )

        driver.get("https://www.testmuai.com/selenium-playground/geolocation-testing")

        assert "geolocation" in driver.current_url.lower()
        assert driver.title != ""
    finally:
        driver.quit()

Understanding the Code

Import Required Library

from selenium import webdriver

Imports the Selenium WebDriver module used to launch and control the Chrome browser.


Create a Chrome Browser Instance

driver = webdriver.Chrome()

Starts a new Chrome browser session.


Override the Browser’s Geolocation

driver.execute_cdp_cmd(
    "Emulation.setGeolocationOverride",
    {
        "latitude": 28.6139,
        "longitude": 77.2090,
        "accuracy": 100,
    },
)

The execute_cdp_cmd() method sends the Emulation.setGeolocationOverride command to Chrome DevTools.

The supplied coordinates simulate a location in New Delhi, India.

The parameters are:

  • latitude – Specifies the north-south position.

  • longitude – Specifies the east-west position.

  • accuracy – Represents the estimated accuracy of the simulated location in meters.


Open the Practice Website

driver.get(
    "https://www.testmuai.com/selenium-playground/geolocation-testing"
)

Navigates to the practice page.

If the website requests the user’s location, Chrome returns the simulated coordinates instead of the actual device location.


Verify the Current URL

assert "geolocation" in driver.current_url.lower()

Verifies that the browser successfully opened the Geolocation Testing page.


Verify the Page Title

assert driver.title != ""

Ensures that the webpage loaded successfully by confirming that the page title is not empty.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Practical Example

Suppose an online food delivery application shows restaurants based on the user’s location.

The automation script:

  • Simulates a location in New Delhi.

  • Opens the website.

  • Verifies that restaurants available in New Delhi are displayed.


Automation Testing Example

Consider a weather forecasting application.

The automation script:

  • Overrides the browser’s location.

  • Opens the weather page.

  • Verifies that weather information for the simulated city is displayed correctly.


Real-World Example

Geolocation testing is commonly used in:

  • Food delivery applications

  • Ride-sharing platforms

  • Weather applications

  • Store locator websites

  • Travel booking websites

  • Banking applications

  • Maps and navigation systems

  • E-commerce websites with location-based services

Many organizations use geolocation testing to ensure their applications behave correctly for users in different regions.


Advantages of Geolocation Testing

  • Simulates any geographic location.

  • Tests location-based features without GPS.

  • Improves automation reliability.

  • Reduces dependency on physical devices.

  • Supports region-specific testing.

  • Easy to integrate into Selenium automation.


Common Mistakes Beginners Make

Setting the Geolocation After Loading the Website

Always override the location before opening the webpage.

Otherwise, the website may already have requested the browser’s location.


Using Incorrect Coordinates

Invalid latitude or longitude values may cause unexpected application behavior.

Always use valid geographic coordinates.


Forgetting Location Permissions

Some websites require permission to access the browser’s location.

Ensure that location access is allowed when testing geolocation features.


Assuming Every Website Uses Browser Geolocation

Not all applications rely on the browser’s location.

Some websites determine the user’s location using IP address or server-side services instead.


Best Practices

  • Override the location before navigating to the webpage.

  • Use realistic latitude and longitude values.

  • Test multiple geographic regions.

  • Verify both UI behavior and business logic.

  • Reset or update the location when testing multiple scenarios.

  • Combine geolocation testing with functional validation.


Conclusion

Geolocation Testing allows Selenium to simulate different geographic locations using Chrome DevTools Protocol. By sending the Emulation.setGeolocationOverride command through execute_cdp_cmd(), automation engineers can verify location-based functionality without depending on the tester’s actual GPS location. This capability is especially valuable for testing applications that provide region-specific content, nearby services, or location-aware features.


Frequently Asked Questions (FAQs)

What is Geolocation Testing?

Geolocation Testing verifies how a web application behaves when accessed from different geographic locations.


Which CDP command overrides the browser’s location?

Use:

driver.execute_cdp_cmd(
    "Emulation.setGeolocationOverride",
    {...}
)

Which Selenium method executes the geolocation command?

Use the execute_cdp_cmd() method.


Can Selenium simulate different cities or countries?

Yes.

You can provide different latitude and longitude values to simulate virtually any location supported by the browser.


Where is Geolocation Testing commonly used?

It is commonly used in food delivery applications, weather services, ride-sharing platforms, store locators, travel websites, banking applications, maps, and other location-aware web applications.


Key Takeaways

  • Geolocation Testing verifies location-based functionality.

  • Selenium uses execute_cdp_cmd() to communicate with Chrome DevTools Protocol.

  • Emulation.setGeolocationOverride overrides the browser’s location.

  • Specify latitude, longitude, and accuracy to simulate a location.

  • Override the location before loading the webpage.

  • Geolocation testing is widely used for testing modern location-aware web applications.