Drive Google Map Address validation with Selenium WebDriver
How to use Selenium WebDriver to select and validate a Google Map address
Integrating Google Maps to validate an address is a common feature on web pages. This article shows how to automate that in raw Selenium WebDriver.
Table of Contents:
· Test Design
∘ Execution (Video)
· Test Script:
∘ Refinement
∘ Refactoring
· Zhimin’s Notes
Test Design:
Quite straightforward.
Find the address text field element, and clear it first.
In case it was not empty.Typing in the new address to the element.
Don’t need to be the full address, as Google maps will try to auto-complete it.Send
downkeys (to the same element) repeatedly until the desired entry is highlighted.
Add a minor delay to let JS complete the operation.Send
tabkey
Add delays to JS to set GPS data, if the web page had the placeholders for them.
Execution (Video)

Astute readers might notice that I ran the selenium steps, directly against the current browser window. This is a great feature of TestWise. For more, check out this article, “Attach test execution to the existing browser”.
Test Script:
The raw Selenium steps to perform the above operations in the wonderful Ruby language.
elem = driver.find_element(:id, "user_address")
elem.clear
elem.send_keys("10 ruby new")
sleep 0.8 # wait for JavaScript dropdown to appear
elem.send_keys(:down)
sleep 0.5 # wait for JavaScript to select next address
elem.send_keys(:down)
sleep 0.5
elem.send_keys(:down)
sleep 0.5
elem.send_keys(:down)
sleep 0.5
elem.send_keys(:tab)
sleep 0.8 # wait for drop down to close and set GPS dataRefinement:
There are duplications (four down keys). Shorten that in a loop.
4.times { elem.send_keys(:down); sleep 0.5 }Refactoring:
Once the test step(s) are working, I immediately refactor them into a Page Class, to make it more readable and maintainable.
class UserProfilePage < AbstractPage
def enter_user_address(addr)
elem = driver.find_element(:id, "user_address")
elem.clear
elem.send_keys(addr)
sleep 0.8
4.times { elem.send_keys(:down); sleep 0.5 } # select the 4th address
elem.send_keys(:tab)
sleep 0.8
end
endThe usage will be like the following:
user_profile_page = UserProfilePage.new(driver)
user_profile_page.enter_user_address("10 ruby new")For more, check the articles:
Zhimin’s Notes:
In real test automation, the full suite of automated end-to-end (via UI) test scripts is executed multiple times daily. This can lead to some costs, as Google Map API is not free.
One workaround is to set the address directly using JavaScript.
# by pass google map
def set_pick_up_address(addr)
driver.execute_script("document.getElementById('pick_up_address').value = arguments[0];", addr)
sleep 0.1
endOf course, if needed, you shall take care of GPS (latitude & longitude) as well, on the client side (using JS) or the server side (I often used a caching service to return GPS data for test addresses), only available on testing server instances, of course.



