From 4f35965a8a5a636a3e3a44e287e28d1d29f32cdc Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Sun, 12 May 2024 09:11:45 +0800 Subject: [PATCH] update apk version 2.3.8 --- uiautomator2/__init__.py | 3 ++- uiautomator2/assets/sync.sh | 2 +- uiautomator2/core.py | 38 ++++++++++++++++++++++--------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/uiautomator2/__init__.py b/uiautomator2/__init__.py index 5099dfe..0aaa3cf 100644 --- a/uiautomator2/__init__.py +++ b/uiautomator2/__init__.py @@ -43,6 +43,7 @@ def enable_pretty_logging(level=logging.DEBUG): formatter = logging.Formatter('[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d pid:%(process)d] %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) + logger.setLevel(level) @@ -100,7 +101,7 @@ def _wait_for_device(self, timeout=10) -> adbutils.AdbDevice: return None @property - def adb_device(self): + def adb_device(self) -> adbutils.AdbDevice: return self._dev @cached_property diff --git a/uiautomator2/assets/sync.sh b/uiautomator2/assets/sync.sh index e7adc4a..725d6b2 100755 --- a/uiautomator2/assets/sync.sh +++ b/uiautomator2/assets/sync.sh @@ -3,7 +3,7 @@ set -e -APK_VERSION="2.3.7" +APK_VERSION="2.3.8" # AGENT_VERSION="0.10.1" cd "$(dirname $0)" diff --git a/uiautomator2/core.py b/uiautomator2/core.py index 6dcd546..34a2dcb 100644 --- a/uiautomator2/core.py +++ b/uiautomator2/core.py @@ -155,6 +155,7 @@ def __init__(self, dev: adbutils.AdbDevice) -> None: self._process = None self._lock = threading.Lock() self._debug = False + self.start_uiautomator() atexit.register(self.stop_uiautomator) @property @@ -177,8 +178,9 @@ def start_uiautomator(self): if self._process: if self._process.pool() is not None: self._process = None - self._process = launch_uiautomator(self._dev) - self._wait_ready() + if not self._check_alive(): + self._process = launch_uiautomator(self._dev) + self._wait_ready() def _setup_apks(self): assets_dir = Path(__file__).parent / "assets" @@ -193,17 +195,20 @@ def _setup_apks(self): if k == "apk_version": apk_version = v.strip() break - logger.debug("apk_version: %s", apk_version) + logger.debug("use apk_version: %s", apk_version) # install apk when not installed or version not match, dev version always keep main_apk_info = self._dev.app_info("com.github.uiautomator") if main_apk_info is None: self._install_apk(main_apk) - elif main_apk_info.version_name != apk_version and "dev" not in main_apk_info.version_name: - logger.debug("apk version not match, reinstall") - self._dev.uninstall("com.github.uiautomator") - self._dev.uninstall("com.github.uiautomator.test") - self._install_apk(main_apk) - self._install_apk(test_apk) + elif main_apk_info.version_name != apk_version: + if "dev" in main_apk_info.version_name or "dirty" in main_apk_info.version_name: + logger.debug("skip version check for %s", main_apk_info.version_name) + else: + logger.debug("apk version not match, reinstall") + self._dev.uninstall("com.github.uiautomator") + self._dev.uninstall("com.github.uiautomator.test") + self._install_apk(main_apk) + self._install_apk(test_apk) if self._dev.app_info("com.github.uiautomator.test") is None: self._install_apk(test_apk) @@ -246,13 +251,16 @@ def _wait_stub_ready(self, timeout: float): raise AccessibilityServiceAlreadyRegisteredError("Possibly another UiAutomation service is running, you may find it output by \"adb shell ps -u shell\"",) if self._process.pool() is not None: raise LaunchUiAutomationError("uiautomator2 server quit", output) - try: - response = _http_request(self._dev, "GET", "/ping") - if response.content == b"pong": - return - except HTTPError: - time.sleep(.5) + if self._check_alive(): + return raise LaunchUiAutomationError("uiautomator2 server not ready") + + def _check_alive(self) -> bool: + try: + response = _http_request(self._dev, "GET", "/ping") + return response.content == b"pong" + except HTTPError: + return False def _wait_ready(self, launch_timeout=30, service_timeout=30): """Wait until uiautomator2 server is ready"""