Skip to content

Commit

Permalink
selenium: add caching for find_anything
Browse files Browse the repository at this point in the history
  • Loading branch information
naisanzaa committed Oct 7, 2024
1 parent 3a56c30 commit 746e049
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions automon/integrations/seleniumWrapper/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, config: SeleniumConfig = None):
self.autosaved = None

self.logs = {}
self.cache = {}

def __repr__(self):
try:
Expand Down Expand Up @@ -524,6 +525,7 @@ def find_anything(
case_sensitive: bool = False,
exact_match: bool = False,
return_first: bool = False,
caching: bool = True,
**kwargs) -> list:
"""fuzzy search through everything
Expand Down Expand Up @@ -553,7 +555,21 @@ def find_anything(
MATCHED = []

for by_ in by_types:
elements = self.find_elements(value=value, by=by_, **kwargs)
# try caching the elements
if caching:
if by_ not in self.cache.keys():
self.cache[by_] = {}
self.cache[by_][value] = self.find_elements(value=value, by=by_, **kwargs)

elif value not in self.cache[by_].keys():
self.cache[by_][value] = self.find_elements(value=value, by=by_, **kwargs)

else:
self.cache[by_] = {}
self.cache[by_][value] = self.find_elements(value=value, by=by_, **kwargs)

elements = self.cache[value]

for element in elements:
dirs = dir(element)
dir_meta = []
Expand Down Expand Up @@ -922,7 +938,7 @@ def wait_for_anything(

logger.debug(
f'wait_for_anything :: '
f'timeout {timeout_elapsed}/{timeout} :: '
f'timeout {timeout_elapsed}/{timeout} sec :: '
f'{self.current_url=} :: '
f'{value=} :: '
f'{by=}'
Expand All @@ -947,7 +963,6 @@ def wait_for_anything(
logger.error(f'wait_for_anything :: failed :: {error=} :: {match=} :: {value=} :: {by=}')

timeout_elapsed = round(abs(timeout_start - time.time()), 1)
logger.debug(f'wait_for_anything :: {timeout_elapsed} seconds elapsed')

raise ElementNotFoundException(f'wait_for_anything :: failed :: {match=} :: {value=} :: {by=}')

Expand All @@ -966,7 +981,12 @@ def wait_for_element(
while timeout_elapsed < timeout:

logger.debug(
f'wait_for_element :: {f"{timeout_elapsed}/{timeout}"} :: {by=} :: {self.current_url} :: {value=}')
f'wait_for_element :: '
f'timeout {timeout_elapsed}/{timeout} sec :: '
f'{by=} :: '
f'{self.current_url} :: '
f'{value=}'
)

try:
find = self.find_element(
Expand All @@ -983,7 +1003,6 @@ def wait_for_element(
logger.error(f'wait_for_element :: failed :: {error=} :: {value=} :: {by=}')

timeout_elapsed = round(abs(timeout_start - time.time()), 1)
logger.debug(f'wait_for_element :: {timeout_elapsed} seconds elapsed')

raise ElementNotFoundException(f'wait_for_element :: failed :: {value=} :: {by=}')

Expand All @@ -1002,7 +1021,12 @@ def wait_for_elements(
while timeout_elapsed < timeout:

logger.debug(
f'wait_for_element :: {f"{timeout_elapsed}/{timeout}"} :: {by=} :: {self.current_url} :: {value=}')
f'wait_for_element :: '
f'timeout {timeout_elapsed}/{timeout} sec :: '
f'{by=} :: '
f'{self.current_url} :: '
f'{value=}'
)

try:
find = self.find_elements(
Expand All @@ -1019,7 +1043,6 @@ def wait_for_elements(
logger.error(f'wait_for_elements :: failed :: {error=} :: {value=} :: {by=}')

timeout_elapsed = round(abs(timeout_start - time.time()), 1)
logger.debug(f'wait_for_elements :: {timeout_elapsed} seconds elapsed')

raise ElementNotFoundException(f'wait_for_elements :: failed :: {value=} :: {by=}')

Expand Down

0 comments on commit 746e049

Please sign in to comment.