def __call__(self, *in_args): ''' For output value, use original value For input arguments: 1. If required argument is an enum, check if input argument fit requirement 2. If required argument is "POINTER(IUIAutomationElement)", we accept UIElement object, get required pointer object from UIElement, and send it to function 3. Other, no change ''' args = list(in_args) if len(self.args) != len(args): LOGGER.warn("Input arguments number not match expected") return None for index, expected_arg in enumerate(self.args): expected_arg_type = expected_arg[0] if expected_arg_type == "POINTER(IUIAutomationElement)": #get the UIAElment args[index] = args[index].UIAElement elif expected_arg_type in UIA.UIA_enums: #enum should be an int value, if argument is a string, should translate to int if args[index] in UIA.UIA_enums[expected_arg_type]: args[index] = UIA.UIA_enums[expected_arg_type][args[index]] if args[index] not in list(UIA.UIA_enums[expected_arg_type].values()): LOGGER.debug("Input argument not in expected value: %s" , args[index]) return None return self.function_object(*args)
def __call__(self, *in_args): ''' For output value, use original value For input arguments: 1. If required argument is an enum, check if input argument fit requirement 2. If required argument is "POINTER(IUIAutomationElement)", we accept UIElement object, get required pointer object from UIElement, and send it to function 3. Other, no change ''' args = list(in_args) if len(self.args) != len(args): LOGGER.warn("Input arguments number not match expected") return None for index, expected_arg in enumerate(self.args): expected_arg_type = expected_arg[0] if expected_arg_type == "POINTER(IUIAutomationElement)": #get the UIAElment args[index] = args[index].UIAElement elif expected_arg_type in UIA.UIA_enums: #enum should be an int value, if argument is a string, should translate to int if args[index] in UIA.UIA_enums[expected_arg_type]: args[index] = UIA.UIA_enums[expected_arg_type][args[index]] if args[index] not in list( UIA.UIA_enums[expected_arg_type].values()): LOGGER.debug("Input argument not in expected value: %s", args[index]) return None return self.function_object(*args)
def find_elements(self, parsed_identifier): '''find all matched element root find should only find in the first level to avoid search in all UI ''' if parsed_identifier is None: translated_identifier = UIA.IUIAutomation_object.CreateTrueCondition( ) else: translated_identifier = Translater.ID_Translater( parsed_identifier).get_translated() if translated_identifier[ 0] == "Coordinate" or translated_identifier[0] == "Index": LOGGER.warn( "find_elements method not support find by Coordinate or find by Index" ) return [] else: translated_identifier = translated_identifier[1] scope = UIA.UIA_wrapper.TreeScope_Children UIAElementArray = self.UIAElement.FindAll(scope, translated_identifier) UIElements = [] for i in range(UIAElementArray.Length): UIElements.append(UIElement(UIAElementArray.GetElement(i))) return UIElements
def verify(self): '''verify UI element is still exist ''' flag = True if self.UIAElement == ctypes.POINTER( UIA.UIA_wrapper.IUIAutomationElement)(): flag = False try: UIAElement = self.UIAElement.FindFirst( UIA.UIA_wrapper.TreeScope_Element, UIA.IUIAutomation_object.CreateTrueCondition()) except _ctypes.COMError: flag = False UIAElement = ctypes.POINTER(UIA.UIA_wrapper.IUIAutomationElement)() if UIAElement == ctypes.POINTER( UIA.UIA_wrapper.IUIAutomationElement)(): flag = False if not flag: LOGGER.warn("Current UIAElement is no longer exist") return None return UIElement(UIAElement)
def _translated_atomic_identifier(self, parsed_atomic_id): if parsed_atomic_id[0] in UIA.UIA_automation_element_property_identifers_mapping: return UIA.IUIAutomation_object.CreatePropertyCondition(UIA.UIA_automation_element_property_identifers_mapping[parsed_atomic_id[0]], parsed_atomic_id[1]) elif parsed_atomic_id[0] in UIA.UIA_control_pattern_property_identifiers_mapping: return UIA.IUIAutomation_object.CreatePropertyCondition(UIA.UIA_control_pattern_property_identifiers_mapping[parsed_atomic_id[0]], parsed_atomic_id[1]) else: #use no UIA identifier will be skipped LOGGER.warn("identifier: %s not in UIA property maps" , parsed_atomic_id[0]) return None
def set_focus(self): '''set foucs this element Will bring this element to the front, used by Keyboard, Mouse, Touch Arguments: Returns: ''' try: self.UIAElement.SetFocus() except _ctypes.COMError: LOGGER.warn("SetFocus fail on current element, maybe due to this element not support SetFocus")
def set_focus(self): '''set foucs this element Will bring this element to the front, used by Keyboard, Mouse, Touch Arguments: Returns: ''' try: self.UIAElement.SetFocus() except _ctypes.COMError: LOGGER.warn( "SetFocus fail on current element, maybe due to this element not support SetFocus" )
def _translated_atomic_identifier(self, parsed_atomic_id): if parsed_atomic_id[ 0] in UIA.UIA_automation_element_property_identifers_mapping: return UIA.IUIAutomation_object.CreatePropertyCondition( UIA.UIA_automation_element_property_identifers_mapping[ parsed_atomic_id[0]], parsed_atomic_id[1]) elif parsed_atomic_id[ 0] in UIA.UIA_control_pattern_property_identifiers_mapping: return UIA.IUIAutomation_object.CreatePropertyCondition( UIA.UIA_control_pattern_property_identifiers_mapping[ parsed_atomic_id[0]], parsed_atomic_id[1]) else: #use no UIA identifier will be skipped LOGGER.warn("identifier: %s not in UIA property maps", parsed_atomic_id[0]) return None
def _find_by_index(self, translated_identifier, scope=UIA.UIA_wrapper.TreeScope_Descendants): if isinstance(translated_identifier, tuple) and len(translated_identifier) == 2: identifier = translated_identifier[0] index = translated_identifier[1] elif isinstance(translated_identifier, int): identifier = UIA.IUIAutomation_object.CreateTrueCondition() index = translated_identifier else: LOGGER.warn("Index identifier is wrong, get %s" , repr(translated_identifier)) return None target_UIAElements = self.UIAElement.FindAll(scope, identifier) if index+1 > target_UIAElements.Length: LOGGER.warn("Find %d matched elements, index:%d out of range", target_UIAElements.Length, index) return None return UIElement(target_UIAElements.GetElement(index))
def find_elements(self, parsed_identifier): '''find all matched element root find should only find in the first level to avoid search in all UI ''' if parsed_identifier is None: translated_identifier = UIA.IUIAutomation_object.CreateTrueCondition() else: translated_identifier = Translater.ID_Translater(parsed_identifier).get_translated() if translated_identifier[0] == "Coordinate" or translated_identifier[0] == "Index": LOGGER.warn("find_elements method not support find by Coordinate or find by Index") return [] else: translated_identifier = translated_identifier[1] scope = UIA.UIA_wrapper.TreeScope_Children UIAElementArray = self.UIAElement.FindAll(scope, translated_identifier) UIElements = [] for i in range(UIAElementArray.Length): UIElements.append(UIElement(UIAElementArray.GetElement(i))) return UIElements
def verify(self): '''verify UI element is still exist ''' flag = True if self.UIAElement == ctypes.POINTER(UIA.UIA_wrapper.IUIAutomationElement)(): flag = False try: UIAElement = self.UIAElement.FindFirst(UIA.UIA_wrapper.TreeScope_Element, UIA.IUIAutomation_object.CreateTrueCondition()) except _ctypes.COMError: flag = False UIAElement = ctypes.POINTER(UIA.UIA_wrapper.IUIAutomationElement)() if UIAElement == ctypes.POINTER(UIA.UIA_wrapper.IUIAutomationElement)(): flag = False if not flag: LOGGER.warn("Current UIAElement is no longer exist") return None return UIElement(UIAElement)
def _find_by_index(self, translated_identifier, scope=UIA.UIA_wrapper.TreeScope_Descendants): if isinstance(translated_identifier, tuple) and len(translated_identifier) == 2: identifier = translated_identifier[0] index = translated_identifier[1] elif isinstance(translated_identifier, int): identifier = UIA.IUIAutomation_object.CreateTrueCondition() index = translated_identifier else: LOGGER.warn("Index identifier is wrong, get %s", repr(translated_identifier)) return None target_UIAElements = self.UIAElement.FindAll(scope, identifier) if index + 1 > target_UIAElements.Length: LOGGER.warn("Find %d matched elements, index:%d out of range", target_UIAElements.Length, index) return None return UIElement(target_UIAElements.GetElement(index))
def check_app_map(XSD_file, app_map_file): XSD_module_name = os.path.basename(XSD_file).split(".")[0] XSD_module_dir = os.path.dirname(XSD_file) generate_module(XSD_file, XSD_module_name, XSD_module_dir) sys.path.insert(0, XSD_module_dir) XSD_module = __import__(XSD_module_name) with open(app_map_file) as app_map: try: XSD_module.CreateFromDocument(app_map.read()) LOGGER.debug("Check successful") except pyxb.UnrecognizedContentError as e: LOGGER.warn(e.details()) except pyxb.IncompleteElementContentError as e: LOGGER.warn(e.details()) except pyxb.ValidationError as e: LOGGER.warn(e.details())