Forum Discussion

vatbub's avatar
vatbub
Contributor
9 years ago

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

  • vatbub's avatar
    vatbub
    9 years ago

    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

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    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-espresso-tests) :

    > ${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.

    • vatbub's avatar
      vatbub
      Contributor

      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

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        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.

    • vatbub's avatar
      vatbub
      Contributor

      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

  • 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