From a4a0a3387ec83b0cd3b7af752eb2b7151a48caab Mon Sep 17 00:00:00 2001 From: aidenmo Date: Sun, 30 Jul 2023 22:20:32 +0800 Subject: [PATCH 1/2] fix: get element rect api --- client/android_element.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/android_element.py b/client/android_element.py index c05450e..84358bb 100644 --- a/client/android_element.py +++ b/client/android_element.py @@ -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: From 230a991ff27886259072041e1d381d2e17e57066 Mon Sep 17 00:00:00 2001 From: aidenmo Date: Sun, 30 Jul 2023 23:02:40 +0800 Subject: [PATCH 2/2] feat: add tap, long_press, swipe api --- client/uia_client.py | 47 +++++++++++++++++++++++++++++++++++++++++++- uia2/driver.py | 12 +++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/client/uia_client.py b/client/uia_client.py index 17070f9..a7c1467 100644 --- a/client/uia_client.py +++ b/client/uia_client.py @@ -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 @@ -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) diff --git a/uia2/driver.py b/uia2/driver.py index 1331ecc..4928587 100644 --- a/uia2/driver.py +++ b/uia2/driver.py @@ -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 @@ -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)