def is_button_enabled_disabled(
    ElemId=None, ElemPath=None, ElemRole=None, ElemValue=None, ElemPosition=None
):
    try:
        sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
        ElementName = [
            Key
            for Key, Value in Global.Ids.items()
            if Value == ElemId or Value == ElemPath
        ]
        if ElemPath == None:
            element = Find_Element(
                elem_id=ElemId,
                elem_path=ElemPath,
                elem_role=ElemRole,
                elem_value=ElemValue,
                elem_position=ElemPosition,
            )
        else:
            element = [ElemPath]
        if element != False:
            enabled_disabled = element[0].enabled.get()
            return enabled_disabled
        else:
            return "Critical"

    except Exception as e:
        return CommonUtil.LogCriticalException(sModuleInfo, e)
def is_check_box_checked_unchecked(
    ElemId=None, ElemPath=None, ElemRole=None, ElemValue=None, ElemPosition=None
):
    try:
        sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
        ElementName = [
            Key
            for Key, Value in Global.Ids.items()
            if Value == ElemId or Value == ElemPath
        ]
        element = Find_Element(
            elem_id=ElemId,
            elem_path=ElemPath,
            elem_role=ElemRole,
            elem_value=ElemValue,
            elem_position=ElemPosition,
        )
        if element != False and len(element) > 0:
            status = element[0].value.get()
            return status
        else:
            return "Critical"

    except Exception as e:
        return CommonUtil.LogCriticalException(sModuleInfo, e)
def get_all_obj_main_win(MenuOrWindow=None):
    try:
        sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

        all_obj = []
        if MenuOrWindow == None:
            MainItems = (
                app(u"System Events").processes[u"ProductName"].UI_elements.get()
            )
            for eachWind in MainItems:
                try:
                    if (
                        eachWind.attributes[u"AXIdentifier"].value.get() == "mainWindow"
                    ):  #'_NS:6':
                        parent_ID = eachWind.UI_elements.get()
                except:
                    pass
        elif MenuOrWindow == "toast":
            MainItems = (
                app(u"System Events").processes[u"ProductName"].UI_elements.get()
            )
            for eachWind in MainItems:
                try:
                    if eachWind.title.get() == "Window":  #'_NS:105':
                        parent_ID = eachWind.UI_elements.get()
                except:
                    pass
        else:
            parent_ID = (
                app(u"System Events")
                .processes[u"ProductName"]
                .menu_bars[1]
                .UI_elements.get()
            )

        # parent =  get_child_obj(parent_ID)
        all_obj = all_obj + parent_ID
        for i in range(13):
            Child = get_child_obj(parent_ID)
            if Child != None and Child != "Critical":
                all_obj = all_obj + Child
            parent_ID = Child
        #        child_1 = get_child_obj(parent)
        #        child_2 = get_child_obj(child_1)
        #        child_3 = get_child_obj(child_2)
        #        child_4 = get_child_obj(child_3)
        #        child_5 = get_child_obj(child_4)
        #        child_6 = get_child_obj(child_5)
        #        child_7 = get_child_obj(child_6)
        #
        #        all_obj = parent_ID + parent + child_1 + child_2 + child_3 + child_4 + child_5 + child_6 + child_7
        total_obj_count = len(all_obj)
        #        print("%s > Total Elements Found (%s)" %(sModuleInfo,total_obj_count))
        #        CommonUtil.ExecLog(sModuleInfo,"Total Elements Found (%s)" %total_obj_count,1)
        Globals.Object_List = all_obj
        return all_obj

    except Exception as e:
        return CommonUtil.LogCriticalException(sModuleInfo, e)
def is_app_running(app_name):
    try:
        sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

        process_list = app(u"System Events").processes.name.get()
        if app_name in process_list:
            return True
        else:
            return False

    except Exception as e:
        return CommonUtil.LogCriticalException(sModuleInfo, e)
def click_button(
    ElemId=None, ElemPath=None, ElemRole=None, ElemValue=None, ElemPosition=None
):
    try:
        sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
        ElementName = [
            Key
            for Key, Value in Global.Ids.items()
            if Value == ElemId or Value == ElemPath
        ]
        if ElemPath == None:
            element = Find_Element(
                elem_id=ElemId,
                elem_path=ElemPath,
                elem_role=ElemRole,
                elem_value=ElemValue,
                elem_position=ElemPosition,
            )
        else:
            element = [ElemPath]

        if element != False:
            if element[0].enabled.get() == True:
                element[0].click()
                #                print("%s > Button (%s) has been clicked" %(sModuleInfo,ElementName))
                CommonUtil.ExecLog(
                    sModuleInfo, "Button (%s) has been clicked" % ElementName, 1
                )
                return True
            else:
                #                print("%s > Button (%s) was not enabled" %(sModuleInfo,ElementName))
                CommonUtil.ExecLog(
                    sModuleInfo, "Button (%s) was not enabled" % ElementName, 1
                )
                return False
        else:
            #            print("%s > Button (%s) was not be found" %(sModuleInfo,ElementName))
            CommonUtil.ExecLog(
                sModuleInfo, "Button (%s) was not be found" % ElementName, 1
            )
            return False

    except Exception as e:
        return CommonUtil.LogCriticalException(sModuleInfo, e)
