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 8a28df1
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 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,14 +525,23 @@ def find_anything(
case_sensitive: bool = False,
exact_match: bool = False,
return_first: bool = False,
caching: bool = True,
**kwargs) -> list:
"""fuzzy search through everything
find all tags
find all matches within meta data
"""
logger.debug(
f'find_anything :: {match=} :: {value=} :: {by=} : {case_sensitive=} :: {exact_match=} :: {return_first=} :: {kwargs=}')
f'find_anything :: '
f'{match=} :: '
f'{value=} :: '
f'{by=} : {case_sensitive=} :: '
f'{exact_match=} :: '
f'{return_first=} :: '
f'{caching=} :: '
f' {kwargs=}'
)

by_types = [
self.by.TAG_NAME,
Expand All @@ -553,7 +563,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[by_][value]

for element in elements:
dirs = dir(element)
dir_meta = []
Expand Down Expand Up @@ -922,7 +946,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 +971,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 +989,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 +1011,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 +1029,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 +1051,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 8a28df1

Please sign in to comment.