Setting Geo Location on iOS Device Using Appium

Setting Geo Location on iOS Device Using Appium

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When working on mobile automation with Appium, setting the geo-location for testing purposes can sometimes be challenging, especially on iOS devices. This guide will walk you through the process of setting geo-location on an iOS simulator using Appium.

Understanding the Challenge

In iOS automation, the ability to set geo-location directly through Appium commands, as you might do with Android, is not straightforward. This limitation arises because Apple does not provide an API for the XCTest framework to simulate GPS location. Consequently, methods that work seamlessly on Android often result in exceptions on iOS.

Proposed Solution

The most effective approach to set geo-location on an iOS simulator involves using AppleScript. Below is a detailed method to achieve this:

AppleScript Method

The following AppleScript can be used to set a custom geo-location on an iOS simulator. This script automates the process of navigating through the simulator’s menu to set the location:


public static void setLocation(Location loc) {
    try {
        String[] cmd = {"osascript", "-e",
                "on menu_click(mList)\\n" +
                        "    local appName, topMenu, r\\n" +
                        "    if mList's length  1 then set r to (items 2 through (mList's length) of mList)\\n" +
                        "    tell application \\\"System Events\\\"\\n" +
                        "        if mList's length is 1 then\\n" +
                        "            click parentObject's menu item f\\n" +
                        "        else\\n" +
                        "            my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))\\n" +
                        "        end if\\n" +
                        "    end tell\\n" +
                        "end menu_click_recurse\\n" +
                        "application \\\"" + simulatorAppName() + "\\\" activate\\n" +
                        "delay 0.2\\n" +
                        "menu_click({\\\"" + simulatorAppName() + "\\\", \\\"Debug\\\", \\\"Location\\\", \\\"None\\\"})\\n" +
                        "delay 0.2\\n" +
                        "menu_click({\\\"" + simulatorAppName() + "\\\", \\\"Debug\\\", \\\"Location\\\", \\\"Custom Location…\\\"})\\n" +
                        "delay 0.2\\n" +
                        "tell application \\\"System Events\\\"\\n" +
                        "    tell process \\\"" + simulatorAppName() + "\\\"\\n" +
                        "        set value of text field 1 of window \\\"Custom Location\\\" to \\\"" + loc.getLatitude() + "\\\"\\n" +
                        "        set value of text field 2 of window \\\"Custom Location\\\" to \\\"" + loc.getLongitude() + "\\\"\\n" +
                        "        click UI Element \\\"OK\\\" of window \\\"Custom Location\\\"\\n" +
                        "    end tell\\n" +
                        "end tell"
        };
        Process process = Runtime.getRuntime().exec(cmd);
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getErrorStream()));
        String lsString;
        while ((lsString = bufferedReader.readLine()) != null) {
            System.out.println(lsString);
        }
        try { Thread.sleep(10000); } catch (Exception e1) {}
    } catch (Exception e) {}
}

public static String simulatorAppName() {
    return "Simulator";
}

Advantages of Using This Method

  • Automates the setting of geo-location without manual intervention.
  • Works effectively with iOS simulators where direct API methods are unavailable.
  • Utilizes AppleScript to achieve the desired functionality seamlessly.

Integrating with Your Testing Framework

Integrating this AppleScript method into your existing testing framework can enhance your testing capabilities by allowing you to simulate different geo-locations. This is particularly useful for applications that rely heavily on location-based services.

Enhancing Automation with Repeato

While Appium is a powerful tool for mobile test automation, it can sometimes be slow and the tests can be unstable. This is where Repeato comes into play. Repeato is a no-code test automation tool designed for iOS and Android applications. It leverages computer vision and AI to create, run, and maintain automated tests quickly and efficiently.

Repeato stands out with its speed and reliability, making it an excellent alternative for scenarios where Appium might fall short. By using Repeato, you can ensure that your automated tests run faster and with greater stability.

For more information on how to get started with Repeato, visit our documentation page.

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