Skip to content

Commit 47c504a

Browse files
committed
Get tree with matches and hierarchy
1 parent 7cdccbb commit 47c504a

2 files changed

Lines changed: 40 additions & 12 deletions

File tree

robocorp-code/src/robocorp_code/inspector/java/java_inspector.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
from JABWrapper.jab_wrapper import JavaWindow
55
from robocorp_ls_core.robotframework_log import get_logger
66

7+
from robocorp_code.inspector.java.robocorp_java._inspector import ColletedTreeTypedDict
8+
79
log = get_logger(__name__)
810

911
JavaWindowInfoTypedDict = TypedDict(
1012
"JavaWindowInfoTypedDict",
1113
{
12-
# Same as JavaWindow
1314
"pid": int,
1415
"hwnd": int,
1516
"title": str,
@@ -32,10 +33,14 @@
3233
},
3334
)
3435

35-
LocatorTreeInfoTypedDict = TypedDict(
36-
"LocatorTreeInfoTypedDict",
37-
{"nodes": List[LocatorNodeInfoTypedDict]},
38-
)
36+
37+
class MatchesAndHierarchyTypedDict(TypedDict):
38+
# A list with the nodes matched (these are the ones that the
39+
# locator matched)
40+
matched_paths: List[str]
41+
# This includes all the entries found along with the full hierarchy
42+
# to reach the matched entries.
43+
hierarchy: List[LocatorNodeInfoTypedDict]
3944

4045

4146
def to_window_info(java_window: JavaWindow) -> JavaWindowInfoTypedDict:
@@ -52,6 +57,18 @@ def to_locator_info(context_node: ContextNode) -> LocatorNodeInfoTypedDict:
5257
return cast(LocatorNodeInfoTypedDict, ret)
5358

5459

60+
def to_matches_and_hierarchy(
61+
matches_and_hierarchy: ColletedTreeTypedDict,
62+
) -> MatchesAndHierarchyTypedDict:
63+
matches = (
64+
[str(matches_and_hierarchy["matches"])]
65+
if type(matches_and_hierarchy["matches"]) == ContextNode
66+
else [str(match) for match in matches_and_hierarchy["matches"]]
67+
)
68+
hierarchy = [to_locator_info(node) for node in matches_and_hierarchy["tree"]]
69+
return {"matches": matches, "hierarchy": hierarchy}
70+
71+
5572
class JavaInspector:
5673
def __init__(self):
5774
from robocorp_code.inspector.java.robocorp_java._inspector import (
@@ -66,8 +83,10 @@ def list_windows(self) -> List[JavaWindowInfoTypedDict]:
6683

6784
def collect_tree(
6885
self, window: str, search_depth: int = 8, locator: Optional[str] = None
69-
) -> LocatorTreeInfoTypedDict:
86+
) -> MatchesAndHierarchyTypedDict:
7087
log.info(f"Collect tree from locator: {locator}")
7188

72-
tree = self._inspector.collect_tree(window, search_depth, locator)
73-
return [to_locator_info(node) for node in tree]
89+
matches_and_hierarchy = self._inspector.collect_tree(
90+
window, search_depth, locator
91+
)
92+
return to_matches_and_hierarchy(matches_and_hierarchy)

robocorp-code/src/robocorp_code/inspector/java/robocorp_java/_inspector.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
from typing import List, Optional, Union
1+
from typing import List, Optional, TypedDict, Union
22

33
from JABWrapper.context_tree import ContextNode, ContextTree
44
from JABWrapper.jab_wrapper import JavaAccessBridgeWrapper, JavaWindow
55

6+
ColletedTreeTypedDict = TypedDict(
7+
"ColletedTreeTypedDict",
8+
{
9+
"matches": Union[ContextNode, List[ContextNode]],
10+
"tree": ContextNode,
11+
},
12+
)
13+
614

715
class ElementInspector:
816
def _start_event_pump(func, *args, **kwargs):
@@ -31,11 +39,12 @@ def collect_tree(
3139
window: str,
3240
search_depth: int,
3341
locator: Optional[str] = None,
34-
) -> Union[ContextNode, List[ContextNode]]:
42+
) -> ColletedTreeTypedDict:
3543
jab_wrapper.switch_window_by_title(window)
3644
context_tree = ContextTree(jab_wrapper, search_depth)
45+
matches: Union[ContextNode, List[ContextNode]] = []
3746
if locator:
3847
from ._locators import find_elements_from_tree
3948

40-
return find_elements_from_tree(context_tree, locator)
41-
return context_tree.root
49+
matches = find_elements_from_tree(context_tree, locator)
50+
return {"matches": matches, "tree": context_tree.root}

0 commit comments

Comments
 (0)