Testcomplete and the new permission system in Android
Hey guys,
as you might know as an Android developer, the new permission system introduced with Android M is awsome from the users point of view but a real pain in the bum for devs and automated qa.
So our app requests the permission to use the devices GPS location at startup which causes Android to ask the user for permission (See my screenshot). As this popup is not part of our package, TestComplete has no direct access to its components, so something like
Aliases.Device.permissionDialog.ButtonAllow.Click()
will not work.
Right now, we use imagebased recognition but this is clearly not what we want as we need to take photos of this allow button in every possible way it can show up.
After doing some research, we found out that Googles UIAutomator can do the job but we didn't find a way to access its capabilities from TestComplete.
So is there any way that we could solve our issue without imagebased recognition (or can we somehow use UIAutomator in TestComplete)?
Regards,
vatbub
Heyho,
I just wanted to post the solution we found here so that anybody can look it up.
The problem was that our app already requests the GPS permission at startup so there is no way we can grant the permission after launching the app. On the other hand it makes no sense to grant the permission before installing the app. The solution is to split up the process of installing and launching the app on the phone:
Instead of using
AndroidTestedApp.Run()
which installs and launches the app,
we now use the following:
// Install the app var packObj = Mobile.Device.PackageManager.GetPackageFromAPK(AndroidTestedApp.APKFileName); Mobile.Device.PackageManager.RemovePackage(packObj); if (!Mobile.Device.PackageManager.InstallPackage(packObj)){ Log.Error("Something went wrong while installing the application."); } // Allow all permissions if necessary var oShell = new ActiveXObject("WScript.Shell"); var grantFineGPSCommand = "adb shell pm grant " + packageName + " android.permission.ACCESS_FINE_LOCATION"; try { Log.Message("Granting fine GPS access for app " + packageName, "Using command: " + grantFineGPSCommand); oShell.Run(grantFineGPSCommand); } catch (e) { Log.Warning(e); } // Launch the application if (!Mobile.Device.PackageManager.LaunchPackage(packObj)){ Log.Error("Something went wrong while launching the application."); return; }
As you can see, this allows us to put the adb commands in between installing and launching the app and no popups appear.
Greetings,
vatbub