Search code examples
selenium-chromedriverappiumappium-androidsaucelabs

Permission issue for appium chrome borwser


I am implementing an appium test on remote android driver, with chrome browser for loading urls. Some of the Urls are pdfs, and chrome asks to store those files. and appears that chrome doesnt have access to filesystem to store those files, which results in a dialog like below.

Please help me pass that dialog without any manual inputs.

File permission dialog

Upon clicking continue, it will load actual permissions dialog from Android.

Here is my code initialize appium capabilities

DesiredCapabilities caps = DesiredCapabilities.android();
    caps.setCapability("appiumVersion", "1.9.1");
    caps.setCapability("deviceName","Samsung Galaxy S9 Plus HD GoogleAPI Emulator");
    caps.setCapability("deviceOrientation", "portrait");
    caps.setCapability("browserName", "Chrome");
    caps.setCapability("platformVersion", "8.1");
    caps.setCapability("platformName","Android");
    caps.setCapability("autoAcceptAlerts", true);
    caps.setCapability("autoGrantPermissions", true);
    caps.setCapability("chromedriverArgs", "--allow-file-access-from-files");
    caps.setCapability("maxDuration", 10000);

and this is the snippet I use to load a Url

        driver.navigate().to("http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf");

autoGrantPermission also doesnt work in this case because chrome is already installed. Appium team has already rejected this issue - https://github.com/appium/appium/issues/10008

Please help!


Solution

  • Indeed I had very hard time finding out the solution, but eventually I found a workaround.

    • The best workaround would have been reinstalling the chrome package. I tried that, but I could not start chrome after reinstalling it, as I had no access to shell, and chromedriver complained. So I left that track.
    • I tried getting hold of adb command or mobile:changePermissions but for that you need to use server flag --relaxed-security while starting the server, and saucelabs doesnt provide any handy interface to start the server with this flag.

    The last resort, I found a solution here - https://stackoverflow.com/a/51241899/4675277 . But just that was not sufficient, because it helped me fix chrome alert, but later on it popped up with another alert with allow and deny, for which another solution in the same question helped me. So this is the code I eventually used -

    driver.navigate().to("http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf");
        String webContext = ((AndroidDriver)driver).getContext();
        Set<String> contexts = ((AndroidDriver)driver).getContextHandles();
        for (String context: contexts){
            if (context.contains("NATIVE_APP")){
                ((AndroidDriver)driver).context(context);
                break;
            }
        }
        driver.findElement(By.id("android:id/button1")).click();
        contexts = ((AndroidDriver)driver).getContextHandles();
        for (String context: contexts){
            if (context.contains("NATIVE_APP")){
                ((AndroidDriver)driver).context(context);
                break;
            }
        }
        driver.findElement(By.id("com.android.packageinstaller:id/permission_allow_button")).click();
        ((AndroidDriver)driver).context(webContext);
    

    This helps allow all permissions required.