Skip to content

Instantly share code, notes, and snippets.

@dpgraham
Created December 23, 2019 22:38
Show Gist options
  • Save dpgraham/048c2a5554a6822c4e5c7cf0d0a71372 to your computer and use it in GitHub Desktop.
Save dpgraham/048c2a5554a6822c4e5c7cf0d0a71372 to your computer and use it in GitHub Desktop.
setLocation workaround for Appium Android

Versions of Appium above v1.9.1 and below v1.16.0 have a bug in setLocation and getLocation that looks like this:

2019-12-23 21:57:26:835 - [HTTP] --> GET /wd/hub/session/372d6e4d-279a-4582-ab6d-90804845bcb0/location
2019-12-23 21:57:26:835 - [HTTP] {}
2019-12-23 21:57:26:836 - [debug] [MJSONWP (372d6e4d)] Calling AppiumDriver.getGeoLocation() with args: ["372d6e4d-279a-4582-ab6d-90804845bcb0"]
2019-12-23 21:57:26:837 - [debug] [ADB] Running '/home/chef/android-sdk-linux/platform-tools/adb -P 5037 -s emulator-5554 shell am broadcast -n io.appium.settings/.receivers.LocationInfoReceiver -a io.appium.settings.location'
2019-12-23 21:57:26:924 - [debug] [MJSONWP (372d6e4d)] Encountered internal error running command: Error: Cannot parse the actual location values from the command output: Broadcasting: Intent { act=io.appium.settings.location flg=0x400000 cmp=io.appium.settings/.receivers.LocationInfoReceiver }
2019-12-23 21:57:26:924 - [debug] [MJSONWP (372d6e4d)] Broadcast completed: result=0, data=""
2019-12-23 21:57:26:924 - [debug] [MJSONWP (372d6e4d)]     at ADB.getGeoLocation (/home/chef/appium/appium-v1.15.0/appium-v1.15.0/node_modules/appium-android-driver/node_modules/appium-adb/lib/tools/adb-commands.js:968:11)
2019-12-23 21:57:26:926 - [HTTP] <-- GET /wd/hub/session/372d6e4d-279a-4582-ab6d-90804845bcb0/location 500 91 ms - 402

The workaround for this is to add this snippet of code:

def updateSettingsApp():
    # Get rid of the broken io.appium.settings apk
    driver.remove_app('io.appium.settings');
    
    # Install the patched io.appium.settings apk
    driver.install_app('https://github.com/appium/io.appium.settings/releases/download/v2.16.2/settings_apk-debug.apk')
    
    # Set the permissions on the app to allow location services
    driver.execute_script('mobile: shell', {
        'command': 'pm',
        'args': ['grant', 'io.appium.settings', 'android.permission.ACCESS_COARSE_LOCATION'],
        'includeStderr': True,
        'timeout': 5000
    })
    driver.execute_script('mobile: shell', {
        'command': 'pm',
        'args': ['grant', 'io.appium.settings', 'android.permission.ACCESS_FINE_LOCATION'],
        'includeStderr': True,
        'timeout': 5000
    })

And call updateSettingsApp after session is initialized (and before any call to getLocation or setLocation).

The snippet is written in Python but can be adapted to any other Appium client languages. Just look for documentation for:

This workaround is unnecessary for Appium 1.16.0 and above as it will already have the patched settings app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment