10

Is there any programmatic way to control the xcode > simulate location command? I have external location test scripts I'd like to run, and ideally the test suite could cause xcode to change the location of the connected phone at will.

Is this possible, and if so how?

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
  • Can you elaborate on what the 'external location test scripts' are? Are they Xcode unit tests? – InsertWittyName Jul 23 '16 at 13:14
  • @InsertWittyName It's scripts literally running outside of xcode doing other things. If I can trigger xcode via automator or xctool or something, that would be ideal. I'm automating more of a whole system that needs an iphone moving and located in positions at different times. – Stefan Kendall Jul 23 '16 at 15:30

4 Answers4

7

Not sure if it's exactly what you're after, but you can have different unit test bundles use different locations (or GPX files) by setting it up in the scheme.

You could then have unit tests in each bundle which test what you need regarding that specific location.

Scheme

xctool can also just run the unit tests in a specific target using the -only option:

path/to/xctool.sh \
  -workspace YourWorkspace.xcworkspace \
  -scheme YourScheme \
  test -only SomeTestTarget
InsertWittyName
  • 3,930
  • 1
  • 22
  • 27
  • This could work...so I could have a fixed gpx file which I wrote to, then execute the unit tests via xctool on the device. It's not a perfect solution, but it's a partial one. – Stefan Kendall Jul 24 '16 at 18:55
5

There is a GitHub project called Pokemon-Go-Controller that does exactly what you want.

Overview of the solution:

  1. Create a gpx file
  2. Create a blank project referencing (not copying) that gpx file and run it on your device
  3. Run auto clicker which will constantly click update location in Xcode
  4. Update gpx file using some script and the device location will be automatically updated

Instead of the auto clicker you can use this Apple Script:

#Will continue update the location to the location name below from Xcode:

property locationName : "pokemonLocation" #name of your gpx filex

###########################
tell application "System Events"
    tell process "Xcode"
        repeat while true
            click menu item locationName of menu 1 of menu item "Simulate Location" of menu 1 of menu bar item "Debug" of menu bar 1
            delay 0.2
        end repeat
    end tell
end tell
bzz
  • 5,556
  • 24
  • 26
  • I really like your answer and the custom apple script, however when I tried to use it just now it fails with a vague error stating that it cannot find either: locationName, menu 1, menu item "Simulate Location" or any of the others in the statement. Could you please check if you answer is still up to date? Thank you in advance :) – Eric Jan 04 '19 at 22:29
2

Yes, it is possible. 1. Set up the GPX files as described in @InsertWittyName 's answer, or as described in this blog post. 2. Use Instruments to run your app in the Simulator using the different GPX files.

I would write out the entire process, but someone more eloquent than myself already has.

As an avid S/O user, I would be bereft to leave what is basically a single-link answer. So here is some extra, bonus information.

  1. You should definitely look into security testing your location aware features. I will be at Black Hat this year, so if you're there, let's talk about it!

  2. If you don't like the previously linked/sort of explained answer, you could use XCTest with code to simulate different locations (like this).

  3. It looks like there are also Apple Script solutions, like this one.

I hope I have at the very least provided enough information to qualify as more than just a lazy link-only answer. Enjoy, and good luck!

Community
  • 1
  • 1
Max von Hippel
  • 2,856
  • 3
  • 29
  • 46
0

idevicelocation is a command line tool to mock geolocation in ios devices.

Usage:
$ idevicelocation [OPTIONS] LATITUDE LONGITUDE

Set the location passing two arguments, latitude and longitude:

$ idevicelocation 48.856614 2.3522219000000177

Passing negative values :

$ idevicelocation 77.432332 -- -7.008373564

Stopping Location Simulation:

$ idevicelocation --stop

Options:

-d enable connection debug messages.<br/> -u specify device UDID.<br/> -h help.<br/> -s stop location simulation on device.

It uses libimobiledevice library to communicate with the process com.apple.dt.simulatelocation which is also used by Xcode internally for the same purpose.
Build instructions are there in the README file.
Make sure to mount the correct developer image before going random places.
Thanks to Angulo for writing this awesome utility.

This tool is not currently being shipped with libimobiledevice package although there's a Pull Request pending since long.

itsrishre
  • 103
  • 1
  • 2
  • 8