From 400adbaac5de3ca8e785ce7a9cb2a9b8b3d77ebe Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Sat, 11 May 2024 12:42:50 +0800 Subject: [PATCH] use 127.0.0.1 instead of localhost, update dump_hierarchy --- uiautomator2/__init__.py | 34 ++++++++++++++++++++++++++-------- uiautomator2/core.py | 4 +++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/uiautomator2/__init__.py b/uiautomator2/__init__.py index 7ca209f..5099dfe 100644 --- a/uiautomator2/__init__.py +++ b/uiautomator2/__init__.py @@ -11,12 +11,12 @@ import re import time import warnings -import xml.dom.minidom from functools import cached_property from typing import Any, Dict, List, Optional, Union import adbutils from deprecated import deprecated +from lxml import etree from retry import retry from uiautomator2.core import BasicUiautomatorServer @@ -261,21 +261,39 @@ def screenshot(self, filename: Optional[str] = None, format="pillow"): elif format == 'raw': return pil_img.tobytes() - @retry(exceptions=HierarchyEmptyError, tries=3, delay=1) def dump_hierarchy(self, compressed=False, pretty=False) -> str: - # empty output - # '\r\n' + """ + Dump window hierarchy + + Args: + compressed (bool): return compressed xml + pretty (bool): pretty print xml + + Returns: + xml content + """ + try: + content = self._do_dump_hierarchy(compressed) + except HierarchyEmptyError: + logger.warning("dump empty, return empty xml") + content = '\r\n' + + if pretty: + root = etree.fromstring(content.encode("utf-8")) + content = etree.tostring(root, pretty_print=True, encoding=str) + return content + + @retry(HierarchyEmptyError, tries=3, delay=1) + def _do_dump_hierarchy(self, compressed=False) -> str: content = self.jsonrpc.dumpWindowHierarchy(compressed, None) if content == "": raise HierarchyEmptyError("dump hierarchy is empty") + # '\r\n' if '' in content: + logger.debug("dump empty, call clear_traversed_text and retry") self.clear_traversed_text() raise HierarchyEmptyError("dump hierarchy is empty with no children") - - if pretty and "\n " not in content: - xml_text = xml.dom.minidom.parseString(content.encode("utf-8")) - content = xml_text.toprettyxml(indent=' ') return content def implicitly_wait(self, seconds: float = None) -> float: diff --git a/uiautomator2/core.py b/uiautomator2/core.py index 9e31310..6dcd546 100644 --- a/uiautomator2/core.py +++ b/uiautomator2/core.py @@ -88,7 +88,9 @@ def _http_request(dev: adbutils.AdbDevice, method: str, path: str, data: Dict[st logger.debug("http request %s %s %s", method, path, data) lport = dev.forward_port(9008) logger.debug("forward tcp:%d -> tcp:9008", lport) - url = f"http://localhost:{lport}{path}" + # https://stackoverflow.com/questions/2386299/running-sites-on-localhost-is-extremely-slow + # so here use 127.0.0.1 instead of localhost + url = f"http://127.0.0.1:{lport}{path}" if print_request: fields = [time.strftime("%H:%M:%S"), f"$ curl -X {method}", url] if data: