There is a great CLI tool by Lyft that can be used to change and setting location of the iOS simulator: set-simulator-location
But we should have booted simulator running so that the tool can set the location correctly.
Once you install and setup the CLI tool, just run a shell command like:
$ set-simulator-location -c 37.7765 -122.3918
and you will see the shell answers that the simulator location has changed to the required longitude and latitude.
The tool can set the location by two ways, as per their documentation:
- Set a specific latitude and longitude:
$ set-simulator-location -c 37.7765 -122.3918
2. Or using place search.
$ set-simulator-location -q Lyft HQ San Francisco
We can also specify which simulator we want to change the location to because this CLI tool will change to all simulators by doing the following:
$ set-simulator-location -c 37.7765 -122.3918 -s iPhone X
So how we use it through our UI Tests:
- We need to make sure that the location is set before our UI tests.
- We need to make sure that the simulator is booted so that the tool can work and behave correctly.
- We need to make sure that this is integrated to our CI.
So to enable this, please do the following (In your CI job of running UI tests):
First, we need to take a look here and use simctl which is a binary to interact with iOS simulators from command line by Apple as explained here: simctl, Control iOS Simulators from Command Line
- Add a task under the CI job with Shell executable.
- Add the following code to the shell script that would do three things:
- Boot a simulator
- Open the simulator
- Set the location
- Close the simulator
xcrun simctl boot 'iPhone 8'
open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app
set-simulator-location -c 37.7765 -122.3918
xcrun simctl shutdown 'iPhone 8'
3. Then call your UI tests task in the CI job to run them using the mocked location set.
This is a way only to mock simulator location before running the UI tests.