From 2ed7d3e587b1558d419c9d15430b845c77c9ffc1 Mon Sep 17 00:00:00 2001 From: PodolyanVladimir Date: Wed, 13 Sep 2023 12:15:45 +0300 Subject: [PATCH 1/3] first version of elements cache --- dyatel/dyatel_sel/core/core_element.py | 47 ++++++++++++++++++-------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/dyatel/dyatel_sel/core/core_element.py b/dyatel/dyatel_sel/core/core_element.py index 103c5da9..330ec653 100644 --- a/dyatel/dyatel_sel/core/core_element.py +++ b/dyatel/dyatel_sel/core/core_element.py @@ -34,6 +34,23 @@ ) +def drop_element_cache(obj): + obj._element = None + if obj.parent: + drop_element_cache(obj.parent) + + +def refetch(func): + + def decorator(*args, **kwargs): + try: + return func(*args, **kwargs) + except: + drop_element_cache(args[0]) + return func(*args, **kwargs) + return decorator + + class CoreElement(ElementABC, ABC): parent: Union[ElementABC, CoreElement] @@ -350,7 +367,7 @@ def is_displayed(self, silent: bool = False) -> bool: is_displayed = self.is_available() if is_displayed: - is_displayed = safe_call(self._cached_element.is_displayed) + is_displayed = safe_call(self.element.is_displayed) return is_displayed @@ -480,20 +497,18 @@ def _get_element(self, wait: Union[bool, Callable] = True, force_wait: bool = Fa :param force_wait: force wait for some element :return: SeleniumWebElement """ - element = self._element + element = None if wait is True: wait = self.wait_element - if not element: - - # Try to get element instantly without wait. Skipped if force_wait given - if not force_wait: - element = safe_call(self._find_element, wait_parent=False) + # Try to get element instantly without wait. Skipped if force_wait given + if not force_wait: + element = safe_call(self._find_element, wait_parent=False) - # Wait for element if it is not found instantly - if (not element and wait) or force_wait: - element = self._get_cached_element(safe_call(wait, silent=True)) + # Wait for element if it is not found instantly + if (not element and wait) or force_wait: + element = self._get_cached_element(safe_call(wait, silent=True)) if not element: if self.parent and not self._get_cached_element(self.parent): @@ -527,6 +542,7 @@ def _get_base(self, wait: Union[bool, Callable] = True) -> Union[SeleniumWebDriv return base + @refetch def _find_element(self, wait_parent: bool = False) -> Union[SeleniumWebElement, AppiumWebElement]: """ Find selenium/appium element @@ -535,11 +551,13 @@ def _find_element(self, wait_parent: bool = False) -> Union[SeleniumWebElement, :return: SeleniumWebElement or AppiumWebElement """ base = self._get_base(wait=wait_parent) - self._cached_element = None + + if self._element: + return self._element try: element = base.find_element(self.locator_type, self.locator) - self._cached_element = element + self._element = element return element except (SeleniumInvalidArgumentException, SeleniumInvalidSelectorException) as exc: self._raise_invalid_selector_exception(exc) @@ -554,13 +572,12 @@ def _find_elements(self, wait_parent: bool = False) -> List[Union[SeleniumWebEle :return: list of SeleniumWebElement or AppiumWebElement """ base = self._get_base(wait=wait_parent) - self._cached_element = None try: elements = base.find_elements(self.locator_type, self.locator) if elements: - self._cached_element = elements[0] + self._element = elements[0] return elements except (SeleniumInvalidArgumentException, InvalidSelectorException) as exc: @@ -600,4 +617,4 @@ def _get_cached_element(self, obj: CoreElement) -> Union[None, SeleniumWebElemen :param obj: CoreElement object :return: None, SeleniumWebElement, AppiumWebElement """ - return getattr(obj, '_cached_element', None) + return getattr(obj, '_element', None) From c2790f227f6ec527bc6979717a17a6eb7065ba3a Mon Sep 17 00:00:00 2001 From: VladimirPodolian <36446855+VladimirPodolian@users.noreply.github.com> Date: Mon, 13 Jan 2025 03:02:17 +0100 Subject: [PATCH 2/3] Test commit --- mops/selenium/core/core_element.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mops/selenium/core/core_element.py b/mops/selenium/core/core_element.py index e159ab92..72e92976 100644 --- a/mops/selenium/core/core_element.py +++ b/mops/selenium/core/core_element.py @@ -678,4 +678,4 @@ def _get_cached_element(self, obj: CoreElement) -> Union[None, SeleniumWebElemen :param obj: CoreElement object :return: None, SeleniumWebElement, AppiumWebElement """ - return getattr(obj, '_element', None) + return getattr(obj, "_element", None) From 52716b8f61df58f99c128dd5a84a0294781f0b8a Mon Sep 17 00:00:00 2001 From: VladimirPodolian <36446855+VladimirPodolian@users.noreply.github.com> Date: Mon, 13 Jan 2025 03:19:12 +0100 Subject: [PATCH 3/3] Test commit --- mops/selenium/core/core_element.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mops/selenium/core/core_element.py b/mops/selenium/core/core_element.py index 72e92976..e159ab92 100644 --- a/mops/selenium/core/core_element.py +++ b/mops/selenium/core/core_element.py @@ -678,4 +678,4 @@ def _get_cached_element(self, obj: CoreElement) -> Union[None, SeleniumWebElemen :param obj: CoreElement object :return: None, SeleniumWebElement, AppiumWebElement """ - return getattr(obj, "_element", None) + return getattr(obj, '_element', None)