Skip to content

Commit e28172d

Browse files
Merge pull request #1 from AutomationSolutionz/zip_and_performance
Zip and progress bar
2 parents 24f0ac8 + 41bdf97 commit e28172d

File tree

10 files changed

+153
-113
lines changed

10 files changed

+153
-113
lines changed

Framework/Built_In_Automation/Sequential_Actions/common_functions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,7 @@ def excel_read(data_set):
21972197

21982198
# Save file so that we don't see the "Want to save" dailog.
21992199
wb.save()
2200+
wb.close()
22002201

22012202
return "passed"
22022203
except:
@@ -2286,6 +2287,7 @@ def excel_comparison(data_set):
22862287

22872288
# Save workbook.
22882289
wb.save()
2290+
wb.close()
22892291

22902292
return "passed"
22912293
except:

Framework/Built_In_Automation/Shared_Resources/BuiltInFunctionSharedResources.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ def Set_Shared_Variables(key, value, protected=False, allowEmpty=False, print_va
5353
shared_variables[key] = value
5454

5555
if print_variable:
56+
try: val = json.dumps(CommonUtil.parse_value_into_object(value), indent=2, sort_keys=True)
57+
except: val = str(value)
5658
CommonUtil.ExecLog(
5759
sModuleInfo, "Saved variable: %s" % key, 1,
5860
variable={
5961
"key": key,
60-
"val": json.dumps(CommonUtil.parse_value_into_object(value), indent=2, sort_keys=True)
62+
"val": val
6163
}
6264
)
6365

Framework/MainDriverApi.py

Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,31 +1327,31 @@ def write_log_file_for_test_case(
13271327
)
13281328

13291329
# upload will go here.
1330-
upload_zip(
1331-
ConfigModule.get_config_value("Authentication", "server_address"),
1332-
ConfigModule.get_config_value("Authentication", "server_port"),
1333-
ConfigModule.get_config_value("sectionOne", "temp_run_file_path", temp_ini_file),
1334-
run_id,
1335-
ConfigModule.get_config_value("sectionOne", "test_case", temp_ini_file) + ".zip",
1336-
ConfigModule.get_config_value("Advanced Options", "_file_upload_path"),
1337-
)
1338-
TCLogFile = (
1339-
os.sep
1340-
+ ConfigModule.get_config_value("Advanced Options", "_file_upload_path")
1341-
+ os.sep
1342-
+ run_id.replace(":", "-")
1343-
+ "/"
1344-
+ ConfigModule.get_config_value(
1345-
"sectionOne", "test_case", temp_ini_file
1346-
)
1347-
+ ".zip"
1348-
)
1349-
FL.DeleteFile(
1350-
ConfigModule.get_config_value(
1351-
"sectionOne", "test_case_folder", temp_ini_file
1352-
)
1353-
+ ".zip"
1354-
)
1330+
# upload_zip(
1331+
# ConfigModule.get_config_value("Authentication", "server_address"),
1332+
# ConfigModule.get_config_value("Authentication", "server_port"),
1333+
# ConfigModule.get_config_value("sectionOne", "temp_run_file_path", temp_ini_file),
1334+
# run_id,
1335+
# ConfigModule.get_config_value("sectionOne", "test_case", temp_ini_file) + ".zip",
1336+
# ConfigModule.get_config_value("Advanced Options", "_file_upload_path"),
1337+
# )
1338+
# TCLogFile = (
1339+
# os.sep
1340+
# + ConfigModule.get_config_value("Advanced Options", "_file_upload_path")
1341+
# + os.sep
1342+
# + run_id.replace(":", "-")
1343+
# + "/"
1344+
# + ConfigModule.get_config_value(
1345+
# "sectionOne", "test_case", temp_ini_file
1346+
# )
1347+
# + ".zip"
1348+
# )
1349+
# FL.DeleteFile(
1350+
# ConfigModule.get_config_value(
1351+
# "sectionOne", "test_case_folder", temp_ini_file
1352+
# )
1353+
# + ".zip"
1354+
# )
13551355
else:
13561356
TCLogFile = ""
13571357
# upload the log file ID
@@ -1433,17 +1433,19 @@ def run_test_case(
14331433
testcase_info,
14341434
executor,
14351435
debug_info,
1436+
all_file_specific_steps,
14361437
send_log_file_only_for_fail=True,
14371438
performance=False,
14381439
browserDriver=None,
14391440
):
14401441
shared.Set_Shared_Variables("run_id", run_id)
14411442
test_case = str(TestCaseID).replace("#", "no")
1442-
ConfigModule.add_config_value("sectionOne", "sTestStepExecLogId", "MainDriver", temp_ini_file)
1443-
file_specific_steps = download_attachments_for_test_case(
1443+
ConfigModule.add_config_value("sectionOne", "sTestStepExecLogId", sModuleInfo, temp_ini_file)
1444+
download_attachments_for_test_case(
14441445
sModuleInfo, run_id, test_case, temp_ini_file,
1445-
testcase_info["testcase_attachments_links"], testcase_info["step_attachments"]
1446+
[],[]
14461447
)
1448+
file_specific_steps = all_file_specific_steps[TestCaseID] if TestCaseID in all_file_specific_steps else {}
14471449
TestCaseName = testcase_info["title"]
14481450
log_line = "# EXECUTING TEST CASE : %s :: %s #" % (test_case, TestCaseName)
14491451
print("#"*(len(log_line)))
@@ -1471,6 +1473,13 @@ def run_test_case(
14711473
performance
14721474
)
14731475

1476+
ConfigModule.add_config_value(
1477+
"sectionOne",
1478+
"sTestStepExecLogId",
1479+
run_id + "|" + test_case + "|" + "none" + "|" + "none",
1480+
temp_ini_file,
1481+
)
1482+
14741483
# get test case end time
14751484
TestCaseEndTime = time.time()
14761485
sTestCaseEndTime = datetime.fromtimestamp(TestCaseEndTime).strftime("%Y-%m-%d %H:%M:%S")
@@ -1738,19 +1747,15 @@ def get_all_run_id_info(Userid, sModuleInfo):
17381747
# return response["json"]
17391748

17401749

1741-
def upload_json_report(Userid, temp_ini_file, run_id):
1742-
# path = os.path.join(os.path.abspath(__file__).split("Framework")[0])/Path("AutomationLog")/Path("execution_log.json")
1743-
path = ConfigModule.get_config_value("sectionOne", "temp_run_file_path", temp_ini_file) / Path(run_id.replace(":", "-"))
1744-
zip_path = path / Path("execution_log.zip")
1745-
path = path / Path("execution_log.json")
1750+
def upload_json_report(Userid, temp_ini_file, run_id, all_run_id_info):
1751+
zip_path = ConfigModule.get_config_value("sectionOne", "temp_run_file_path", temp_ini_file) / Path(run_id.replace(":", "-"))
1752+
path = zip_path / Path("execution_log.json")
17461753
json_report = CommonUtil.get_all_logs(json=True)
17471754
with open(path, "w") as f:
1748-
json.dump(json_report, f, indent=2)
1749-
1750-
FL.ZipFile(path, zip_path)
1751-
FL.DeleteFile(path)
1755+
json.dump(json_report, f)
17521756

17531757
if ConfigModule.get_config_value("RunDefinition", "local_run") == "False":
1758+
FL.ZipFolder(str(zip_path), str(zip_path) + ".zip")
17541759
for i in range(720): # 1 hour
17551760
res = requests.get(RequestFormatter.form_uri("is_copied_api/"), {"runid": run_id}, verify=False)
17561761
r = res.json()
@@ -1762,7 +1767,8 @@ def upload_json_report(Userid, temp_ini_file, run_id):
17621767
"Get the report from below path-\n" + path)
17631768
return
17641769

1765-
with open(zip_path, "rb") as fzip:
1770+
with open(str(zip_path) + ".zip", "rb") as fzip:
1771+
print("Uploading report of %s KB. Please wait" % (os.stat(str(zip_path) + ".zip").st_size / 1000))
17661772
for i in range(5):
17671773
res = requests.post(
17681774
RequestFormatter.form_uri("create_report_log_api/"),
@@ -1776,17 +1782,22 @@ def upload_json_report(Userid, temp_ini_file, run_id):
17761782
print("Could not Upload json report to server")
17771783
return
17781784
if isinstance(res_json, dict) and 'message' in res_json and res_json["message"]:
1779-
print("Successfully Uploaded json report to server")
1785+
print("Successfully Uploaded the report to server of run_id '%s'" % run_id)
17801786
else:
1781-
print("Could not Upload json report to server")
1787+
print("Could not Upload the report to server of run_id '%s'" % run_id)
17821788
break
17831789
time.sleep(1)
17841790
else:
17851791
print("Could not Upload json report to server")
1786-
1792+
os.unlink(str(zip_path) + ".zip")
1793+
with open(path, "w") as f:
1794+
json.dump(json_report, f, indent=2)
1795+
path = zip_path / Path("test_cases_data.json")
1796+
with open(path, "w") as f:
1797+
json.dump(all_run_id_info, f, indent=2)
17871798

17881799
# main function
1789-
def main(device_dict, user_info_object, all_run_id_info):
1800+
def main(device_dict, user_info_object):
17901801

17911802
# get module info
17921803
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
@@ -1807,13 +1818,31 @@ def main(device_dict, user_info_object, all_run_id_info):
18071818
Userid = (CommonUtil.MachineInfo().getLocalUser()).lower()
18081819

18091820
# all_run_id_info = get_all_run_id_info(Userid, sModuleInfo)
1810-
1821+
get_json, all_file_specific_steps = True, {}
1822+
save_path = Path(ConfigModule.get_config_value("sectionOne", "temp_run_file_path", temp_ini_file)) / "attachments"
1823+
cnt = 0
1824+
for i in os.walk(save_path):
1825+
if get_json:
1826+
get_json = False
1827+
json_path = Path(i[0]) / i[2][0]
1828+
folder_list = i[1]
1829+
for j in folder_list:
1830+
all_file_specific_steps[j] = {}
1831+
else:
1832+
for j in i[2]:
1833+
all_file_specific_steps[folder_list[cnt]][j] = str(Path(i[0]) / j)
1834+
cnt += 1
1835+
with open(json_path, "r") as f:
1836+
all_run_id_info = json.loads(f.read())
1837+
with open(Path.cwd().parent / "Projects" / "Local_run.json", "w") as f:
1838+
f.write(json.dumps(all_run_id_info))
18111839
if len(all_run_id_info) == 0:
18121840
CommonUtil.ExecLog("", "No Test Run Schedule found for the current user : %s" % Userid, 2)
18131841
return False
18141842

18151843
executor = CommonUtil.GetExecutor()
18161844
for run_id_info in all_run_id_info:
1845+
run_id_info["base_path"] = ConfigModule.get_config_value("Advanced Options", "_file_upload_path")
18171846
run_id = run_id_info["run_id"]
18181847
run_cancelled = ""
18191848
debug_info = ""
@@ -1822,9 +1851,7 @@ def main(device_dict, user_info_object, all_run_id_info):
18221851
# Write testcase json
18231852
path = ConfigModule.get_config_value("sectionOne", "temp_run_file_path", temp_ini_file) / Path(run_id.replace(":", "-"))
18241853
FL.CreateFolder(path)
1825-
path = path / Path("test_cases_data.json")
1826-
with open(path, "w") as f:
1827-
json.dump(all_run_id_info, f, indent=2)
1854+
18281855

18291856
# Start websocket server if we're in debug mode.
18301857
if run_id.lower().startswith("debug"):
@@ -1884,7 +1911,7 @@ def main(device_dict, user_info_object, all_run_id_info):
18841911
else:
18851912
CommonUtil.ExecLog("", "No Automated test cases found for the current user : %s" % Userid, 2)
18861913
return "pass"
1887-
1914+
num_of_tc = len(all_testcases_info)
18881915
CommonUtil.all_logs_json = all_run_id_info
18891916
cnt = 1
18901917
for testcase_info in all_testcases_info:
@@ -1986,6 +2013,7 @@ def kill(process):
19862013
testcase_info,
19872014
executor,
19882015
debug_info,
2016+
all_file_specific_steps,
19892017
send_log_file_only_for_fail,
19902018
)
19912019
CommonUtil.clear_all_logs() # clear logs
@@ -2008,14 +2036,15 @@ def kill(process):
20082036
"duration": TestSetDuration
20092037
}
20102038
CommonUtil.CreateJsonReport(setInfo=after_execution_dict)
2039+
print("Report creation time = %s sec for %s testcases" % (CommonUtil.report_json_time, num_of_tc))
20112040
CommonUtil.ExecLog("", "Test Set Completed", 4, False)
20122041

