How to enable/disable app permission using Appium?
Asked Answered
M

7

6

My app need the multiple app permission. I need to check how my app behaves by allowing or denying the different permission. How can i enable/disable the app permission from the appium to create the multiple scenario?

For example lets say my app need to permissions: permission1 and permission2.

scenario 1 = allow permission1 allow permission2

scenario 2 = allow permission1 deny permission2

scenario 3 = deny permission1 allow permission2

scenario 4 = deny permission1 deny permission2
Mccray answered 6/9, 2018 at 5:3 Comment(9)
I think you can grant or allow permission by opening Settings app...Hearst
Is there any way to do it using appium?Mccray
As per my understanding appium does not have direct method to verify permissions, can u give more details what kind of permission you have to test means 2-3 examples of permission ?Hearst
I need to give camera and location permission to the appMccray
@Amit is there any way that i can open specific app setting using appium?Mccray
If you go to Settings > App > App under Test (e.g. Calculator) > App Info > Permission - This will list all permissions of app, from here you can grant or revoke any permission specific to appHearst
Let us continue this discussion in chat.Hearst
If you want to run adb command you can use adb shell pm grant <appPackageName> android.permission.CAMERA adb shell pm grant <appPackageName> android.permission.ACCESS_FINE_LOCATION Permissions can be found here androiddoc.qiniudn.com/preview/features/…Hearst
Thanks @Amit, i will try it.Mccray
M
0

I found the solution for android using adb shell command.

 String packageName= ((AndroidDriver) driver).getCurrentPackage();
 String grantCameraPermission= "adb shell pm grant " + packageName +" android.permission.CAMERA";
 String grantLocationPermission= "adb shell pm grant " + packageName +" android.permission.ACCESS_FINE_LOCATION";
 String revokeCameraPermission= "adb shell pm revoke " + packageName +" android.permission.CAMERA";
 String revokeLocationPermission= "adb shell pm revoke " + packageName +" android.permission.ACCESS_FINE_LOCATION";
        try {
            Runtime.getRuntime().exec(grantCameraPermission);
            Runtime.getRuntime().exec(revokeLocationPermission);

        } catch (IOException e) {
            e.printStackTrace();
        }

Here is the list of the Android Permission

Mccray answered 5/11, 2018 at 8:37 Comment(1)
Should think: String revokeCameraPermission= "adb shell pm revoke " + packageName +" android.permission.CAMERA"; String revokeLocationPermission= "adb shell pm revoke " + packageName +" android.permission.ACCESS_FINE_LOCATION";Roberge
L
7

The above answers didn't resolve my problem. I solved this problem by using the the caps of "autoGrantPermissions".

Setup:

desired_caps['autoGrantPermissions'] = True

You will have to disable the noReset caps to False, otherwise it won't work. See reference here: http://appium.io/docs/en/writing-running-appium/caps/

Lozier answered 4/1, 2019 at 0:0 Comment(2)
setting autoGrantPermissions=true will always grant the permission, I need to check the condition of not granting the permission i.e deny tjhe permission check app behaviourMccray
I made this but still the pop-up is displayed, even I cross checked the noReset is set to False only. What am I missing here?Cryptanalysis
M
3

For those who just started with appium and python, and are wondering how to add this capability, this is how my conftest.py file looks like:

My versions: Python 3.9 Appium: 1.21.0 Pytest: 6.2.4

capabilities = {
  "platformName": "Android",
  "platformVersion": "11.0",
  "deviceName": "emulator-5554",
  "automationName": "Appium",
  "app": r"C:\Project\app-universal-release.apk",
  **'autoGrantPermissions':'true'**
}

This works for me while I am developing a positive scenario test. This is not for negative testing. For that, the capabilities must not have the above.

The result:

All popup requirements(permissions) will be already be given.

Common ones are: Location, Folder, Camera etc

Miranda answered 27/7, 2021 at 12:37 Comment(0)
D
2

