May I specify the current location for testing purposes? - ios

For testing purposes, is it possible to specify a coordination as the default current location. As the app runs in the simulator, it returns the specified location when the app needs current location.

You can of course do this through the simulator:
Debug -> Location -> custom location

It's also possible to use GPX files. These are small XML-based files that have longitude and latitude information. This way you can create some easy to use favorite locations, which you can trigger from the debug area (from the "Simulate Location" compass icon) or from Product > Debug > Simulate Location. You can set any of them as your default location. Very handy! Also works as an override for attached devices.
I use http://www.gpx-poi.com to create them, but I'm sure you do it in other ways too.

Related

SImulating location with gpx file on expo app using iOS simulator

I would like to input a gpx file into the iOS simulator to simulate driving along a route. However, in order to do that, I need to add a gpx file to my xcode project. The project I'm using was made using expo, so I cannot open it in xcode. Is there any workaround to be able to simulate the route without opening in xcode?
Simulating location can be done at the simulator level via tools like set-simulator-location
Just install it with brew install lyft/formulae/set-simulator-location
Then set the simulator location: set-simulator-location -q Expo 624 University Ave Palo Alto CA (sets it to the Expo office).
I ended up solving my issue by using set-simulator-location-trip-simulator, a tool built using set-simulator-location. It allows you to select a start and end destination, and simulate a trip from the start to the end. It doesn't allow the use of a gpx file, however.
Since Xcode 14 simctl offers the possibility to set the location of a simulator:
$ xcrun simctl location
Control a device's simulated location
Usage: simctl location <device> <action> [arguments]
list
List available simulation scenarios.
clear
Stop any running scenario and clear any simulated location.
set <lat1>,<lon1>
Set the location to a specific latitude and longitude.
run <scenario>
Run a simulated location scenario (use the list action to get a list of scenarios).
start [--speed=<meters/sec>] [--distance=<meters per second>|--interval=<seconds>] <lat1>,<lon1> <latN>,<lonN>...
Set the location to a series of waypoints specified as 'lat,lon' pairs, interpolating between them over time.
At least two waypoints are required. Use '-' to read waypoints from stdin, one waypoint per line.
Speed specifies how quickly to move between waypoints in meters per second. If not specified 20m/s is used.
The system will issue location updates along the path between each pair of waypoints. Use distance or interval to
control how often those updates are issued. Distance will issue an update every <meters> travelled without regard
for the time between updates. Interval will issue updates at fixed times without regard for how much
the location moves between updates.
If neither are specified an interval of 1.0 seconds is used. If both are specified the system picks ones.
Example simulating a direct line between San Francisco and New York City, with updates every km:
set --distance=1000 --speed=260 37.629538,-122.395733 40.628083,-73.768254
Latitude and longitude pairs must be specified using '.' as the decimal separator and ',' as the field separator.
To use it with a gpx file, you will need some kind of a wrapper to generate a command like so (note N coordinates are possible):
xcrun simctl location "iPhone 14 Pro" start 37.629538,-122.395733 40.628083,-73.768254
From my research, this offers much more functionality than all the other tools I have found and is superior to lyft/formulae/set-simulator-location.
If you like to use an UI, LocationSimulator is very nice, it also works for real devices!

Is it possible to set a location automatically for xcode UI testing?

I am currently writing tests for my app on xcode, and was wondering if there is a line of code I could use that would run my tests using an pre determined location service? I know it is possible to do it manually by clicking on the arrow when the test is running, but i want it automatically? I cant find any documentation or similar questions online.
Anyone know of anything?
A quick solution to this is going on simulator > Debug > Location > Custom Location > Then add the Lat and long in.

Automating Xcode' Debug > Simulate Location command

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?
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.
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
There is a GitHub project called Pokemon-Go-Controller that does exactly what you want.
Overview of the solution:
Create a gpx file
Create a blank project referencing (not copying) that gpx file and run it on your device
Run auto clicker which will constantly click update location in Xcode
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
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.
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!
If you don't like the previously linked/sort of explained answer, you could use XCTest with code to simulate different locations (like this).
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!
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.

iOS UI-Test simulate (changing) Location

I have some kind of navigation app where I handle different locations. Is it possible in UI tests to simulate and even to change them at runtime? What would be the best approach to archive this?
I think the best approach to this will be to use GPX files. These allow you to set the lat and long of a location. (For more details see here: https://blackpixel.com/writing/2013/05/simulating-locations-with-xcode.html)
However, I suspect you will need to call these from within the app itself and then use UI testing launch arguments (when you launch XCUIApplication) to stipulate which GPX files to use depending upon the test.
See my detailed note https://github.com/onmyway133/blog/issues/45
You need to declare the gpx file in the app target, not the UI Tests target
Go to Simulator -> Location -> Custom Location to point to your location in the gpx. It is duplication, but without it, it does not work
If it does not work because of caching or something, run the app again, then run UI Tests again.
If it still does not work, reset simulator
You can create gpx file directly from Xcode

Testing a location based iOS application using KIF

I want to write tests for an iOS app that are location dependent, like if you are at place A (I mean geo-location) this testStep should fail and that one should succeed and in location B the situation is the other way around,
Is there a way to simulate being at a location for KIF tests, like defining a stepForBeingAtLocation , for the simulator obviously, and not by manually choosing a gpx file for each test separately,
EDIT: to make it more clear, I want to do it in my tests, so it should be done in my code or with calling an external script or in some automatic way, I want to run tests that should happen in different locations.
The iOS Simulator is able to simulate different locations.
Select the iOS Simulator app, and from its OS X menu bar, look under Debug -> Location.

Resources