From 48458fd0e985da646a2d36c63b26e2cd30a5dd2a Mon Sep 17 00:00:00 2001 From: Andreas Sekulski Date: Fri, 1 Mar 2024 11:04:53 +0100 Subject: [PATCH] #160 Property exception handling --- TestApplications/TestApplications.sln | 4 +- src/FlaUILibrary/flaui/module/element.py | 52 +++++++++++++++++++++--- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/TestApplications/TestApplications.sln b/TestApplications/TestApplications.sln index acb2266..54aa31e 100644 --- a/TestApplications/TestApplications.sln +++ b/TestApplications/TestApplications.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30011.22 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34607.119 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfApplication", "src\WpfApplication\WpfApplication.csproj", "{C8276299-FA43-409B-A969-EF030AB56224}" EndProject diff --git a/src/FlaUILibrary/flaui/module/element.py b/src/FlaUILibrary/flaui/module/element.py index f21a520..6c8145b 100644 --- a/src/FlaUILibrary/flaui/module/element.py +++ b/src/FlaUILibrary/flaui/module/element.py @@ -2,7 +2,8 @@ from enum import Enum from typing import Optional, Any from System import Exception as CSharpException # pylint: disable=import-error -from FlaUI.Core import Debug as FlaUIDebug # pylint: disable=import-error +from FlaUI.Core import Debug as FlaUIDebug # pylint: disable=import-error +from FlaUI.Core.Exceptions import PropertyNotSupportedException # pylint: disable=import-error from FlaUILibrary.flaui.util.converter import Converter from FlaUILibrary.flaui.exception import FlaUiError from FlaUILibrary.flaui.interface import (ModuleInterface, ValueContainer) @@ -61,7 +62,7 @@ def __init__(self, automation: Any, timeout: int = 1000): self._timeout = timeout @staticmethod - def create_value_container(name=None, xpath=None, retries=None, use_exception=None, msg=None): + def create_value_container(name=None, xpath=None, retries=None, use_exception=None, msg=None): """ Helper to create container object. @@ -98,7 +99,7 @@ def execute_action(self, action: Action, values: Container): lambda: self._get_name_from_element(values["xpath"]), self.Action.GET_ELEMENT_RECTANGLE_BOUNDING: lambda: self._get_rectangle_bounding_from_element( - values["xpath"]), + values["xpath"]), self.Action.IS_ELEMENT_ENABLED: lambda: self._get_element(values["xpath"]).IsEnabled, self.Action.NAME_SHOULD_BE: @@ -229,9 +230,9 @@ def _find_all_elements(self, xpath: str): elements = self._get_all_elements_by_xpath(xpath) for element in elements: values.append(AutomationElement( - element.AutomationId, - element.Name, - element.ClassName, + self._try_get_automation_id_property(element), + self._try_get_name_property(element), + self._try_get_classname_property(element), FlaUIDebug.GetXPathToElement(element) )) @@ -456,3 +457,42 @@ def _set_timeout(self, timeout: int): timeout (Number): Timeout value in seconds """ self._timeout = timeout + + @staticmethod + def _try_get_automation_id_property(element): + """ + Try to get automation id property from element. Return empty string if failed. + + Args: + element (UIA): AutomationElement. + """ + try: + return element.AutomationId + except PropertyNotSupportedException: + return "" + + @staticmethod + def _try_get_name_property(element): + """ + Try to get name property from element. Return empty string if failed. + + Args: + element (UIA): AutomationElement. + """ + try: + return element.Name + except PropertyNotSupportedException: + return "" + + @staticmethod + def _try_get_classname_property(element): + """ + Try to get class name property from element. Return empty string if failed. + + Args: + element (UIA): AutomationElement. + """ + try: + return element.ClassName + except PropertyNotSupportedException: + return ""