def wait_for_obj_to_appear(
    ElemId=None,
    ElemPath=None,
    ElemRole=None,
    ElemValue=None,
    ElemPosition=None,
    Timeout=15,
):
    try:
        sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

        ElementName = [
            Key
            for Key, Value in Global.Ids.items()
            if Value == ElemId or Value == ElemPath
        ]
        element = Find_Element(
            elem_id=ElemId,
            elem_path=ElemPath,
            elem_role=ElemRole,
            elem_value=ElemValue,
            elem_position=ElemPosition,
        )
        wait = 0
        while element == [] or element == False:
            time.sleep(1)
            wait = wait + 1
            if wait == Timeout:
                #                print("%s > Timeout: Element (%s) was not found in (%s) seconds!" %(sModuleInfo,ElementName,wait))
                #                CommonUtil.ExecLog(sModuleInfo,"Timeout: Element (%s) was not found in (%s) seconds!" %(ElementName,wait),3)
                return False
            element = Find_Element(
                elem_id=ElemId,
                elem_role=ElemRole,
                elem_value=ElemValue,
                elem_position=ElemPosition,
            )
        #        print("%s > Element (%s) was found in (%s) seconds!" %(sModuleInfo,ElementName,wait))
        #        CommonUtil.ExecLog(sModuleInfo,"Element (%s) was found in (%s) seconds!" %(ElementName,wait),3)
        return True

    except Exception as e:
        return CommonUtil.LogCriticalException(sModuleInfo, e)
def check_uncheck_box(
    ElemId=None,
    ElemPath=None,
    check_uncheck=None,
    ElemRole=None,
    ElemValue=None,
    ElemPosition=None,
):
    try:
        sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

        element = Find_Element(
            elem_id=ElemId,
            elem_path=ElemPath,
            elem_role=ElemRole,
            elem_value=ElemValue,
            elem_position=ElemPosition,
        )
        if element != False and len(element) > 0:
            status = element[0].value.get()
            if check_uncheck == 1:
                if status == 1:
                    return True
                else:
                    element[0].click()

            elif check_uncheck == 0:
                if status == 0:
                    return True
                else:
                    element[0].click()

            status = element[0].value.get()
            if status == check_uncheck:
                return True
            else:
                return False
        else:
            return False

    except Exception as e:
        return CommonUtil.LogCriticalException(sModuleInfo, e)
def Run_Application(app_name):
    try:
        sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

        if is_app_running(app_name) == False:
            app(app_name).activate()
            count = 0
            while count != 8:
                time.sleep(0.5)
                count = count + 1

                if is_app_running(app_name) == True:
                    return True
                elif count == 8:
                    return "Timeout"
        else:
            return True

    except Exception as e:
        return CommonUtil.LogCriticalException(sModuleInfo, e)
def get_child_obj(obj_list):
    try:
        sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
        get_child_obj = []
        if obj_list != None:
            obj_list_size = len(obj_list)
            for i in range(obj_list_size):
                obj = obj_list[i]
                try:
                    child_obj = obj.attributes[u"AXChildren"].value.get()
                    if child_obj != []:
                        get_child_obj = get_child_obj + child_obj

                except:
                    pass
        #                            Obj_without_child = []
        #                            Obj_without_child.append(obj)
        #                            get_child_obj = get_child_obj + Obj_without_child
        if get_child_obj != []:
            return get_child_obj
        else:
            return None
    except Exception as e:
        return CommonUtil.LogCriticalException(sModuleInfo, e)
def Find_Element(
    elem_id=None,
    elem_path=None,
    elem_role=None,
    elem_value=None,
    elem_position=None,
    MenuOrWindow=None,
):
    try:
        sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

        Element = [
            Key
            for Key, Value in Global.Ids.items()
            if Value == elem_id or Value == elem_path
        ]
        for N in Element:
            if "DeviceMenu" in N:
                MenuOrWindow = "Menu"

        @logger
        def Find(obj_list, elem_id, elem_path):
            for obj_temp in obj_list:
                obj_id_temp = False
                try:
                    obj_id_temp = obj_temp.attributes[u"AXIdentifier"].value.get()
                except:
                    a = 1
                if str(obj_temp) == str(elem_path):
                    obj_id_list.append(obj_temp)
                    elem_id = elem_path
                if str(CommonUtil.to_unicode(obj_id_temp)) == elem_id:
                    obj_id_list.append(obj_temp)
            return obj_id_list

        ###If length of found elements is zero###

        obj_id_list = []

        obj_list = Globals.Object_List
        obj_id_list = Find(obj_list, elem_id, elem_path)
        if obj_id_list == []:

            # print("Calling get_all_obj function")
            obj_list = get_all_obj_main_win(MenuOrWindow)
            # print(obj_list)
            obj_id_list = Find(obj_list, elem_id, elem_path)

        ObjsLen = len(obj_id_list)
        if ObjsLen == 0:
            # print("%s > Object of id/path (%s) not found!" %(sModuleInfo,Element))
            # CommonUtil.ExecLog(sModuleInfo,"Object of id/pth (%s) not found!" %(Element),3)
            return False

        ###If length of found elements is one###
        if ObjsLen == 1 or ObjsLen > 1:
            # print("%s > (%s) Object/s of id (%s) found!" %(sModuleInfo,ObjsLen,Element))
            # CommonUtil.ExecLog(sModuleInfo,"(%s) Object/s of id (%s) found!" %(ObjsLen,Element),1)
            FoundObj = []
            if elem_role != None:
                for eachElem in obj_id_list:
                    Attribute = get_role_of_element(eachElem)
                    if Attribute == elem_role:
                        FoundObj.append(eachElem)
                        return FoundObj

            elif elem_value != None:
                for eachElem in obj_id_list:
                    Attribute = get_value_of_element(eachElem)
                    if Attribute == elem_role:
                        FoundObj.append(eachElem)
                        return FoundObj

            if (elem_role != None or elem_value != None) and (FoundObj == []):
                return FoundObj
            else:

                return obj_id_list

    except Exception as e:
        return CommonUtil.LogCriticalException(sModuleInfo, e)