picardo
picardo

Reputation: 24896

How to simulate sharing geolocation with Capybara?

My scenario calls for user giving permission for the app to user his location. This dialog looks different in every browser. How do I go about testing it with Capybara?

Upvotes: 6

Views: 1359

Answers (4)

Joe
Joe

Reputation: 405

So, I liked the page.execute_script version above... however I discovered this didn't work with turbolinks.

After much Googling I came up with this as an alternative:

page
  .driver
  .browser
  .execute_cdp(
    'Page.setGeolocationOverride',
    accuracy: 100,
    latitude: latitude.to_f,
    longitude: longitude.to_f
  )

I'm not a big fan of this solution since it's accessing part of their "private api" which of course they recommend against... but it works for now.

UPDATE: At present this works for the selenium_chrome driver, but not selenium_chrome_headless... back to the drawing board.

Upvotes: 0

davetapley
davetapley

Reputation: 17968

I've successfully used this in a Cucumber step definition:

page.execute_script "navigator.geolocation.getCurrentPosition = function(success) { success({coords: {latitude: 50.455755, longitude: 30.511565}}); }"

Upvotes: 2

Tomasz Borowski
Tomasz Borowski

Reputation: 49

If you are testing with Chrome, then this might be helpfull:

How do I enable geolocation support in chromedriver for Selenium?

Upvotes: 0

socjopata
socjopata

Reputation: 5095

I would mock/stub it if I needed it for the test suite flow and didn't want to call the API. Unfortunately, with all goodnes of the automatic testing there are still a few % that need to be done manually. At least that I what I learned from my experience.

Upvotes: 0

Related Questions