app_control.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from lxml import etree
  2. from module.device.method.adb import Adb
  3. from module.device.method.uiautomator_2 import Uiautomator2
  4. from module.device.method.utils import HierarchyButton
  5. # from module.device.method.wsa import WSA
  6. from module.logger import logger
  7. class AppControl(Adb, Uiautomator2):
  8. hierarchy: etree._Element
  9. _app_u2_family = ['uiautomator2', 'minitouch', 'scrcpy']
  10. def app_is_running(self) -> bool:
  11. # 桌面模式:窗口存在 且 已完成登录(自动启动的客户端停在登录页,需先走登录流程)
  12. if self.is_desktop:
  13. return self.desktop_window_exists() and getattr(self, '_desktop_login_done', True)
  14. method = self.config.script.device.control_method
  15. # if self.is_wsa:
  16. # package = self.app_current_wsa()
  17. if method in AppControl._app_u2_family:
  18. package = self.app_current_uiautomator2()
  19. else:
  20. package = self.app_current_adb()
  21. package = package.strip(' \t\r\n')
  22. logger.attr('Package_name', package)
  23. return package == self.package
  24. def app_start(self):
  25. method = self.config.script.device.screenshot_method
  26. logger.info(f'App start: {self.package}')
  27. # if self.config.Emulator_Serial == 'wsa-0':
  28. # self.app_start_wsa(display=0)
  29. if method in AppControl._app_u2_family:
  30. self.app_start_uiautomator2()
  31. else:
  32. self.app_start_adb()
  33. def app_stop(self):
  34. method = self.config.script.device.screenshot_method
  35. logger.info(f'App stop: {self.package}')
  36. if method in AppControl._app_u2_family:
  37. self.app_stop_uiautomator2()
  38. else:
  39. self.app_stop_adb()
  40. def dump_hierarchy(self) -> etree._Element:
  41. """
  42. Returns:
  43. etree._Element: Select elements with `self.hierarchy.xpath('//*[@text="Hermit"]')` for example.
  44. """
  45. method = self.config.script.device.screenshot_method
  46. if method in AppControl._app_u2_family:
  47. self.hierarchy = self.dump_hierarchy_uiautomator2()
  48. else:
  49. self.hierarchy = self.dump_hierarchy_adb()
  50. return self.hierarchy
  51. def xpath_to_button(self, xpath: str) -> HierarchyButton:
  52. """
  53. Args:
  54. xpath (str):
  55. Returns:
  56. HierarchyButton:
  57. An object with methods and properties similar to Button.
  58. If element not found or multiple elements were found, return None.
  59. """
  60. return HierarchyButton(self.hierarchy, xpath)