|
| 1 | +"""Element lookup over the uiautomator2 widget tree. |
| 2 | +
|
| 3 | +The Android equivalent of the macOS accessibility-tree locator: |
| 4 | +callers describe a widget by ``text`` / ``resource_id`` / |
| 5 | +``description`` / ``class_name``, and the helper returns the |
| 6 | +bounding rect or taps it. The thin :func:`dump_hierarchy` is |
| 7 | +exposed so test code can snapshot the live UI tree. |
| 8 | +""" |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from typing import Any, Dict, Optional, Tuple |
| 12 | + |
| 13 | +from je_auto_control.android.client import ( |
| 14 | + UIAutomatorDevice, default_ui_device, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class ElementNotFoundError(LookupError): |
| 19 | + """Raised when no widget on screen matches the supplied selector.""" |
| 20 | + |
| 21 | + |
| 22 | +def _build_query(handle: Any, |
| 23 | + text: Optional[str], |
| 24 | + resource_id: Optional[str], |
| 25 | + description: Optional[str], |
| 26 | + class_name: Optional[str]) -> Any: |
| 27 | + """Translate the public kwargs into uiautomator2's chained selector.""" |
| 28 | + selectors: Dict[str, Any] = {} |
| 29 | + if text is not None: |
| 30 | + selectors["text"] = text |
| 31 | + if resource_id is not None: |
| 32 | + selectors["resourceId"] = resource_id |
| 33 | + if description is not None: |
| 34 | + selectors["description"] = description |
| 35 | + if class_name is not None: |
| 36 | + selectors["className"] = class_name |
| 37 | + if not selectors: |
| 38 | + raise ValueError( |
| 39 | + "at least one of text/resource_id/description/class_name " |
| 40 | + "is required", |
| 41 | + ) |
| 42 | + return handle(**selectors) |
| 43 | + |
| 44 | + |
| 45 | +def find_element(text: Optional[str] = None, |
| 46 | + resource_id: Optional[str] = None, |
| 47 | + description: Optional[str] = None, |
| 48 | + class_name: Optional[str] = None, |
| 49 | + *, timeout_s: float = 5.0, |
| 50 | + device: Optional[UIAutomatorDevice] = None, |
| 51 | + ) -> Tuple[int, int, int, int]: |
| 52 | + """Return the matched widget's bounding rect ``(x1, y1, x2, y2)``.""" |
| 53 | + handle = (device or default_ui_device()).handle |
| 54 | + query = _build_query(handle, text, resource_id, description, class_name) |
| 55 | + if not query.wait(timeout=float(timeout_s)): |
| 56 | + raise ElementNotFoundError( |
| 57 | + f"no widget matched selectors text={text!r} " |
| 58 | + f"resource_id={resource_id!r} description={description!r} " |
| 59 | + f"class_name={class_name!r}", |
| 60 | + ) |
| 61 | + info = query.info |
| 62 | + bounds = info.get("bounds") or {} |
| 63 | + return ( |
| 64 | + int(bounds.get("left", 0)), |
| 65 | + int(bounds.get("top", 0)), |
| 66 | + int(bounds.get("right", 0)), |
| 67 | + int(bounds.get("bottom", 0)), |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def click_element(text: Optional[str] = None, |
| 72 | + resource_id: Optional[str] = None, |
| 73 | + description: Optional[str] = None, |
| 74 | + class_name: Optional[str] = None, |
| 75 | + *, timeout_s: float = 5.0, |
| 76 | + device: Optional[UIAutomatorDevice] = None, |
| 77 | + ) -> Tuple[int, int]: |
| 78 | + """Tap the matched widget; return the click-centre ``(x, y)``. |
| 79 | +
|
| 80 | + Uses the uiautomator2 handle for the tap rather than ``adb shell |
| 81 | + input tap`` so the daemon notices the press synchronously and |
| 82 | + can update its event queue. |
| 83 | + """ |
| 84 | + bounds = find_element( |
| 85 | + text=text, resource_id=resource_id, description=description, |
| 86 | + class_name=class_name, timeout_s=timeout_s, device=device, |
| 87 | + ) |
| 88 | + cx = (bounds[0] + bounds[2]) // 2 |
| 89 | + cy = (bounds[1] + bounds[3]) // 2 |
| 90 | + handle = (device or default_ui_device()).handle |
| 91 | + handle.click(int(cx), int(cy)) |
| 92 | + return (int(cx), int(cy)) |
| 93 | + |
| 94 | + |
| 95 | +def dump_hierarchy(*, device: Optional[UIAutomatorDevice] = None) -> str: |
| 96 | + """Return the device's current widget tree as an XML string.""" |
| 97 | + handle = (device or default_ui_device()).handle |
| 98 | + return str(handle.dump_hierarchy()) |
| 99 | + |
| 100 | + |
| 101 | +__all__ = [ |
| 102 | + "ElementNotFoundError", "click_element", "dump_hierarchy", |
| 103 | + "find_element", |
| 104 | +] |
0 commit comments