app_control.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. method = self.config.script.device.control_method
  12. # if self.is_wsa:
  13. # package = self.app_current_wsa()
  14. if method in AppControl._app_u2_family:
  15. package = self.app_current_uiautomator2()
  16. else:
  17. package = self.app_current_adb()
  18. package = package.strip(' \t\r\n')
  19. logger.attr('Package_name', package)
  20. return package == self.package
  21. def app_start(self):
  22. method = self.config.script.device.screenshot_method
  23. logger.info(f'App start: {self.package}')
  24. # if self.config.Emulator_Serial == 'wsa-0':
  25. # self.app_start_wsa(display=0)
  26. if method in AppControl._app_u2_family:
  27. self.app_start_uiautomator2()
  28. else:
  29. self.app_start_adb()
  30. def app_stop(self):
  31. method = self.config.script.device.screenshot_method
  32. logger.info(f'App stop: {self.package}')
  33. if method in AppControl._app_u2_family:
  34. self.app_stop_uiautomator2()
  35. else:
  36. self.app_stop_adb()
  37. def dump_hierarchy(self) -> etree._Element:
  38. """
  39. Returns:
  40. etree._Element: Select elements with `self.hierarchy.xpath('//*[@text="Hermit"]')` for example.
  41. """
  42. method = self.config.script.device.screenshot_method
  43. if method in AppControl._app_u2_family:
  44. self.hierarchy = self.dump_hierarchy_uiautomator2()
  45. else:
  46. self.hierarchy = self.dump_hierarchy_adb()
  47. return self.hierarchy
  48. def xpath_to_button(self, xpath: str) -> HierarchyButton:
  49. """
  50. Args:
  51. xpath (str):
  52. Returns:
  53. HierarchyButton:
  54. An object with methods and properties similar to Button.
  55. If element not found or multiple elements were found, return None.
  56. """
  57. return HierarchyButton(self.hierarchy, xpath)