Skip to content

Commit

Permalink
use 127.0.0.1 instead of localhost, update dump_hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed May 11, 2024
1 parent 06fda31 commit 400adba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
34 changes: 26 additions & 8 deletions uiautomator2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
# '<?xml version=\'1.0\' encoding=\'UTF-8\' standalone=\'yes\' ?>\r\n<hierarchy rotation="0" />'
"""
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 = '<?xml version=\'1.0\' encoding=\'UTF-8\' standalone=\'yes\' ?>\r\n<hierarchy rotation="0" />'

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")

# '<?xml version=\'1.0\' encoding=\'UTF-8\' standalone=\'yes\' ?>\r\n<hierarchy rotation="0" />'
if '<hierarchy rotation="0" />' 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:
Expand Down
4 changes: 3 additions & 1 deletion uiautomator2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 400adba

Please sign in to comment.