Code et cetera

android

While testing your app, its common that you want to test it under different locales as part of your automation strategy.

Be it on real devices or emulators this used to be quite simple to do before Android API 29

Simply running this adb command would let you change the locale easily:

adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE en_US com.android.customlocale2

The reason this worked, is because emulators were shipped with the CustomLocale app, which could be sent the previous command and it would set the desired locale for you. But this app is no longer provided and is incompatible with Android versions past 8.

After spending hours looking everywhere, I couldn't find a similar solution, but it seems that Appium manages to change the device locale.

How does Appium do it?

We don't use Appium as part of our automation strategy, we use Calabash, which certainly has fallen behind in features, but we don't want to change the whole infrastructure right now. So the question was, how does Appium achieve this? And can we benefit from it?

They use Appium Settings for this end. This app, like Custom Locale used to do, lets you directly interact with certain system settings, like enabling / disabling wifi, reading received notifications, or changing the device locale.

So then all you need to do is install Appium Settings in the device under test and run these adb commands:

First we enable changing system settings though Appium Settings

adb shell pm grant io.appium.settings android.permission.CHANGE_CONFIGURATION

Then we can change the desired country and language

adb shell am broadcast -a io.appium.settings.locale -n io.appium.settings/.receivers.LocaleSettingReceiver --es lang en --es country US

You can install the app in every test or a more optimal way could be, if you use docker, to perform this when you create your emulator docker images so they already come installed with Appium Settings.

Hope this helps!

#appium #automation #android

The search button won't work!

I've encountered this problem where you won't be able to proceed with a search while automating some Android apps with Appium, and I've found some solutions that have worked in some cases and others in others. So I'm gonna lay them down here, they may be helpful for anyone finding himself with this problem.

First case, this is the standard solution. Here you simply input the text and send the Enter key:

driver.click(searchField);
driver.sendKeys(textValue);
driver.pressKey(new KeyEvent(AndroidKey.ENTER));

Second case. Here we append a line break at the end of the text we want to search. In most cases this will trigger the search. I find this to be simple and working most of the times:

driver.click(searchField);
driver.sendKeys(textValue + "\n");

Third case. The most rare one, is when the app specifically waits for a special kind of event, the EditorAction

driver.click(searchField);
driver.sendKeys(textValue);
driver.executeScript("mobile:performEditorAction", ImmutableMap.of("action", "search"));

I hope this can be helpful to anyone facing this issue. If I find another way in the future I will add it to the list.

#appium #automation #android