1313
1414
1515class ElementInspector :
16+ def __init__ (self ) -> None :
17+ self ._context : Optional [ContextNode ] = None
18+
1619 def _start_event_pump (func , * args , ** kwargs ):
1720 def wrapper (self : "ElementInspector" , * args , ** kwargs ):
1821 from ._event_pump import EventPumpThread
@@ -32,6 +35,47 @@ def wrapper(self: "ElementInspector", *args, **kwargs):
3235 def list_windows (self , jab_wrapper : JavaAccessBridgeWrapper ) -> List [JavaWindow ]:
3336 return jab_wrapper .get_windows ()
3437
38+ def _collect_from_root (
39+ self ,
40+ jab_wrapper : JavaAccessBridgeWrapper ,
41+ locator : Optional [str ],
42+ search_depth = 1 ,
43+ ) -> ColletedTreeTypedDict :
44+ self ._context = ContextTree (jab_wrapper , search_depth ).root
45+ matches : Union [ContextNode , List [ContextNode ]] = []
46+ if locator :
47+ from ._locators import find_elements_from_tree
48+
49+ matches = find_elements_from_tree (self ._context , locator )
50+ return {"matches" : matches , "tree" : self ._context }
51+
52+ def _collect_from_context (
53+ self , jab_wrapper : JavaAccessBridgeWrapper , locator : str , search_depth = 1
54+ ) -> ColletedTreeTypedDict :
55+ from threading import RLock
56+
57+ from ._errors import NoMatchingLocatorException
58+ from ._locators import find_elements_from_tree
59+
60+ # The JavaAccessBridgeWrapper object needs to be inserted into the context as the
61+ # object has to be recreated every time we do a new query
62+ # TODO: update the ContextTree to introduce the API for this
63+ self ._context ._jab_wrapper = jab_wrapper
64+ match = find_elements_from_tree (self ._context , locator )
65+ node = match [0 ] if isinstance (match , List ) and len (match ) > 0 else match
66+ if not isinstance (node , ContextNode ):
67+ raise NoMatchingLocatorException (f"No matching locator for { locator } " )
68+ self ._context = ContextNode (
69+ jab_wrapper ,
70+ node .context ,
71+ RLock (),
72+ node .ancestry ,
73+ True ,
74+ search_depth + node .ancestry ,
75+ )
76+ matches = find_elements_from_tree (self ._context , locator )
77+ return {"matches" : matches , "tree" : self ._context }
78+
3579 @_start_event_pump
3680 def collect_tree (
3781 self ,
@@ -41,10 +85,8 @@ def collect_tree(
4185 locator : Optional [str ] = None ,
4286 ) -> ColletedTreeTypedDict :
4387 jab_wrapper .switch_window_by_title (window )
44- context_tree = ContextTree (jab_wrapper , search_depth )
45- matches : Union [ContextNode , List [ContextNode ]] = []
46- if locator :
47- from ._locators import find_elements_from_tree
4888
49- matches = find_elements_from_tree (context_tree , locator )
50- return {"matches" : matches , "tree" : context_tree .root }
89+ if not self ._context :
90+ return self ._collect_from_root (jab_wrapper , locator , search_depth )
91+ else :
92+ return self ._collect_from_context (jab_wrapper , locator , search_depth )
0 commit comments