My app successfully integrates the Google Maps JS API/Google Places API Web Service to create a few Autocomplete dropdowns. I have my google api browser key set up so development (localhost), staging, and production urls work. However, on any page this service is utilized, acceptance tests (using 127.0.0.1) break. For example:
Capybara::Poltergeist::JavascriptError:
One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).
Google Maps API error: RefererNotAllowedMapError https://developers.google.com/maps/documentation/javascript/error-messages#referer-not-allowed-map-error
Your site URL to be authorized: http://127.0.0.1:52724/clients/3
Google Maps API error: RefererNotAllowedMapError https://developers.google.com/maps/documentation/javascript/error-messages#referer-not-allowed-map-error
Your site URL to be authorized: http://127.0.0.1:52724/clients/3
at https://maps.googleapis.com/maps/api/js?v=3.exp&key=(my_browser_key)&signed_in=true&libraries=places:34 in hb
I've tried adding different 127.0.0.1 configurations into my Browser Key credentials that haven't worked, such as:
http://127.0.0.1:* , */127* , and */127.0.0.1:\d\d\d\d\d/*
That last one looks funny because every new run of my test suite generates 5 random digits after 127.0.0.1:, as seen in the error above.
*I don't want to ignore JS errors by changing my poltergeist config as mentioned in the error. With that said, I am NOT actually using the service on any of these acceptance tests. I don't want to test google functionality, I want to test the custom functionality surrounding these dropdowns.
Was able to circumnavigate this by setting the app_host and server_port directly into my Capybara config. Thanks to the accepted answer here. I added this to my spec_helper file:
def set_host(host)
default_url_options[:host] = host
Capybara.app_host = "http://" + host
end
RSpec.configure do |config|
config.before(:each) do
Capybara.current_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
set_host("127.0.0.1:30111")
Capybara.server_port = 30111
end
end
Then I specified this exact server_port in my Google API key credentials. Acceptance tests now pass.
UPDATE:
It seems that Google's instructions are just out of date and incomplete (sigh). After more experimentation, simply 127.0.0.1
without any *
or /
does the job.
The above solution works as well.