Permissions error trying to run Appium test on Android



  • I am not able to run my automation testing with Appium server using Eclipse with Selenium. The error I get is:

    Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to create a new remote session. Please check the server log for more details. 
    java.lang.SecurityException: You need the android.permission.INSTALL_GRANT_RUNTIME_PERMISSIONS permission to use the PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS
    

    The code I am trying to run is:

    package tests;
    
    import java.net.MalformedURLException;
    
    import java.net.URL;
    
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    import io.appium.java_client.AppiumDriver;
    
    import io.appium.java_client.MobileElement;
    
    import io.appium.java_client.android.AndroidDriver;
    
    public class StartChrome {
    
        public static void main(String[] args) {
    
            // Set the Desired Capabilities
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.setCapability("deviceName", "My Phone");
            caps.setCapability("udid", "7970dc54"); // Give Device ID of your mobile phone
            caps.setCapability("platformName", "Android");
            caps.setCapability("platformVersion", "9 PKQ1.180904.001");
            caps.setCapability("appPackage", "com.android.vending");
            caps.setCapability("appActivity", "com.google.android.finsky.activities.MainActivity");
            caps.setCapability("noReset", "true");
    
            // Instantiate Appium Driver
            try {
    
                AppiumDriver<MobileElement> Driver = new AndroidDriver<MobileElement>(
                        new URL("http://0.0.0.0:4723/wd/hub"), caps);
    
            } catch (MalformedURLException e) {
                System.out.println(e.getMessage());
            }
        }
    
    }
    


  • I suspect your problem lies with the errors:

    java.lang.SecurityException: You need the android.permission.INSTALL_GRANT_RUNTIME_PERMISSIONS permission to use the PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS

    This is saying that the phone does not have permission to run your software.

    Because your code is setting the capability "noReset" to true, you can't set the capability "autoGrantPermissions" to true because it won't work. The two capability settings are incompatible.

    This limits your options.

    You could remove the "noReset" capability, if it is not essential that your device keeps the app running and the app data in local memory. That would then allow you to set "autoGrantPermissions" to 'true' and probably fix your problem. (http://appium.io/docs/en/writing-running-appium/other/reset-strategies/index.html has more information)

    You could specifically install the permissions using adb - there is a Stack Overflow question here with more information. You should do this with your code, so you don't leave the device with an open security flaw.

    You could probably also use the Java Appium API call from the package io.appium.java_client.android.appmanagement and the class AndroidInstallApplicationOptions myInstallOptions.withGrantPermissionsEnabled() which will grant all required permissions as soon as your code is installed.

    I'd also suggest you take some time to review the Appium documentation.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2