import struct from pathlib import Path from appium import webdriver from appium.options.android import UiAutomator2Options APPIUM_SERVER = "http://127.0.0.1:4723" SCREENSHOT = Path("screenshots/settings-home.png") capabilities = { "platformName": "Android", "automationName": "UiAutomator2", "deviceName": "Android Emulator", "appPackage": "com.android.settings", "appActivity": ".Settings", "language": "en", "locale": "US", } def png_size(path: Path) -> tuple[int, int]: data = path.read_bytes() if not data.startswith(b"\x89PNG\r\n\x1a\n"): raise RuntimeError(f"{path} is not a PNG screenshot") return struct.unpack(">II", data[16:24]) SCREENSHOT.parent.mkdir(exist_ok=True) driver = webdriver.Remote( APPIUM_SERVER, options=UiAutomator2Options().load_capabilities(capabilities), ) try: if not driver.save_screenshot(str(SCREENSHOT)): raise RuntimeError("Appium did not save the screenshot") width, height = png_size(SCREENSHOT) print(f"session={driver.session_id}") print(f"saved={SCREENSHOT}") print("format=PNG") print(f"size={width}x{height}") finally: driver.quit()