########################################################################### # # 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) Joseph varghese # @date (last) 15-Jan-2022 # ############################################################################ import sys import csv import os import test import glob import squish from dialin.ui.hd_simulator import HDSimulator from builtins import int as pyInt from builtins import int as pyInt from builtins import format from datetime import datetime from dialin.common.msg_ids import MsgIds hd_simulator = HDSimulator() LOG_LOCATION = "/home/denali/Desktop/sd-card/log/*.log" 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(app_name) if counter == 1: test.log("Application launched at the "+str(counter)+" st try.") elif counter == 2: test.log("Application launched at the "+str(counter)+" nd try.") elif counter == 3: test.log("Application launched at the "+str(counter)+" rd try.") else: test.log("Application launched at the "+str(counter)+" th try.") break except RuntimeError: if counter == 1: test.log("Application failed to launch after "+str(counter)+" try - Please refer logs") elif counter == 20: test.log("Exiting after "+str(counter)+ " tries..") sys.exit(1) else: test.log("Application failed to launch after "+str(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 true/false """ 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): """ scroll to the numeric if object is hidden @param zone - UI object @param screen_object - UI object (UI Home screen = waveforms + numerics) @return boolean true/false """ 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.waitForObject(screen_object) screenHeight = pyInt(ScreenObj.height) screenWidth = pyInt(ScreenObj.width) squish.mouseWheel(ScreenObj, screenWidth-1000, screenHeight-10, 0, -50, squish.Qt.NoModifier) raise LookupError("zone object is not in view to the user after " + \ "trying 100 times") def get_extracted_file(): """ This function is the handler for getting file from log folder. This handler will go inside log folder and looks for newly added log based on current time. if it satisfied that condition, it will return the exact path of newly created log. @return latest_file - (string) returns latest file that append on log folder from sd-data """ try: list_of_files = glob.glob(LOG_LOCATION) latest_file = max(list_of_files, key=os.path.getctime) return latest_file except: test.fail("log file is not created during application interaction") return False def get_message_from_log(file_name, message_text): """ This method intended to extract the message from application log. The handler find a match between time displayed on log and occurrences time of event to extract the details. For row[index], index represent column to be extracted. @param file_name - (string) path of the latest log file created. @param message_text - (string) message text to be extracted. @return message - (string) binary message for the event. """ message = "" # Message binaries appending with for easy identification try: with open(file_name, 'r') as csv_file: try: for row in reversed(list(csv.reader(csv_file))): if row[0].isalpha(): pass else: row_length = sum(1 for values in row) if row_length >= 4: if row[3]!= None and row[3] == message_text: message_length = sum(1 for values in row) for column in range((message_length-1), 3, -1): message = row[column] + message return message else: pass except: test.fail("application log data is corrupted") except: test.fail("application log is protected") def get_ack_request_details(file_name, message_text): """ This method intended to extract the ack request from application log. The handler find a match between time displayed on log and occurrences time of event along with message id to extract the request acknowledgement. For row[index], were index represent column to be extracted. @param file_name - (string) path of the latest log file created. @return row[3] - (string) acknowledgement request status. @return row[4] - (string) Negative requested acknowledgement value (Sq) """ try: message_id = " ID" with open(file_name, 'r') as csv_file: try: for row in reversed(list(csv.reader(csv_file))): if row[0].isalpha(): pass else: row_length = sum(1 for values in row) if row_length == 6 and row[3] != message_text: if row[5].split(':')[0] == message_id: extracted_message_id = pyInt(row[5].split(':')[1], 16) formatted_message_id = format(extracted_message_id, '#0x') return row[3], row[4], formatted_message_id.replace("00", "") else: pass except: test.fail("application log data is corrupted - unable to fetch data") except: test.fail("application log is protected") def get_bak_request_details(file_name, ack_bak_value): """ This method intended to extract the ack from application log. The handler find a match between time displayed on log and occurrences time of event along with message id to extract the request acknowledgement. For row[index], were index represent column to be extracted. @param file_name - (string) path of the latest log file created. @param message_id - (string) message text to be extracted. @param ack_bak_value - (string) Positive back acknowledgement value (Sq). @return row[3] - (string) acknowledgement back status. """ try: with open(file_name, 'r') as csv_file: try: for row in reversed(list(csv.reader(csv_file))): if row[0].isalpha(): pass else: row_length = sum(1 for values in row) log_time = row[0].split(':') if row_length >= 5: if row[4] == ack_bak_value: return row[3] else: pass else: pass except: test.fail("application log data is corrupted") except: test.fail("application log is protected") def get_current_log_details(message_ack = None, message_text = None): """ This function is capable to perform data analysis from application log folder. logs are automatically created in path :"/home/denali/Desktop/sd-card/log/*.log". In row[index], index represent column to be extracted. @param message_ack - (string) 'Y' - if ack is satisfied in log / 'N' - if ack condition is not satisfied @param message_text - (string) message text to be extracted from log. @param message_id - (string) message id to be extracted from log. @return content_record - (list) list contains extracted data (ack_req, ack_bak, message). """ content_record = [] file_name = get_extracted_file() if message_text != None: message = get_message_from_log(file_name, message_text) content_record.append(message) if message_ack != None: ack_req_status, ack_req_value, message_id = get_ack_request_details(file_name, message_text) ack_bak_value = ack_req_value.replace(":-", ":") # getting negative requested ack from positive requested ack content_record.append(ack_req_status) content_record.append(message_id.lower()) if message_ack != None and ack_bak_value != None: ack_bak_status = get_bak_request_details(file_name, ack_bak_value) content_record.append(ack_bak_status) return content_record