Save Google Maps Cost in Selenium WebDriver Tests
How to Bypass Google Maps in your Selenium WebDriver Tests, saving cost!
In the article, “Drive Google Map Address validation with Selenium WebDriver”, I showed how to use Selenium WebDriver to select and validate a Google Map address.
Some software professionals know that Google Maps usage is not free, but the cost is pretty trivial most of the time.
However, when you have a big E2E regression suite (as in real Agile projects), like my father’s WhenWise Suite, with over 600 End-to-End (UI) selenium tests. Not only that, he runs the whole suite frequently (as regression testing) in a BuildWise CT server. The cost adds up quite quickly.
How to save the cost? A logical solution is to minimize the calls to Google Maps services.
There are two types of test scripts:
Some rely on Google Maps validation to return the latitude/longitude address, for later use. For this, no change, will continue to use them.
The address does not matter, just needed to pass validation or …
After a quick search, I found a large percentage of tests fall into the second category, that means, they can be replaced without using Google Maps, since it’s not required.
Of course, we don’t do that with raw Selenium scripts, you have to refactor to page objects to follow a maintainable test design first.
This is an example step (many of them) in WhenWise that enters an address using Google Maps:
confirm_booking_modal_page = ConfirmBookingModalPage.new(browser)
confirm_booking_modal_page.enter_pick_up_address("10 Ruby Street, Newmarket")
Visually:
The actual test steps, defined in the page class:
def enter_pick_up_address(addr)
elem = driver.find_element(:id, "pick_up_address")
elem.clear
elem.send_keys(addr)
sleep 0.75
elem.send_keys(:down)
sleep 0.3
elem.send_keys(:tab)
sleep 1
end
The script enters the address, then when the Google Maps suggestions pops up, chooses the first auto-complete result via the arrow keys. There are minor fixed waits to handle JS delays (usually very quick).
Updating the test to skip Google Maps
Don’t delete the old
enter_pick_up_address
method in the page class, it might still be needed in some other GEO related test cases.Add a new page method:
set_pick_up_address
# bypass google map
def set_pick_up_address(addr)
driver.execute_script("document.getElementById('pick_up_address').value = arguments[0];", addr)
sleep 0.1
end
This version does not involve send keys (which would trigger the Google Maps autocomplete); instead, just set the address directly with JavaScript.
And luckily, due to our maintainable test design, at the test script level, only minor changes are required:
confirm_booking_modal_page = ConfirmBookingModalPage.new(browser)
confirm_booking_modal_page.set_pick_up_address("10 Ruby Street, Newmarket")
Related reading: