Skip to content

Python Example 1

Goal to Achieve

Open the Battery screen on an iPhone using Python.

Battery Settings

Steps to reproduce:

  • Open Settings.
  • Scroll to "Battery" option and click on it.

Prerequisites

Project Setup

  1. Create a project folder named ios-python-1 with two files, as follows:

    └── ios-python-1/
       ├── test_battery.py
       └── requirements.txt
  2. Copy the following content to the requirements.txt file:

    txt
    Appium-Python-Client
  3. Copy the following content to the test_battery.py file:

    python
    import unittest
    from appium.webdriver.webdriver import WebDriver
    from appium.options.ios.xcuitest.base import XCUITestOptions
    from appium.webdriver.common.appiumby import AppiumBy
    import time
    
    capabilities = dict(
        platformName='iOS',
        automationName='XCUITest',
        udid='00000000-0000000000000000', # <-- Change this to your device's UDID
        usePrebuiltWDA=True,
        bundleId='com.apple.Preferences',
    )
    
    appium_server_url = 'http://localhost:4723'
    
    class TestAppium(unittest.TestCase):
        def setUp(self) -> None:
            self.driver = WebDriver(appium_server_url, options=XCUITestOptions().load_capabilities(capabilities))
    
        def tearDown(self) -> None:
            if self.driver:
                self.driver.quit()
    
        def test_find_battery(self) -> None:
            battery_menu = 'Bateria' # <-- Change this to your Battery menu text if needed
            try:
                el = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, battery_menu)
                self.driver.execute_script("mobile: scroll", {"element": el, "toVisible": True})
                el.click()
                time.sleep(2)
                self.assertTrue(True)
    
            except Exception:
                self.fail("Battery element not found")
    
    if __name__ == '__main__':
        unittest.main()
  4. Initialize the Python virtual environment. Follow the instructions here.

  5. Install the dependencies:

    shell
    pip install -r requirements.txt

Capabilities

Before running, you need to adjust the capabilities dictionary:

  • udid:
    Replace '00000000-0000000000000000' with the UDID of your connected device.
    You can find this ID as explained here.

Running the script

To run the script, execute the following command in your terminal (with the virtual environment activated):

shell
python test_battery.py