########################################################################### # # Copyright (c) 2019-2021 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file utils.py # # @author (last) LTTS # @date (last) 15-Jan-2022 # ############################################################################ import sys import test import squish import names from configuration import config from builtins import int as pyInt from builtins import str as pyStr from builtins import float as pyFloat from unicodedata import bidirectional def start_application(app_name): """ Function to start application and verify application status [running] If application does not start or running status is false, test stops Argument: @param app_name : (str) - Name of the application @param app_executable : (str) - Actual application @return: handle for the application if the application is in running state, or error (exist the application) """ counter = 0 while True: try: counter += 1 test.log("Starting {}".format(app_name)) squish.startApplication(config.APPLICATION_NAME) if counter == 1: test.log(f"Application launched at the {counter}'st try.") elif counter == 2: test.log(f"Application launched at the {counter}'nd try.") elif counter == 3: test.log(f"Application launched at the {counter}'rd try.") else: test.log(f"Application launched at the {counter}'th try.") break except RuntimeError: if counter == 1: test.log(f"Application failed to launch after {counter} try - Please refer logs") elif counter == 20: test.log(f"Exiting after {counter} tries..") sys.exit(1) else: test.log(f"Application failed to launch after {counter} tries - Please refer logs") except: logErrorDetails("Failed to start the application") sys.exit(1) def check_if_object_is_within_the_container(obj=None, container=None): """ check if an object is inside a container @param obj - child UI object @param container - container UI object @return boolean """ container = squish.findObject(container) containerPos = container.mapToGlobal(squish.QPoint(0, 0)) container_x, container_y = pyInt(containerPos.x), pyInt(containerPos.y) container_width, container_height = pyInt(container.width), pyInt(container.height) obj = squish.findObject(obj) objPos = obj.mapToGlobal(squish.QPoint(0, 0)) obj_x, obj_y = pyInt(objPos.x), pyInt(objPos.y) obj_width, obj_height = pyInt(obj.width), pyInt(obj.height) if obj_x >= container_x and obj_y >= container_y: if (obj_x + obj_width) <= (container_x + container_width) and (obj_y + obj_height) <= (container_y + container_height): return True return False def scroll_to_zone(zone=None, screen_object=None, direction = None): """ scroll to the UI, if object is hidden @param zone - object to be find out. @param screen_object - object of the screen. @return boolean """ counter = 0 while counter <= 100: try: counter += 1 squish.findObject(zone) squish.snooze(0.5) if check_if_object_is_within_the_container(obj=zone, container=screen_object): return True else: raise RuntimeError except RuntimeError: ScreenObj = squish.findObject(screen_object) screenHeight = pyInt(ScreenObj.height) screenWidth = pyInt(ScreenObj.width) if direction is None: squish.mouseWheel(ScreenObj, (screenWidth-100), 107, 0, -(screenHeight-460), squish.Qt.NoModifier) else: squish.mouseWheel(ScreenObj, (screenWidth-100), -(screenHeight-700), 0, 200, squish.Qt.NoModifier) raise LookupError("zone object is not in view to the user after " + \ "trying 100 times") def set_slider_value(slider_value= None, slider_object= None, bidirectional= False, slider_range = None): """ Method to drag slider to desired value @param slider_value : value in which slider should be placed @param slider_object : object of the slider @param bidirectional : True, if slider have 2 handler @return boolean (True - if user able to select slider. else, false) """ slider_value = pyFloat(slider_value) actual_value = slider_value if slider_range == "Max": x_value = 600 else: x_value = 0 counter = 0 while True: if slider_value < 0: slider_value = -1 * slider_value squish.mouseDrag(slider_object, x_value, 0, slider_value, 0, squish.Qt.NoModifier, squish.Qt.LeftButton) if counter == 50: test.fail("User unable to fix slider value") return False if bidirectional is True: if slider_range == "Low": if actual_value == pyFloat(slider_object.minValue): return True if slider_value >= pyFloat(slider_object.minValue): slider_value = slider_value - 30 if slider_value <= pyFloat(slider_object.minValue): slider_value = slider_value + 30 if slider_range == "Max": if actual_value == pyFloat(slider_object.maxValue): return True if slider_value >= pyFloat(slider_object.maxValue): slider_value = slider_value + 30 if slider_value <= pyFloat(slider_object.maxValue): slider_value = slider_value - 30 else: if actual_value == pyFloat(slider_object.value): return True if slider_value >= pyFloat(slider_object.value): slider_value = slider_value + 30 if slider_value <= pyFloat(slider_object.value): slider_value = slider_value - 30 counter = counter + 1 def slider_movement_for_negative_values(slider_value= None, slider_object= None, bidirectional= False, slider_range = None): """ Method to drag slider for negative values value @param slider_value : value in which slider should be placed @param slider_object : object of the slider @param bidirectional : True, if slider have 2 handler @return boolean (True - if user able to select slider. else, false) """ slider_value = pyFloat(slider_value) actual_value = slider_value if slider_range == "Max": x_value = 600 else: x_value = 0 counter = 0 while True: squish.mouseDrag(slider_object, x_value, 0, slider_value, 0, squish.Qt.NoModifier, squish.Qt.LeftButton) if counter == 50: test.fail("User unable to fix slider slider_value") return False if bidirectional is True: if slider_range == "Low": if actual_value == pyFloat(slider_object.minValue): return True if slider_value >= pyFloat(slider_object.minValue): slider_value = slider_value + 30 if slider_value <= pyFloat(slider_object.minValue): slider_value = slider_value - 30 if slider_range == "Max": if actual_value == pyFloat(slider_object.maxValue): return True if slider_value >= pyFloat(slider_object.maxValue): slider_value = slider_value + 30 if slider_value <= pyFloat(slider_object.maxValue): slider_value = slider_value - 30 counter = counter + 1 def logErrorDetails(error_message): """ Logs the execution details. """ excep_type, excep_val, excep_tb = sys.exc_info() test.log(str(excep_val)) test.fail(error_message + ", " + "Exception Type: " + str(excep_type) + "\n" + str(excep_tb))