iOS locationServicesEnabled/Authorized capabilities unreliable

Appium: 1.5.3
Xcode: 7.3.1
Simulator: 9.3 iPhone 6
OSx: 10.11.4

Has anyone figured out how to get the iOS only server capabilities locationServicesEnabled/Authorized to be reliable when a test starts up? It works about 80% of the time, and maybe there is something obvious I’m not taking into account since I’m fairly new to Appium. A solution has to also work in SauceLabs.

    caps[:caps][:bundleId] = bundleId
    caps[:caps][:locationServicesEnabled] = true
    caps[:caps][:locationServicesAuthorized] = true

I’ve tried the following on each scenario begin:

  • Hard sleep right after driver start
  • Reset device settings (noReset = false)
  • waitForAppScript setting a delay

Assuming location services is on, then I have a method that sets the gps coordinates:

def self.spoof_location(something)
  gps = get_gps(something)

  # Change location twice in hopes it gets set, also not reliable
  # I've read that setting it to none via the sim debug menu works, and then set the real gps
  # I've also tried set_location too, but that isn't reliable either (another issue i have to deal with)
  # I'm not sure if this is setting it to none or not but setting it to something else first seems to help a little
  execute_script 'target.setLocation( {longitude:-0.1, latitude:0.1, altitude:1} )'

  # now set real coordinates
  execute_script "target.setLocation( {longitude:#{gps[:longitude]}, latitude:#{gps[:latitude]}, altitude:#{gps[:altitude]}} )"

  # Allow the sim enough time for the new location to properly propagate
  sleep 2