mercredi 8 février 2017

How to specify/override/fake geolocation in capybara and phantomjs

I want to set my geolocation before visiting a site. How can I do that?

A small example script is as follows:

Capybara.default_driver = :poltergeist
driver_name = "poltergeist_#{rand 1_000_000}"
Capybara.run_server     = false
Capybara.current_driver = driver_name
Capybara.default_wait_time = 10
opts = {}
Capybara.register_driver driver_name do |app|
  opts = { :js_errors => true, :phantomjs_options => ['--ignore-ssl-errors=yes', '--ssl-protocol=TLSv1'], :timeout => 90 }
  Capybara::Poltergeist::Driver.new app, opts
end
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2'
Capybara.current_session.driver.headers = { 'User-Agent' => user_agent}
Capybara
browser = Capybara
# now my attempt to set geolocation, which is unsuccessful.
  browser.page.execute_script "navigator.geolocation.getCurrentPosition = function(success, failure) { success({coords: {latitude: 25.455755, longitude: -80.511565}, timestamp: Date.now()}); }"

but if i now head to a location-aware site, i dont see data relevant to my geolocation. So overriding geolocation fails.

what is the correct way to set geolocation?

Aucun commentaire:

Enregistrer un commentaire