From 9ca66a4c1b9397139449381b14a49c3ead62e359 Mon Sep 17 00:00:00 2001 From: Muntasib-creator Date: Thu, 4 Feb 2021 20:34:05 +0600 Subject: [PATCH 1/3] Renamed ss --- .../Sequential_Actions/sequential_actions.py | 1 + Framework/MainDriverApi.py | 2 +- Framework/Utilities/CommonUtil.py | 27 ++++++++----------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/Framework/Built_In_Automation/Sequential_Actions/sequential_actions.py b/Framework/Built_In_Automation/Sequential_Actions/sequential_actions.py index f41e9fb7a..26dc2e103 100755 --- a/Framework/Built_In_Automation/Sequential_Actions/sequential_actions.py +++ b/Framework/Built_In_Automation/Sequential_Actions/sequential_actions.py @@ -725,6 +725,7 @@ def Run_Sequential_Actions( if test_action_info: Action_name = ": '" + test_action_info[dataset_cnt]["Action name"] + "'" Action_disabled = test_action_info[dataset_cnt]["Action disabled"] + CommonUtil.current_action_no = str(dataset_cnt + 1) else: Action_name = "" Action_disabled = False diff --git a/Framework/MainDriverApi.py b/Framework/MainDriverApi.py index d1004c122..b222008c5 100644 --- a/Framework/MainDriverApi.py +++ b/Framework/MainDriverApi.py @@ -946,7 +946,7 @@ def run_all_test_steps_in_a_test_case( run_id + "|" + test_case + "|" + str(current_step_id) + "|" + str(current_step_sequence), temp_ini_file, ) - + CommonUtil.current_step_no = str(current_step_sequence) # add log log_line = "STEP #%d: %s" % (StepSeq, current_step_name) print("-"*len(log_line)) diff --git a/Framework/Utilities/CommonUtil.py b/Framework/Utilities/CommonUtil.py index 2d6c8e6ef..543da100c 100644 --- a/Framework/Utilities/CommonUtil.py +++ b/Framework/Utilities/CommonUtil.py @@ -104,6 +104,11 @@ previous_log_line = None teardown = True +current_action_no = "" +current_action_name = "" +current_step_no = "" +current_step_name = "" + executor = concurrent.futures.ThreadPoolExecutor() all_threads = {} @@ -641,7 +646,9 @@ def TakeScreenShot(function_name, local_run=False): "********** Capturing Screenshot for Action: %s Method: %s **********" % (function_name, Method), 4, ) - thread = executor.submit(Thread_ScreenShot, function_name, image_folder, Method, Driver) + image_name = "Step#" + current_step_no + "_Action#" + current_action_no + "_" + str(function_name) + print(image_name) + thread = executor.submit(Thread_ScreenShot, function_name, image_folder, Method, Driver, image_name) SaveThread("screenshot", thread) except: @@ -655,7 +662,7 @@ def pil_image_to_bytearray(img): return img_byte_array -def Thread_ScreenShot(function_name, image_folder, Method, Driver): +def Thread_ScreenShot(function_name, image_folder, Method, Driver, image_name): """ Capture screen of mobile or desktop """ sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME chars_to_remove = [ @@ -676,20 +683,8 @@ def Thread_ScreenShot(function_name, image_folder, Method, Driver): trans_table = str.maketrans( dict.fromkeys("".join(chars_to_remove)) ) # python3 version of translate - ImageName = os.path.join( - image_folder, - TimeStamp("utc") - + "_" - + (function_name.translate(trans_table)).strip().replace(" ", "_") - + ".png", - ) - ExecLog( - sModuleInfo, - "Capturing screen on %s, with driver: %s, and saving to %s" - % (str(Method), str(Driver), ImageName), - 0, - ) - + ImageName = os.path.join(image_folder, (image_name.translate(trans_table)).strip().replace(" ", "_") + ".png") + ExecLog(sModuleInfo, "Capturing screen on %s, with driver: %s, and saving to %s" % (str(Method), str(Driver), ImageName), 0) try: # Capture screenshot of desktop if Method == "desktop": From 76c20e224132a25d4c40da5a643fe898acc63ad2 Mon Sep 17 00:00:00 2001 From: Muntasib-creator Date: Fri, 5 Feb 2021 03:31:49 +0600 Subject: [PATCH 2/3] New action added: Read from CSV file --- .../action_declarations/common.py | 1 + .../Sequential_Actions/common_functions.py | 59 ++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/Framework/Built_In_Automation/Sequential_Actions/action_declarations/common.py b/Framework/Built_In_Automation/Sequential_Actions/action_declarations/common.py index 7ff0c215d..7752bbff6 100644 --- a/Framework/Built_In_Automation/Sequential_Actions/action_declarations/common.py +++ b/Framework/Built_In_Automation/Sequential_Actions/action_declarations/common.py @@ -59,6 +59,7 @@ { "name": "write into excel", "function": "excel_write", "screenshot": "none" }, { "name": "excel comparison", "function": "excel_comparison", "screenshot": "none" }, { "name": "read from excel", "function": "excel_read", "screenshot": "none" }, + { "name": "read from csv", "function": "csv_read", "screenshot": "none" } ) # yapf: disable module_name = "common" diff --git a/Framework/Built_In_Automation/Sequential_Actions/common_functions.py b/Framework/Built_In_Automation/Sequential_Actions/common_functions.py index 7eb2c0b0f..11fc02a89 100755 --- a/Framework/Built_In_Automation/Sequential_Actions/common_functions.py +++ b/Framework/Built_In_Automation/Sequential_Actions/common_functions.py @@ -5,7 +5,7 @@ Caveat: Functions common to multiple Built In Functions must have action names that are unique, because we search the common functions first, regardless of the module name passed by the user """ -import inspect, sys, time, collections, ftplib, os, ast, copy +import inspect, sys, time, collections, ftplib, os, ast, copy, csv from pathlib import Path try: @@ -2894,3 +2894,60 @@ def execute_python_code(data_set): CommonUtil.ExecLog(sModuleInfo, "Executed the python code which was provided", 1) return "passed" + + +@logger +def csv_read(data_set): + sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME + try: + filepath = None + delimiter = "," + delimiter_support = { + "comma": ",", + "semicolon": ";", + "dot": ".", + "colon": ":", + "dash": "-", + "space": " ", + "tab": "\t", + "pipe": "|", + "asterisk": "*", + "plus": "+", + "slash": "/", + "backslash": "\\", + } + var_name = "" + structure = "list of dictionaries" + for left, mid, right in data_set: + left = left.lower().strip() + if "file path" in left: + filepath = right.strip() + # Expand ~ (home directory of user) to absolute path. + if "~" in filepath: + filepath = Path(os.path.expanduser(filepath)) + filepath = Path(filepath) + elif "delimiter" in left: + right = right.strip() + if right in delimiter_support: + delimiter = delimiter_support[right] + else: + delimiter = right + elif "structure of the variable" in left: + pass + if "read from csv" in left: + var_name = right.strip() + + with open(filepath, "r") as csv_file: + if structure == "list of dictionaries": + csv_read_data = csv.DictReader(csv_file, delimiter=delimiter) + data_to_save = [] + for line in csv_read_data: + data_to_save.append(line) + + CommonUtil.ExecLog(sModuleInfo, "Extracted CSV data with '%s' delimiter and saved data as %s format" % (delimiter, structure), 1) + sr.Set_Shared_Variables(var_name, data_to_save) + return "passed" + + except: + return CommonUtil.Exception_Handler(sys.exc_info()) + From fa1c2f4f257afba3ac70e465e5d859c3afe52080 Mon Sep 17 00:00:00 2001 From: Muntasib-creator Date: Fri, 5 Feb 2021 04:03:35 +0600 Subject: [PATCH 3/3] Added list comprehension for Read from CSV file --- .../Sequential_Actions/common_functions.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Framework/Built_In_Automation/Sequential_Actions/common_functions.py b/Framework/Built_In_Automation/Sequential_Actions/common_functions.py index 11fc02a89..dff1223c2 100755 --- a/Framework/Built_In_Automation/Sequential_Actions/common_functions.py +++ b/Framework/Built_In_Automation/Sequential_Actions/common_functions.py @@ -2940,9 +2940,7 @@ def csv_read(data_set): with open(filepath, "r") as csv_file: if structure == "list of dictionaries": csv_read_data = csv.DictReader(csv_file, delimiter=delimiter) - data_to_save = [] - for line in csv_read_data: - data_to_save.append(line) + data_to_save = [line for line in csv_read_data] CommonUtil.ExecLog(sModuleInfo, "Extracted CSV data with '%s' delimiter and saved data as %s format" % (delimiter, structure), 1) sr.Set_Shared_Variables(var_name, data_to_save)