| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- from lxml import etree
- from module.device.method.adb import Adb
- from module.device.method.uiautomator_2 import Uiautomator2
- from module.device.method.utils import HierarchyButton
- # from module.device.method.wsa import WSA
- from module.logger import logger
- class AppControl(Adb, Uiautomator2):
- hierarchy: etree._Element
- _app_u2_family = ['uiautomator2', 'minitouch', 'scrcpy']
- def app_is_running(self) -> bool:
- # 桌面模式:窗口存在 且 已完成登录(自动启动的客户端停在登录页,需先走登录流程)
- if self.is_desktop:
- return self.desktop_window_exists() and getattr(self, '_desktop_login_done', True)
- method = self.config.script.device.control_method
- # if self.is_wsa:
- # package = self.app_current_wsa()
- if method in AppControl._app_u2_family:
- package = self.app_current_uiautomator2()
- else:
- package = self.app_current_adb()
- package = package.strip(' \t\r\n')
- logger.attr('Package_name', package)
- return package == self.package
- def app_start(self):
- method = self.config.script.device.screenshot_method
- logger.info(f'App start: {self.package}')
- # if self.config.Emulator_Serial == 'wsa-0':
- # self.app_start_wsa(display=0)
- if method in AppControl._app_u2_family:
- self.app_start_uiautomator2()
- else:
- self.app_start_adb()
- def app_stop(self):
- method = self.config.script.device.screenshot_method
- logger.info(f'App stop: {self.package}')
- if method in AppControl._app_u2_family:
- self.app_stop_uiautomator2()
- else:
- self.app_stop_adb()
- def dump_hierarchy(self) -> etree._Element:
- """
- Returns:
- etree._Element: Select elements with `self.hierarchy.xpath('//*[@text="Hermit"]')` for example.
- """
- method = self.config.script.device.screenshot_method
- if method in AppControl._app_u2_family:
- self.hierarchy = self.dump_hierarchy_uiautomator2()
- else:
- self.hierarchy = self.dump_hierarchy_adb()
- return self.hierarchy
- def xpath_to_button(self, xpath: str) -> HierarchyButton:
- """
- Args:
- xpath (str):
- Returns:
- HierarchyButton:
- An object with methods and properties similar to Button.
- If element not found or multiple elements were found, return None.
- """
- return HierarchyButton(self.hierarchy, xpath)
|