Skip to content

Commit

Permalink
Merge pull request #8 from mocobk/main
Browse files Browse the repository at this point in the history
fix: get element rect api
  • Loading branch information
ZhouYixun authored Jul 31, 2023
2 parents e258587 + 230a991 commit 6219100
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
3 changes: 2 additions & 1 deletion client/android_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def get_rect(self) -> ElementRect:
)
)
if b.err is None:
element_rect = ElementRect(**json.loads(b.value))
rect = b.value if isinstance(b.value, dict) else json.loads(b.value)
element_rect = ElementRect(**rect)
self.logger.info(f"get {self.id} rect {element_rect}.")
return element_rect
else:
Expand Down
47 changes: 46 additions & 1 deletion client/uia_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import time
from base64 import b64decode, b64encode
from typing import Any, Dict, List
from typing import Any, Dict, List, Union

from client.android_element import AndroidElement
from common.http_client import HttpUtil, HttpRequest
Expand Down Expand Up @@ -289,3 +289,48 @@ def set_appium_settings(self, settings: dict):
else:
self.logger.error("set appium settings failed.")
raise SonicRespException(b.err.message)

def tap(self, x: int, y: int):
self.check_session_id()
data = {"x": x, "y": y}
b = self.resp_handler.get_resp(
HttpUtil.create_post(
self._remote_url + "/session/" + self.session_id + "/appium/tap"
).body(json.dumps(data))
)
if b.err is None:
self.logger.info("perform tap action %s.", json.dumps(data))
else:
self.logger.error("perform tap action failed.")
raise SonicRespException(b.err.message)

def long_press(self, x: Union[int, float], y: Union[int, float], duration_ms: Union[int, float]):
self.check_session_id()
touch_event_params = {"x": x, "y": y, "duration": duration_ms}
data = {"params": touch_event_params}
b = self.resp_handler.get_resp(
HttpUtil.create_post(
self._remote_url + "/session/" + self.session_id + "/touch/longclick"
).body(json.dumps(data))
)
if b.err is None:
self.logger.info("perform long_press action %s.", json.dumps(data))
else:
self.logger.error("perform long_press action failed.")
raise SonicRespException(b.err.message)

def swipe(self, start_x: Union[int, float], start_y: Union[int, float], end_x: Union[int, float],
end_y: Union[int, float], duration_ms: Union[int, float]):
self.check_session_id()
steps = int(duration_ms / 5) if duration_ms else 100
data = {"startX": start_x, "startY": start_y, "endX": end_x, "endY": end_y, "steps": steps}
b = self.resp_handler.get_resp(
HttpUtil.create_post(
self._remote_url + "/session/" + self.session_id + "/touch/perform"
).body(json.dumps(data))
)
if b.err is None:
self.logger.info("perform swipe action %s.", json.dumps(data))
else:
self.logger.error("perform swipe action failed.")
raise SonicRespException(b.err.message)
12 changes: 12 additions & 0 deletions uia2/driver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

from client.uia_client import UiaClient
from common.models import PasteboardType, AndroidSelector
from common.resp_handler import RespHandler
Expand Down Expand Up @@ -57,3 +59,13 @@ def screenshot(self):

def set_appium_settings(self, settings: dict):
self._client.set_appium_settings(settings)

def tap(self, x: int, y: int):
return self._client.tap(x, y)

def long_press(self, x: Union[int, float], y: Union[int, float], duration_ms: Union[int, float]):
return self._client.long_press(x, y, duration_ms)

def swipe(self, start_x: Union[int, float], start_y: Union[int, float], end_x: Union[int, float],
end_y: Union[int, float], duration_ms: Union[int, float]):
return self._client.swipe(start_x, start_y, end_x, end_y, duration_ms)

0 comments on commit 6219100

Please sign in to comment.