Top

Selenium 4: Mocking Geolocation

Selenium 4: Mocking Geolocation

Selenium 4 is in alpha mode, and I’ve been trying out some of the new features. In this upcoming release, they are providing access to Chrome DevTools and the Chrome DevTools Protocol (CDP). Since I’ve been stuck in the house for 2 months due to COVID-19, I’m going to use this new feature to travel to Austin, TX to do some shopping…virtually at least!

Here is the recipe for programmatically overriding your browser’s geolocation.

 

 

Recipe to Change Geolocation in Chrome Browser

Ingredients

  • Selenium WebDriver 4+
  • Chrome browser
  • Coordinates of mock location

Instructions

  1. Create an instance of ChromeDriver
  2. Invoke executeCdpCommand method
— Angie Jones

 

Live Stream Video

 

 

Ingredients

 

1 Selenium WebDriver 4+

 

Access to the Chrome DevTools Protocol was added in Selenium 4, so you’ll need that version as a minimum. At the time of this writing, Selenium 4 is in alpha, so note that this is experimental right now. Using maven, I add the dependency to my pom.xml file.

 

 

 

2 Chrome Browser

 

Because we’re using Chrome DevTools, of course, we will need the Chrome browser. I don’t yet see similar support for Firefox, and I’m not sure if that is on the roadmap.

 

 

3 Coordinates of Mock Location

 

To override the actual location of your browser, you’ll need to supply the longitude and latitude of the mock location. To find this info, you can use Google Maps.

 

Notice the latitude and longitude in the URL. The first number is the latitude (30.3079823) and the second number is the longitude (-97.893803).

 

 

Instructions

 

1 Create an instance of ChromeDriver

 

I usually create my instance as WebDriver driver = new ChromeDriver(); however, in doing this, my object’s type is WebDriver and not ChromeDriver, which means my object does not have access to the methods that are specific to ChromeDriver. So, in order to use the CDP methods, I have to declare my driver as ChromeDriver.

 

 

2 Invoke executeCdpCommand method

 

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 consulted the DevTools Protocol website and searched for geolocation.

 

I see two setGeolocationOverride commands here. I clicked on the one for Page and it was deprecated, so I knew to use the Emulation one. I copied this entire String as the command.

Now, I need the parameters for the executeCdpCommand method. I clicked on the Emulation.setGeolocationOverride method on the Chrome DevTools Protocol website to get more info.

 

 

Given this, I can see that I need to add three entries to my Map: the latitude, the longitude, and the accuracy.

Confession: I have no idea what the range is for accuracy, but when I use 0 it does not work and when I use 1 or anything greater, it does. 🤷🏽‍♀️

Now that I have the command and the parameters, I can finally call the executeCdpCommand method.

After this, be sure to refresh your page, or navigate to the page you’d like to go to.

 

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 Old Navy’s store locator, it will detect my geolocation and show me stores near me.

 

 

See Code on Github

Angie Jones
No Comments

Post a Comment