Mocking GeoLocation

Ruchi Sharma
3 min readFeb 4, 2021

Where are you?” is a burning question since a smartphone has become a go-to partner of a modern man.

Geolocation makes it possible, from any device connected to the internet, to obtain all types of information in real-time and locate the user with pinpoint accuracy at a given point in time.

Let’s take an example of a cab booking app(Ola, Uber, Cabify etc). A user stays in Dwarka, Delhi and wants to book a cab for MG Road, Gurugram. But due to a location tracking bug in the app, incorrect user’s location is visible to the driver. It makes an application purposeless.

This is the responsibility of a QA to test application on different locations. This could be possible with the help of Selenium 4(currently in alpha version. It is possible at the time you are reading this article, it could be in beta or stable version) feature — Mock GeoLocation.

If there is a use case where you need to override the geolocation of the browser before you hit the website, Selenium 4 Alpha version does support that now by using the power of Chrome DevTools and Protocol (CDP) along with setGeolocationOverride method present in Emulation class.

WHAT DO WE NEED?

  • Selenium WebDriver
  • Chrome Browser
  • Coordinates of the mock location

HOW WE CAN DO IT?

Selenium 4 introduces executeCdpCommand as a new method in the ChromeDriver class. This method takes the command name and a Map of parameters. To find the exact command to use, I seek information on the DevTools Protocol website.

To set location back to default, use the clearGeolocationOverride method.

WAYS TO MOCK GEOLOCATION

There are two ways to mock location:

1. Using executeCdpCommand : Execute a Chrome Devtools Protocol command and get a returned result. The command and command args should follow chrome devtools protocol domains/commands.

Parameters: commandName, parameters

2. Send command to DevTools: Overrides the Geolocation Position or Error. Omitting any of the parameters emulates position unavailable.

Parameters: latitude, longitude, accuracy

WHERE IS THIS USEFUL?

This new feature empowers us to test application features that are dependent on the user’s geolocation. For example, if I go to Nearbuy, it will detect my geolocation and help me to discover restaurants, spa, salons, movie halls, amusement parks and fun things to do nearby my mocked location.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response