From 2f43bbe3091abde8e1027dc60f21c927cb86fb8a Mon Sep 17 00:00:00 2001 From: ut001652 Date: Wed, 11 Sep 2024 11:11:43 +0800 Subject: [PATCH] =?UTF-8?q?dogtail=20=E5=A2=9E=E5=8A=A0=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E6=8C=87=E5=AE=9Aapp=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dogtail_utils.py | 107 ++++++++++++++++++++++++++++++++++++++++ src/remotectl/remote.py | 13 ++++- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/src/dogtail_utils.py b/src/dogtail_utils.py index 4eb28df8..b68b3d34 100644 --- a/src/dogtail_utils.py +++ b/src/dogtail_utils.py @@ -60,6 +60,113 @@ def __init__(self, name=None, description=None, number=-1, check_start=True, key logger.error(search_app) raise ApplicationStartError(self.name) from SearchError + @staticmethod + def __remote_init(app=None, name=None, roleName=None, description=None, label=None, debugName=None, + obj_index=-1, check_start=True): + if GlobalConfig.NO_DOGTAIL: + raise EnvironmentError("Dogtail 及其相关依赖存在问题,调用相关方法失败~") + try: + obj = root.application(app) + if obj_index > 0: + obj = obj.findChildren(predicate.GenericPredicate( + name, roleName, description, label, debugName) + )[obj_index] + return obj + except SearchError: + if check_start: + search_app = CmdCtl.run_cmd(f"ps -ef | grep {app}") + logger.error(search_app) + raise ApplicationStartError(app) from SearchError + + def __get_remote_app_element(self, element, **kwargs): + """ 远程特有元素获取 + + Args: + element: 应用的元素 + **kwargs: app, roleName, description, label, debugName, obj_index, check_start + + Returns: element + + """ + kwargs["name"] = element + if kwargs.get('app'): + obj = self.__remote_init(**kwargs) + del kwargs['app'] + else: + obj = self.obj + + try: + element = obj.child(**kwargs, retry=False) + logger.debug(f"{kwargs} 获取元素对象 <{element}>") + return element + except SearchError: + raise ElementNotFound(**kwargs) from SearchError + + def get_element_children_txt(self, element, index, **kwargs): + """获取元素的子元素文本 + + Args: + element: 元素 + index: 位置 + kwargs:app, roleName, description, label, debugName, obj_index, check_start + + Returns: 文本 + + """ + kwargs["name"] = element + element = self.__get_remote_app_element(element, **kwargs) + all_children = element.children + return all_children[index].name + + def remote_element_click(self, element, button=1, **kwargs): + """ 远程特有元素点击 + + Args: + element: 应用的元素 + button: 1>left,2>middle,3>right + **kwargs: app, roleName, description, label, debugName, obj_index, check_start + + Returns: None + + """ + kwargs["name"] = element + logger.debug( + f"""{"左键" if button == 1 else f"{'右键' if button == 3 else '鼠标中健'}"} 点击元素 {element}""" + ) + mouse_click = ( + self.click if button == 1 else self.right_click if button == 3 else self.middle_click + ) + mouse_click(self.remote_element_center(element, **kwargs)) + + def remote_element_center(self, element, **kwargs): + """ 远程特有元素位置获取 + + Args: + element: 应用的元素 + **kwargs: app, roleName, description, label, debugName, obj_index, check_start + + Returns: None + + """ + element = self.__get_remote_app_element(element, **kwargs) + _x, _y, _w, _h = element.extents + print(f"获取元素中心坐标 ({_x, _y})") + return _x + _w / 2, _y + _h / 2 + + def element_click_by_brother(self, element, **kwargs): + """通过相邻元素点击 + + Args: + element: 元素 + kwargs:app, roleName, description, label, debugName, obj_index, check_start + + Returns: + + """ + kwargs["name"] = element + element = self.__get_remote_app_element(element, **kwargs) + element.get_parent().click() + def app_element(self, *args, **kwargs) -> Node: """ 获取app元素的对象 diff --git a/src/remotectl/remote.py b/src/remotectl/remote.py index 4499cc84..f1ca7b22 100644 --- a/src/remotectl/remote.py +++ b/src/remotectl/remote.py @@ -66,8 +66,17 @@ def rdog(self) -> DogtailUtils: restart_service=self.restart_service, ) - def click_element_by_attr(self, element, button=1): - self.rdog.element_click(element, button=button) + def click_element_by_attr(self, element, button=1, app=None, **kwargs): + self.rdog.remote_element_click(element, button=button, app=app, **kwargs) + + def click_element_by_brother(self, element, app=None): + self.rdog.element_click_by_brother(element, app=app) + + def get_element_children_txt(self, element, index, app=None): + return self.rdog.get_element_children_txt(element, index, app=app) + + def get_element_location(self, element, app=None, **kwargs): + return self.rdog.remote_element_center(element, app=app, **kwargs) @property def rctl(self) -> Src: