6

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?

picardo
  • 24,530
  • 33
  • 104
  • 151

4 Answers4

2

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}}); }"
davetapley
  • 17,000
  • 12
  • 60
  • 86
  • I like this solution as it allows one to mock out the expected result, thank you sir! – Joe Dec 17 '19 at 14:19
0

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.

Joe
  • 405
  • 3
  • 8
0

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.

socjopata
  • 5,028
  • 3
  • 30
  • 33
0

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

How do I enable geolocation support in chromedriver for Selenium?

Community
  • 1
  • 1