简体   繁体   中英

How to grant Android system permissions using Python

I am new to Python and trying to make an automation test for an application using Appium 1.3.1 and Python 3.6 on Android 7.1.1 simulator . At this point I am stuck at system permissions popup and don't know how to select "ALLOW" element with Python (not the same as selecting regular button inside the application). Application doesn't start until access to files is granted but I am not sure exactly where to set this in code and how to write it in Python. Does someone have some kind of sample code or some idea how to do this? This is what I have done so far:

import os
import unittest
from appium import webdriver
from time import sleep

class meTest(unittest.TestCase):

def setUp(self):
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '7.1.1'
    desired_caps['deviceName'] = 'Android Emulator'
    # Returns abs path relative to this file and not cwd
    desired_caps['app'] = ‘example.apk'

    self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

 def test_OpenDocuments(self):
    sleep(10)
    documentSearchButton = self.driver.find_element_by_id(
        ‘example.example').click()


def tearDown(self):
    self.driver.quit()


if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(meTest)
unittest.TextTestRunner(verbosity=2).run(suite)

Thanks.

To grant the permission (in my case for the "ALLOW" button in Android permission dialog) it is necessary to access the desired_caps in Appium webdriver. It automatically granted all permissions for this app.

desired_caps['autoGrantPermissions'] = 'true'

As far as I know you can generally set permissions using os.chown(). In your case (Python 3.6) the following could work:

os.chmod("path/to/your/file", 0o666)

Of course you need to modify the access rights to meet your requirements, '0o666' is just an example for a return like:

-rw-rw-rw- 1 ag ag 0 Mar 25 05:55 your_file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM