Appium to get Android device system locale properties

By running adb shell getprop command, you can get device properties, including device system language/country or locale information depends on the version of the Android OS.

You can do the same thing(running adb shell command) from within Appium test script, which may help you in case you cannot make a direct adb connection from your test script to the target device.

adb shell getprop

By using adb shell command, you can get device system properties, like below,

$ adb shell getprop

[DEVICE_PROVISIONED]: [1]
[af.fast_track_multiplier]: [1]
[audio.deep_buffer.media]: [true]
...
[persist.sys.locale]: [en-US]                 <----- system locale info
...
[ro.build.version.release]: [6.0]
...
[ro.build.version.sdk]: [23]
...
[ro.product.locale]: [ja-JP]                  <----- product locale info (product static)
[ro.product.manufacturer]: [LGE]
[ro.product.model]: [LGV32]
[ro.product.name]: [p1_kddi_jp]
...
[ro.product.model]: [LGV32]
...


$ adb shell getprop ro.product.locale
ja-JP

From Appium

Appium itself does not provide an API to do the same thing, but it instead, provides more generic API, `execute`. You can run mobile specific commands by using it.

You can run adb shell commands, as shown below,

const wdio = require("webdriverio");

const opts = {
    port: 4723,
    desiredCapabilities: {
        platformName: "Android",
        platformVersion: "9",
        deviceName: "PH-1",
        app: "/path/to/apk/app-debug.apk",
        automationName: "UiAutomator2"
    }
};

const client = wdio.remote(opts);

client.init().execute('mobile: shell',  {command: 'getprop persist.sys.locale'}).then( res => console.log(res.value));

Note

  • The appium server needs to be run with `–relaxed-security` option set.
    This means that if you have no control over appium server(e.g. if you are using a public remote device provider), the above method may not work, so you should check if this works on your setup and device provider.
  • The property name, may be different based on the Android OS version(s).
    • Older versions of OS(4.x?)
      • persist.sys.country
      • persist.sys.language
    • Newer versions of OS(5.0+?)
      • persis.sys.locale

Using adb wrapper

You can run adb command(s) by using an adb wrapper library, like below.

const appuimADB = require('appium-adb')
const  {ADB} = appuimADB
let adb = new ADB();

adb.shell('getprop persist.sys.locale').then(res => (console.log(res)));

*) Please note that this works only in the setup where adb commands directly works, so this may not work on a device provided by a public remote device farm.

Setting locale from adb

You basically cannot do it, unless you do it against rooted device or avd.

Per the SO answer, the following adb command should do the job.

https://stackoverflow.com/questions/21712205/change-device-language-via-adb

adb shell
root@generic:/ # getprop persist.sys.language
getprop persist.sys.language
en
root@generic:/ # setprop persist.sys.language fr
setprop persist.sys.language fr
root@generic:/ # setprop persist.sys.country CA
setprop persist.sys.country CA
root@generic:/ # stop
stop
root@generic:/ # start
start
root@generic:/ # sleep 5
sleep 5
root@generic:/ # getprop |grep lang
getprop |grep lang
[persist.sys.language]: [fr]
root@generic:/ # getprop |grep country
getprop |grep country
[persist.sys.country]: [CA]
root@generic:/ #

Setting from Application

It’s not using adb, but Android app with an appropriate permission can change the language/locale setting of the device(until Android 5.0).

https://stackoverflow.com/questions/2900023/change-app-language-programmatically-in-android/4683532#4683532

From Android 4.2 and before 5.0, you also need to give your app the following permission.

$ adb shell pm grant com.yourapp.packagename android.permission.CHANGE_CONFIGURATION

Reference

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s