Appium Automation for Android 10

SDVRY9UqrpjkgLWnx7rFHh

      As we know that Android 10 was released in September 2019.  There is one important change to be aware of from an automation perspective, however. If the app you are testing has a target SDK version of less than 23 (Android 6.0), then when you launch it on Android 10, you might see a new permissions interstitial before your app launches those related to contacts, location, storage, SMS, microphone and camera.

     This is because of Android 10’s new, more app permissions model. Because these older apps haven’t been coded with an eye to the new permissions model, Android needs to pop up this annoying interstitial in order to make sure everything’s cool, permissions-wise. This is a slight annoyance for users, with a great gain in terms of privacy. However, it can be damaging for automation if your code isn’t expecting it. You have three options for what to do about this.

Option 1: Automate the Permissions View

     The first option is the big hammer, you could just use Appium to automate this dialog by tapping the “Continue” button. But this means you have to throw a conditional in your test code for Android 10, and potentially waste a bunch of timing waiting for a “Continue” button that doesn’t exist on versions of Android less than 10. I used this kind of logic check but it wasted my automation execution time. So I don’t recommend it.

Option 2: Update your application’s targetSdkVersion

     If you have access to your applications’ source code, or can convince your developers to do this, you could always update your applications’ targetSdkVersion to 29 (the SDK version of Android 10), and this will do away with the interstitial. Though of course, you’ll need to participate in the new permissions system, and that might require some code updates throughout the app.

Option 3: Use autoGrantPermission

     The final option is most straightforward to use the autoGrantPermissions capability. If you include this capability and set it to true, Appium will attempt to automatically grant your app all permissions from the system perspective, so that when it comes time to launch your app, Android already thinks that all permissions have been requested and accepted. You can set the capability as follows:

capabilities.setCapability(“autoGrantPermissions “, true);

OR

capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, true);

     Using this capability is nice because you might need it already (as a way to do away with dialogs popping up throughout the execution of your test), but also because you don’t need to update the app in order to use it.

    Already Appium 1.15 supports Android 10, so make sure you download it whenever you need to automate apps on this Android platform. Don’t forget to use the last option. Following are the sample Desired Capability I used to Automation Android 10 devices,

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, “One Plus”);
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “10”);
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 180);
capabilities.setCapability(“udid”, “e6916f40”);
capabilities.setCapability(MobileCapabilityType.APP,
“C:\\Users\\abc\\Test.apk”);
capabilities.setCapability(MobileCapabilityType.NO_RESET, true);
capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, true);
driver = new AndroidDriver<WebElement>(new URL(“http://0.0.0.0:4723/wd/hub&#8221;), capabilities);

Reference: Appium Pro

make it perfect!