Skip to content

Commit

Permalink
add android and ios home, volume control (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue authored May 30, 2024
1 parent 7b56fff commit 48379ec
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 18 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.8"
pillow = "*"
adbutils = "^2.6.0"
adbutils = "^2.7.0"
construct = "*"
lxml = "*"
click = "^8.1.7"
Expand All @@ -23,6 +23,7 @@ fastapi = "^0.111.0"
uvicorn = {version = "*", extras = ["standard"]}
poetry = "^1.8.2"
pydantic = "^2.6"
wdapy = "^0.2.2"

[tool.poetry.extras]
appium = ["appium-python-client", "httppretty"]
Expand Down
8 changes: 2 additions & 6 deletions uiautodev/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
"""Created on Mon Mar 04 2024 14:28:53 by codeskyblue
"""

from importlib.metadata import PackageNotFoundError, version

try:
__version__ = version("uiautodev")
except PackageNotFoundError:
__version__ = "0.0.0"
# version is auto managed by poetry
__version__ = "0.4.0"
25 changes: 25 additions & 0 deletions uiautodev/command_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ def home(driver: BaseDriver):
driver.home()


@register(Command.BACK)
def back(driver: BaseDriver):
driver.back()


@register(Command.APP_SWITCH)
def app_switch(driver: BaseDriver):
driver.app_switch()


@register(Command.VOLUME_UP)
def volume_up(driver: BaseDriver):
driver.volume_up()


@register(Command.VOLUME_DOWN)
def volume_down(driver: BaseDriver):
driver.volume_down()


@register(Command.VOLUME_MUTE)
def volume_mute(driver: BaseDriver):
driver.volume_mute()


@register(Command.DUMP)
def dump(driver: BaseDriver) -> DumpResponse:
source, _ = driver.dump_hierarchy()
Expand Down
7 changes: 7 additions & 0 deletions uiautodev/command_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class Command(str, enum.Enum):

LIST = "list"

# 0.4.0
BACK = "back"
APP_SWITCH = "appSwitch"
VOLUME_UP = "volumeUp"
VOLUME_DOWN = "volumeDown"
VOLUME_MUTE = "volumeMute"


class TapRequest(BaseModel):
x: Union[int, float]
Expand Down
15 changes: 15 additions & 0 deletions uiautodev/driver/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ def home(self):

def wake_up(self):
self.adb_device.keyevent("WAKEUP")

def back(self):
self.adb_device.keyevent("BACK")

def app_switch(self):
self.adb_device.keyevent("APP_SWITCH")

def volume_up(self):
self.adb_device.keyevent("VOLUME_UP")

def volume_down(self):
self.adb_device.keyevent("VOLUME_DOWN")

def volume_mute(self):
self.adb_device.keyevent("VOLUME_MUTE")


def parse_xml(xml_data: str, wsize: WindowSize, display_id: Optional[int] = None) -> Node:
Expand Down
20 changes: 20 additions & 0 deletions uiautodev/driver/base_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ def app_terminate(self, package: str):
def home(self):
""" press home button """
raise NotImplementedError()

def back(self):
""" press back button """
raise NotImplementedError()

def app_switch(self):
""" switch app """
raise NotImplementedError()

def volume_up(self):
""" volume up """
raise NotImplementedError()

def volume_down(self):
""" volume down """
raise NotImplementedError()

def volume_mute(self):
""" volume mute """
raise NotImplementedError()

def wake_up(self):
""" wake up the device """
Expand Down
31 changes: 20 additions & 11 deletions uiautodev/driver/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
from typing import List, Optional, Tuple
from xml.etree import ElementTree

import wdapy
from PIL import Image

from uiautodev.command_types import CurrentAppResponse
from uiautodev.driver.base_driver import BaseDriver
from uiautodev.exceptions import IOSDriverException
from uiautodev.model import Node, WindowSize
from uiautodev.utils.usbmux import MuxDevice, select_device
from uiautodev.utils.usbmux import select_device


class IOSDriver(BaseDriver):
def __init__(self, serial: str):
""" serial is the udid of the ios device """
super().__init__(serial)
self.device = select_device(serial)
self.wda = wdapy.AppiumUSBClient(self.device.serial)

def _request(self, method: str, path: str, payload: Optional[dict] = None) -> bytes:
conn = self.device.make_http_connection(port=8100)
Expand Down Expand Up @@ -56,29 +58,36 @@ def status(self):
return self._request_json("GET", "/status")

def screenshot(self, id: int = 0) -> Image.Image:
png_base64 = self._request_json_value("GET", "/screenshot")
png_data = base64.b64decode(png_base64)
return Image.open(io.BytesIO(png_data))
return self.wda.screenshot()

def window_size(self):
return self._request_json_value("GET", "/window/size")
return self.wda.window_size()

def dump_hierarchy(self) -> Tuple[str, Node]:
"""returns xml string and hierarchy object"""
xml_data = self._request_json_value("GET", "/source")
t = self.wda.sourcetree()
xml_data = t.value
root = ElementTree.fromstring(xml_data)
return xml_data, parse_xml_element(root, WindowSize(width=1, height=1))

def tap(self, x: int, y: int):
self._request("POST", f"/wda/tap/0", {"x": x, "y": y})
self.wda.tap(x, y)

def app_current(self) -> CurrentAppResponse:
# {'processArguments': {'env': {}, 'args': []}, 'name': '', 'pid': 32, 'bundleId': 'com.apple.springboard'}
value = self._request_json_value("GET", "/wda/activeAppInfo")
return CurrentAppResponse(package=value["bundleId"], pid=value["pid"])
info = self.wda.app_current()
return CurrentAppResponse(package=info.bundle_id, pid=info.pid)

def home(self):
self._request("POST", "/wda/homescreen")
self.wda.homescreen()

def app_switch(self):
raise NotImplementedError()

def volume_up(self):
self.wda.volume_up()

def volume_down(self):
self.wda.volume_down()


def parse_xml_element(element, wsize: WindowSize, indexes: List[int]=[0]) -> Node:
Expand Down

0 comments on commit 48379ec

Please sign in to comment.