Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appium - File upload to web using Chrome - ERR_ACCESS_DENIED

I am trying to upload a file to https://the-internet.herokuapp.com/upload via Chrome using Appium. But it leads to ERR_ACCESS_DENIED error all the time.

The file resides in the Download folder of my device.

Refer the image below:

enter image description here

I have tried the following capabilities as well with different combinations, but it didn't help: noReset, autoGrantPermissions, fastReset.

My Script:


from appium import webdriver
import time

def execute_script():
  driver = webdriver.Remote(
      command_executor='http://0.0.0.0:4723/wd/hub',
      desired_capabilities={
          "platform": "android",
          "platformName": "android",
          "platformVersion": "10",
          "deviceName": "<xxxx>",
          "udid": "<xxxx>",
          "browserName": "chrome",
          "automationName": "UIAutomator2",
          "chromeOptions": {
              "w3c": False
          },
          # "autoGrantPermissions": True,
          # "noReset": True,
          # "fastReset": True,
          # "fullReset": False
      }
  )

  driver.get('https://the-internet.herokuapp.com/upload')

  up = driver.find_element_by_id("file-upload")
  up.send_keys("/sdcard/Download/file.pdf")
  driver.find_element_by_id("file-submit").click()

  driver.quit()

driver = execute_script()

The script executes fine till the send_keys step. But as soon as the file-submit click is performed, it leads to the mentioned error. I tried it on https://fileconvoy.com/ as well and it results in the same error.

Version Details:

  • Appium Version: 1.17.0 (Tried version from 1.10.x to 1.20.x)
  • Device: Samsung Galaxy S9, Android 10
  • Chromedriver: 90.0.4430.24 (Tried for Chrome 81 as well, same error).

What all I have tried:

  • Granted permissions via the following command explicitly before starting appium (and also midway of the script in case the permissions get reset), but no luck.
adb -P 5037 shell 'pm grant com.android.chrome android.permission.READ_EXTERNAL_STORAGE  android.permission.WRITE_EXTERNAL_STORAGE'
  • Instead of using browserName: "Chrome", I have also tried specifying appPackage and appActivity to begin chrome. But no luck.

Is there anything that I am fundamentally missing? This works fine on desktop browsers (using selenium). But I haven't been able to run it once on my mobile device.

Let me know if there is anything else that I should share.

Have created an issue on Appium as well: https://github.com/appium/appium/issues/15293

like image 392
Rohan Chougule Avatar asked Apr 22 '21 13:04

Rohan Chougule


1 Answers

It turns out, Chrome can't access /sdcard/Download/ directory during Automation.

Trying to upload a file from /data/local/tmp/ directory (by pushing a file there first) worked.

Secondly, there is a slight difference between Android 9 and Android 10+ considering the file download and upload scenarios for the path /sdcard/Download/ (yet to come up with a reasoning for this):

  1. In case of file download:
  • Android 10 and above don't ask for any permission (i.e. no dialog box pops up)
  • Android 9 (and probably below that), opens up a dialog box asking for the permission.
  1. In case of file upload:
  • None of them ask for any permission, and thus the upload fails.

Therefore, as a workaround to upload files, we have two ways of doing so:

  1. Either using some other directory, i.e. /data/local/tmp/ (The only thing I found working for Android 10 and above).
  2. (For Android 9 and probably below too) Download any file first, allow/accept the permissions requested. Then proceed to upload from the /sdcard/Download/ directory.
like image 122
Rohan Chougule Avatar answered Nov 20 '22 06:11

Rohan Chougule