Python Example 1
Goal to Achieve
Open the Battery screen on an iPhone using Python.

Steps to reproduce:
- Open Settings.
- Scroll to "Battery" option and click on it.
Prerequisites
- Common environment steps to be completed.
- iOS environment steps to be completed.
- Python installed on the computer.
- A connected iPhone, as described here.
- Appium server running, as described here.
Project Setup
Create a project folder named
ios-python-1with two files, as follows:└── ios-python-1/ ├── test_battery.py └── requirements.txtCopy the following content to the
requirements.txtfile:txtAppium-Python-ClientCopy the following content to the
test_battery.pyfile:pythonimport 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()Initialize the Python virtual environment. Follow the instructions here.
Install the dependencies:
shellpip 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