Fake Your GeoLocation in Web Test Automation

How to fake geolocation to assist in web test automation

Test Design

  1. Pass in the desired geolocation to the browser
  2. Click “Use my current location”
  3. Expect “current location” to be the desired geolocation

Test Steps

  1. Decide the latitude and longitude you want to use
    You can use GoogleMaps to find the latitude and longitude values for a location in the URL. Click the location on the Google Map, and the geolocation will be shown in the URL. Below, I am looking at the Sydney Opera House, and the URL has the latitude -33.8571197 and longitude 151.2138464.
GoogleMaps screenshot of the Sydney Opera House & URL which has the longitude and latitude.

JavaScript method

This can be done with the JavaScript script below:

window.navigator.geolocation.getCurrentPosition=function(success){; var position = {'coords' : {'latitude': '-33.8571197','longitude': '151.2138464'}}; success(position);}
# set geo location for user
lati = -33.8571197
longti = 151.2138464
driver.execute_script("window.navigator.geolocation.getCurrentPosition=function(success){\ ; var position = {'coords' : {'latitude': '#{lati}','longitude': '#{longti}'}}; success(\ position);}");

Selenium 4/Chrome Devtools Protocol Method

For this method to succeed, verify that your Selenium version is at least v4 and the Ruby gem selenium-devtools is installed.

coordinates = {
latitude: -33.8571197,
longitude: 151.2138464,
accuracy: 100
}
driver.execute_cdp("Emulation.setGeolocationOverride", coordinates)# Alternatively, driver.devtools.send_cmd() is the same as execute_cdp
driver.find_element(:id, "get-current-location-link").click
sleep 0.25 # wait for JavaScript
# verify autofilled location
expect(driver.find_element(:id, "location")['value']).to eq("2 Circular Quay E, Sydney NSW 2000, Australia")

Complete Script

JavaScript Method

it "Fake Location to Sydney Opera House" do
driver.find_element(:id, "tab-location").click

lati = -33.8571197
longti = 151.2138464
driver.execute_script("window.navigator.geolocation.getCurrentPosition=function(success){\ ; var position = {'coords' : {'latitude': '#{lati}','longitude': '#{longti}'}}; success(\ position);}");

driver.find_element(:id, "get-current-location-link").click
sleep 0.25
expect(driver.find_element(:id, "location")['value']).to eq("2 Circular Quay E, Sydney NSW 2000, Australia")
end
it "Fake Location to Sydney Opera House with CDP (Windows)" do
coordinates = {
latitude: -33.8571197,
longitude: 151.2138464,
accuracy: 100,
}
driver.execute_cdp("Emulation.setGeolocationOverride", coordinates)
driver.get("https://whenwise.agileway.net")
driver.find_element(:id, "tab-location").click
driver.find_element(:id, "get-current-location-link").click
sleep 0.25
expect(driver.find_element(:id, "location")["value"]).to eq("2 Circular Quay E, Sydney NSW 2000, Australia")
end

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store