Before you launch/initialise the appium driver, just give all the required permissions to the respective app through adb command as below so that the ALLOW pop up won't be displayed:

        adb -s yourDeviceSerialNumber shell pm grant your.app.packagename android.permission.READ_EXTERNAL_STORAGE
        adb -s yourDeviceSerialNumber shell pm grant your.app.packagename android.permission.WRITE_EXTERNAL_STORAGE
        adb -s yourDeviceSerialNumber shell pm grant your.app.packagename android.permission.ACCESS_FINE_LOCATION
        adb -s yourDeviceSerialNumber shell pm grant your.app.packagename android.permission.CAMERA
        adb -s yourDeviceSerialNumber shell pm grant your.app.packagename android.permission.READ_CONTACTS

You can call these through your automation code for android

OR

There is appium capabilities to grant permission : autoGrantPermissions Appium automatically determine which permissions your app requires and grant them to the app on install. Defaults to false. If noReset is true, this capability doesn't work.

http://appium.io/docs/en/writing-running-appium/caps/

Domel answered 6/11, 2018 at 6:59 Comment(0)
I
1

One solution would be to go to Settings app and automate the scenario within that. You can go to Settings app (in fact, any iOS system app) by providing the bundle ID. For settings app it's com.apple.Preferences

I am using ruby, but the idea might be the similar for other clients.

def launch_settings_app
    @driver.execute_script('mobile: launchApp', {'bundleId': "com.apple.Preferences"});
end

You can find other bundle IDs here https://emm.how/t/ios-11-list-of-default-apps-and-bundle-id-s/465 Or using command line - this will return bundle ids for all installed apps on your device: ideviceinstaller -u device_udid -l

Interoceptor answered 6/9, 2018 at 13:30 Comment(0)
F
0

As per your needs you can't control the permission dialog from capabilities so you should use:

//for allow button
driver.findElement(By.id("com.android.packageinstaller:id/permission_allow_button")).click();
//or
driver.findElement(By.xpath("//*[@text='ALLOW']")).click();

//For deny button
driver.findElement(By.id("com.android.packageinstaller:id/permission_deny_button")).click();
//or
driver.findElement(By.xpath("//*[@text='DENY']")).click();
Fortissimo answered 13/9, 2018 at 11:34 Comment(1)
This not a solution as the permission won't be visible once you accept the permission. I found the solution to do it by using adb shell command.Mccray
C
0

You should invoke the app package and app capability properly... So when you are working with your app, then set the package and activity to your app, when the android activity comes, invoke the package and activity for native android...Also be sure to set the package and activity to your app after you are done with the permissions... Please find the code below:

public static void main(String[] args) throws MalformedURLException {
    // TODO Auto-generated method stub

    File f=new File("src");
    File fs=new File(f,"app-debug.apk");

    DesiredCapabilities cap=new DesiredCapabilities();
    cap.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
    cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());

    cap.setCapability("appPackage", "yourAppPackageName");
    cap.setCapability("appActivity", "yourAppActivityName");

    AndroidDriver<AndroidElement> driver=new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),cap);
    cap.setCapability("appPackage", "com.google.android.packageinstaller");
    cap.setCapability("appActivity", "com.android.packageinstaller.permission.ui.GrantPermissionsActivity");
    driver.findElementById("com.android.packageinstaller:id/permission_allow_button").click();
Conclude answered 21/10, 2018 at 9:27 Comment(0)
M
0

I found the solution for android using adb shell command.

 String packageName= ((AndroidDriver) driver).getCurrentPackage();
 String grantCameraPermission= "adb shell pm grant " + packageName +" android.permission.CAMERA";
 String grantLocationPermission= "adb shell pm grant " + packageName +" android.permission.ACCESS_FINE_LOCATION";
 String revokeCameraPermission= "adb shell pm revoke " + packageName +" android.permission.CAMERA";
 String revokeLocationPermission= "adb shell pm revoke " + packageName +" android.permission.ACCESS_FINE_LOCATION";
        try {
            Runtime.getRuntime().exec(grantCameraPermission);
            Runtime.getRuntime().exec(revokeLocationPermission);

        } catch (IOException e) {
            e.printStackTrace();
        }

Here is the list of the Android Permission

Mccray answered 5/11, 2018 at 8:37 Comment(1)
Should think: String revokeCameraPermission= "adb shell pm revoke " + packageName +" android.permission.CAMERA"; String revokeLocationPermission= "adb shell pm revoke " + packageName +" android.permission.ACCESS_FINE_LOCATION";Roberge

© 2022 - 2024 — McMap. All rights reserved.