Setting the Mock GPS Location for Mobile App Testing

location based testing - map with pin

19 September 2023 Stephan Petzl Leave a comment Manual testing, QA, Test automation

Whether it’s a fitness app tracking your morning run, a shopping platform offering localized deals, or a travel app suggesting nearby attractions: the ability to accurately determine and utilize a user’s geographical location is paramount. But how do QA teams test if these features work seamlessly across the globe? Enter the world of mock locations.

Setting mock locations allows developers to simulate a device’s location, offering a powerful tool to test location-based features without physically moving. This capability is invaluable, especially when considering the vast array of scenarios an app might encounter in the real world. Whether you’re a seasoned developer, a QA professional, or just a tech enthusiast curious about the behind-the-scenes of app development: understanding how to set mock locations is a valuable skill that can extensively contribute to your mobile app testing toolkit.

In this article, we’ll delve deep into the methodologies of setting mock locations on two of the most popular mobile platforms: iOS and Android.

Setting Device Location for iOS Testing

When it comes to iOS app development, simulating different locations can be crucial for testing various features of your app, especially those that rely on geolocation services. Apple provides a powerful tool for this purpose: the xcrun simctl location command. This command allows developers to control a device’s simulated location, making it an invaluable asset for automated testing.

Basic Usage

The basic structure of the command is:

simctl location <device> <action> [arguments]

Here’s a breakdown of its functionalities:

List Available Scenarios

simctl location <device> list

This command lists all available simulation scenarios. It’s useful to see predefined location scenarios that you can run.

Clear Simulated Location

simctl location <device> clear

If you want to stop any running scenario and clear any simulated location, this command comes in handy.

Set a Specific Location

simctl location <device> set <lat>,<lon>

This allows you to set the device’s location to a specific latitude and longitude. For instance, to set the location to the coordinates of Paris, you would use:

simctl location <device> set 48.8566,2.3522

Run a Simulated Location Scenario

simctl location <device> run <scenario>

After listing available scenarios with the list action, you can run a specific scenario using this command.

Simulate Movement Between Waypoints

simctl location <device> start [--speed=<meters/sec>] [--distance=<meters>|--interval=<seconds>] <lat1>,<lon1> <latN>,<lonN>...

This is a more advanced feature, allowing you to simulate movement between multiple waypoints. You can specify the speed of movement, the distance between location updates, or the interval for updates. For example, to simulate a direct line between San Francisco and New York City with updates every kilometer, you’d use:

simctl location <device> start --distance=1000 --speed=260 37.629538,-122.395733 40.628083,-73.768254

Formatting Tips

When specifying latitude and longitude pairs:

Use ‘.’ as the decimal separator. Use ‘,’ as the field separator.

Setting Device Location for Android Testing

For Android app testing, simulating different locations is essential, especially for apps that rely on geolocation services. Appium provides a tool for this purpose: the Appium Settings app. This app allows testers and developers to toggle various settings on an Android device or emulator, including setting mock locations.

Building and Installing the Appium Settings App

Before you can use the Appium Settings app, you need to build and install it on your Android device or emulator.

Requirements:

Optionally, you can use Android Studio for easier debugging.

Building:

To build the app, execute the following commands:

$ ./gradlew clean assembleDebug

You can also run gradlew installDebug to build and immediately deploy the app to a connected Android device or emulator.

Installing:

Install the APK using the Android Debug Bridge (ADB) with the following commands:

$ cd app/build/outputs/apk
$ adb install settings_apk-debug.apk

To uninstall the app:

$ adb uninstall io.appium.settings

Setting Mock Locations

Once the Appium Settings app is installed, you can set mock locations using the following steps:

    1. Ensure that location mocking is enabled on the device:
        • On Android 5: Enable the “Allow mock locations” option in Developer Settings.
        • On later Android versions, use the following command:
      $ adb shell appops set io.appium.settings android:mock_location allow
    2. Start sending scheduled updates for mock location:
        • For API versions 26 and above:
      $ adb shell am start-foreground-service --user 0 -n io.appium.settings/.LocationService --es longitude {longitude-value} --es latitude {latitude-value}
        • For older versions:
      $ adb shell am startservice --user 0 -n io.appium.settings/.LocationService --es longitude {longitude-value} --es latitude {latitude-value}
    3. To stop sending mock locations and clean up, execute:
$ adb shell am stopservice io.appium.settings/.LocationService

Like this article? there’s more where that came from.