20132042
ConfigModule.add_config_value("sectionOne", "sTestStepExecLogId", "MainDriver", temp_ini_file)
20142043

20152044
if run_cancelled == CANCELLED_TAG:
20162045
CommonUtil.ExecLog(sModuleInfo, "Test Set Cancelled by the User", 1) # add log
20172046
elif not run_id.startswith("debug"):
2018-
upload_json_report(Userid, temp_ini_file, run_id)
2047+
upload_json_report(Userid, temp_ini_file, run_id, all_run_id_info)
20192048
# executor.submit(upload_json_report)
20202049

20212050
# Close websocket connection.

Framework/Utilities/CommonUtil.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393

9494
all_logs = {}
9595
all_logs_json, json_log_cond = [], False
96+
tc_error_logs = []
9697
all_logs_count = 0
9798
all_logs_list = []
9899
skip_list = ["step_data"]
@@ -290,19 +291,21 @@ def Result_Analyzer(sTestStepReturnStatus, temp_q):
290291
except Exception as e:
291292
return Exception_Handler(sys.exc_info())
292293

294+
report_json_time = 0.0
295+
import time
296+
293297

294298
def CreateJsonReport(logs=None, stepInfo=None, TCInfo=None, setInfo=None):
295-
global all_logs_json
299+
global all_logs_json, report_json_time, tc_error_logs
300+
start = time.perf_counter()
296301
if logs or stepInfo or TCInfo or setInfo:
297302
log_id = ConfigModule.get_config_value("sectionOne", "sTestStepExecLogId", temp_config)
298303
if not log_id:
299304
return
300305
log_id_vals = log_id.split("|")
301306
if logs:
302307
log_id, now, iLogLevel, status, sModuleInfo, sDetails = logs
303-
if len(log_id_vals) != 4:
304-
pass
305-
else:
308+
if len(log_id_vals) == 4:
306309
# these loops can be optimized by saving the previous log_id_vals and comparing it with current one
307310
runID, testcase_no, step_id, step_no = log_id_vals
308311
for run_id_info in all_logs_json:
@@ -316,31 +319,35 @@ def CreateJsonReport(logs=None, stepInfo=None, TCInfo=None, setInfo=None):
316319
if testcase_no == testcase_info["testcase_no"].replace("#", "no"):
317320
if TCInfo:
318321
testcase_info["execution_detail"] = TCInfo
322+
fail_reason_str = ""
323+
if TCInfo["status"] in ("Failed", "Blocked"):
324+
count = -min(len(tc_error_logs), 3)
325+
while count <= -1:
326+
fail_reason_str += tc_error_logs[count]
327+
if count != -1:
328+
fail_reason_str += "\n---------------------------------------------\n"
329+
count += 1
330+
testcase_info["execution_detail"]["failreason"] = fail_reason_str
331+
break
332+
if step_id == "none":
319333
break
320334
all_step_info = testcase_info["steps"]
321335
for step_info in all_step_info:
322336
if step_no == str(step_info["step_sequence"]) and step_id == str(step_info["step_id"]):
323337
if stepInfo:
324338
step_info["execution_detail"] = stepInfo
325-
fail_reason_log = []
326-
fail_reason_str = ""
339+
step_error_logs = []
327340
if stepInfo["status"].lower() == "failed":
328-
count = 0
329-
for each_log in reversed(step_info["log"]):
330-
if count == 4:
331-
break
332-
if each_log["status"].lower() == "error":
333-
fail_reason_log.append(each_log["details"])
334-
count += 1
335-
fail_reason_log.reverse()
336-
if fail_reason_log[-1].endswith(to_dlt_from_fail_reason):
337-
del fail_reason_log[-1]
338-
if len(fail_reason_log) > 3:
339-
del fail_reason_log[0]
340-
for i in fail_reason_log:
341-
fail_reason_str += i + "\n"
342-
fail_reason_str = fail_reason_str[:-1]
343-
step_info["failreason"] = fail_reason_str
341+
count, err_count, max_count = -1, 0, -len(step_info["log"])
342+
# Can be optimized by taking error when occurs and append it if the step fails only
343+
while count >= max_count and err_count < 3:
344+
each_log = step_info["log"][count]
345+
if each_log["status"].lower() == "error" and not each_log["details"].endswith(to_dlt_from_fail_reason):
346+
step_error_logs.append(each_log["details"])
347+
err_count += 1
348+
count -= 1
349+
step_error_logs.reverse()
350+
tc_error_logs += step_error_logs
344351
break
345352
log_info = {
346353
"status": status,
@@ -362,6 +369,7 @@ def CreateJsonReport(logs=None, stepInfo=None, TCInfo=None, setInfo=None):
362369
break
363370
elif stepInfo:
364371
pass
372+
report_json_time += (time.perf_counter() - start)
365373

366374

367375
def ExecLog(

0 commit comments

Comments
 (0)