cancel
Showing results for 
Search instead for 
Did you mean: 

Testcomplete and the new permission system in Android

SOLVED
Highlighted
Contributor

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

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Contributor

Re: Testcomplete and the new permission system in Android

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

View solution in original post

6 REPLIES 6
Highlighted
Community Hero

Re: Testcomplete and the new permission system in Android

Hi,

 

I am not an expert in this area yet, so the below is a kind of wild guess, but following your links I found this (http://stackoverflow.com/questions/32787234/how-to-manage-runtime-permissions-android-marshmallow-es...) :

> ${adb} -s ${device} shell pm grant ${applicationId} android.permission.ACCESS_FINE_LOCATION".execute()

 

which made me think that it might help if the command like above is executed from test code (via the Device.ShellExecute() method) either before tested application start or before requesting GPS access permission.

 

Another idea is to compile the code for UIAutomator into some executable form (not sure whether this is possible or not) and, again, call it from your test code.

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
Highlighted
Contributor

Re: Testcomplete and the new permission system in Android

Hey Alex,

I just had a different idea, it's just an idea, I didn't try it out yet:

I could create a Unit-Testing-/Selenium-Testing-Item in Testcomplete and then call UIAutomator from there.

Does that even make sense?

Greetings,

vatbub

Highlighted
Contributor

Re: Testcomplete and the new permission system in Android

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

View solution in original post

Highlighted
Community Hero

Re: Testcomplete and the new permission system in Android

Hi,

 

Great! Thank you for the update. This is a nice workaround for the case when permission is requested on application start and there is no option to grant it after the application started.

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
Highlighted
Contributor

Re: Testcomplete and the new permission system in Android

Hello,

We are facing the same problem. I see the workaround (thanks!) but I'd prefer to not have to go that route. It would make much more sense (and be easier on all of us!) for TestComplete to add some "Run Mode Parameters" to the Tested Application to allow permission requests or make it a default project setting.  Not sure the best implementation route but I figure it would be easier for SmartBear to figure it out than for myself to code a workaround.  🙂  Thanks for listening.

~Chris

Highlighted
Community Hero

Re: Testcomplete and the new permission system in Android

@culisse:

 

Hi Chris,

 

You may create a feature request in this section of the forum

http://community.smartbear.com/t5/TestComplete-Feature-Requests/idb-p/TestXCompleteFeatureRequests that is more closely monitored by SmartBear and reference this thread.

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
New Here?
Join us and watch the welcome video:
Announcements
Join TechCorner Challenge!
Want a fun and easy way to learn TestComplete? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Compare images using the Region Checkpoint

See replies!

Converting UTC TimeDate in an Excel file

See replies!

Compare HTML table with Excel file and correct data in Excel file

Participate!

How to execute remote test and obtain results via Test Runner REST API

Participate!
Top Kudoed Authors