Skip to content

Commit adbbb53

Browse files
committed
Convert action handlers and MainDriver to async
1 parent 604151f commit adbbb53

4 files changed

Lines changed: 51 additions & 40 deletions

File tree

Drivers/Built_In_Driver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
from Framework.Built_In_Automation.Sequential_Actions import sequential_actions as sa
1010

1111

12-
def sequential_actions(
12+
async def sequential_actions(
1313
step_data,
1414
test_action_info,
1515
temp_q,
1616
debug_actions=None,
1717
):
1818
try:
19-
sTestStepReturnStatus = sa.Sequential_Actions(
19+
sTestStepReturnStatus = await sa.Sequential_Actions(
2020
step_data,
2121
test_action_info,
2222
debug_actions,

Framework/Built_In_Automation/Sequential_Actions/sequential_actions.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def if_else_log_for_actions(left, next_level_step_data, statement="if"):
279279
return left + ".... condition matched\n" + "Running actions: " + log_actions
280280

281281

282-
def If_else_action(step_data, data_set_no):
282+
async def If_else_action(step_data, data_set_no):
283283
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
284284
try:
285285
data_set = step_data[data_set_no]
@@ -526,7 +526,7 @@ def check_operators():
526526
)
527527
return "zeuz_failed"
528528
if data_set_index not in inner_skip:
529-
result, skip = Run_Sequential_Actions(
529+
result, skip = await Run_Sequential_Actions(
530530
[data_set_index]
531531
) # Running
532532
inner_skip = list(set(inner_skip+skip))
@@ -559,7 +559,7 @@ def sanitize_deprecated_dataset(value):
559559
return value
560560

561561

562-
def for_loop_action(step_data, data_set_no):
562+
async def for_loop_action(step_data, data_set_no):
563563
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
564564
try:
565565
data_set = step_data[data_set_no]
@@ -748,7 +748,7 @@ def for_loop_action(step_data, data_set_no):
748748
sr.Set_Shared_Variables(CommonUtil.dont_prettify_on_server[0], step_data, protected=True, pretty=False)
749749
sr.test_action_info = CommonUtil.all_action_info[step_index]
750750
return "zeuz_failed", outer_skip
751-
result, skip = Run_Sequential_Actions([data_set_index])
751+
result, skip = await Run_Sequential_Actions([data_set_index])
752752
inner_skip = list(set(inner_skip + skip))
753753
outer_skip = list(set(outer_skip + inner_skip))
754754

@@ -848,7 +848,7 @@ def for_loop_action(step_data, data_set_no):
848848
return CommonUtil.Exception_Handler(sys.exc_info()), []
849849

850850

851-
def While_Loop_Action(step_data, data_set_no):
851+
async def While_Loop_Action(step_data, data_set_no):
852852
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
853853
try:
854854
data_set = step_data[data_set_no]
@@ -947,7 +947,7 @@ def While_Loop_Action(step_data, data_set_no):
947947
3
948948
)
949949
return "zeuz_failed", outer_skip
950-
result, skip = Run_Sequential_Actions(
950+
result, skip = await Run_Sequential_Actions(
951951
[data_set_index]
952952
) # new edit: full step data is passed. [step_data[data_set_index]])
953953
# Recursively call this function until all called data sets are complete
@@ -1049,7 +1049,7 @@ def ticker_linear_shape(seconds, callable, *args, **kwargs):
10491049
seconds -= 1
10501050

10511051

1052-
def Sequential_Actions(
1052+
async def Sequential_Actions(
10531053
step_data,
10541054
test_action_info,
10551055
debug_actions=None,
@@ -1068,13 +1068,13 @@ def Sequential_Actions(
10681068
# sr.Set_Shared_Variables("test_action_info", test_action_info, protected=True, print_variable=False)
10691069
sr.test_action_info = test_action_info
10701070

1071-
result, skip_for_loop = Run_Sequential_Actions([], debug_actions)
1071+
result, skip_for_loop = await Run_Sequential_Actions([], debug_actions)
10721072
# empty list means run all, instead of step data we want to send the dataset no's of the step data to run
10731073
write_browser_logs()
10741074
return result
10751075

10761076

1077-
def Run_Sequential_Actions(
1077+
async def Run_Sequential_Actions(
10781078
data_set_list=None, debug_actions=None
10791079
): # data_set_no will used in recursive conditional action call
10801080
if data_set_list is None:
@@ -1100,7 +1100,7 @@ def Run_Sequential_Actions(
11001100
data_set_list.append(i)
11011101

11021102
if len(data_set_list) == 0 and CommonUtil.debug_status and not sr.Test_Shared_Variables("selenium_driver") and ConfigModule.get_config_value("Inspector", "ai_plugin").strip().lower() in CommonUtil.affirmative_words:
1103-
return Action_Handler([["browser", "selenium action", "browser"]], ["browser", "selenium action", "browser"]), []
1103+
return await Action_Handler([["browser", "selenium action", "browser"]], ["browser", "selenium action", "browser"]), []
11041104

11051105
for dataset_cnt in data_set_list: # For each data set within step data
11061106
data_set = step_data[dataset_cnt] # Save data set to variable
@@ -1196,15 +1196,15 @@ def Run_Sequential_Actions(
11961196

11971197
# If middle column = action, call action handler, but always return a pass
11981198
elif "optional action" in action_name:
1199-
result = Action_Handler(data_set, row) # Pass data set, and action_name to action handler
1199+
result = await Action_Handler(data_set, row) # Pass data set, and action_name to action handler
12001200
if result == "zeuz_failed":
12011201
CommonUtil.ExecLog(sModuleInfo, "Optional action failed. Returning pass anyway", 2)
12021202
result = "passed"
12031203

12041204
# If middle column = conditional action, evaluate data set
12051205
elif "conditional action" in action_name or "if else" in action_name:
12061206
if action_name.lower().strip() == "windows conditional action":
1207-
result, to_skip = Conditional_Action_Handler(step_data, dataset_cnt)
1207+
result, to_skip = await Conditional_Action_Handler(step_data, dataset_cnt)
12081208
skip += to_skip
12091209
skip_for_loop += to_skip
12101210
if result in failed_tag_list:
@@ -1215,7 +1215,7 @@ def Run_Sequential_Actions(
12151215

12161216
elif action_name.lower().strip() != "conditional action" and action_name.lower().strip() != "if else":
12171217
# old style conditional action
1218-
result, to_skip = Conditional_Action_Handler(step_data, dataset_cnt)
1218+
result, to_skip = await Conditional_Action_Handler(step_data, dataset_cnt)
12191219
skip += to_skip
12201220
skip_for_loop += to_skip
12211221
if result in failed_tag_list:
@@ -1224,7 +1224,7 @@ def Run_Sequential_Actions(
12241224
break
12251225

12261226
else:
1227-
result, to_skip = If_else_action(step_data, dataset_cnt)
1227+
result, to_skip = await If_else_action(step_data, dataset_cnt)
12281228
skip += to_skip
12291229
skip_for_loop += to_skip
12301230
if result in failed_tag_list:
@@ -1235,15 +1235,15 @@ def Run_Sequential_Actions(
12351235
# Simulate a while/for loop with the specified data sets
12361236
elif "loop action" in action_name:
12371237
if action_name.lower().strip() == "for loop action":
1238-
result, skip_for_loop = for_loop_action(step_data, dataset_cnt)
1238+
result, skip_for_loop = await for_loop_action(step_data, dataset_cnt)
12391239
skip = list(set(skip + skip_for_loop))
12401240
if result in failed_tag_list:
12411241
return "zeuz_failed", skip_for_loop
12421242
break
12431243
elif action_name.lower().strip() not in ("while loop action", "for loop action"):
12441244
# old style loop action
12451245
# CommonUtil.ExecLog(sModuleInfo,"Old style loop action found. This will not be supported in 2020, please replace them with new loop actions",2)
1246-
result, skip_for_loop = Loop_Action_Handler(data_set, row, dataset_cnt)
1246+
result, skip_for_loop = await Loop_Action_Handler(data_set, row, dataset_cnt)
12471247
skip = skip_for_loop
12481248

12491249
position_of_loop_action = dataset_cnt
@@ -1273,7 +1273,7 @@ def Run_Sequential_Actions(
12731273
return "zeuz_failed", skip_for_loop
12741274
elif "loop" in action_name:
12751275
if "while" in action_name.lower():
1276-
result, skip_for_loop = While_Loop_Action(step_data, dataset_cnt)
1276+
result, skip_for_loop = await While_Loop_Action(step_data, dataset_cnt)
12771277
skip = list(set(skip + skip_for_loop))
12781278
if result in failed_tag_list:
12791279
return "zeuz_failed", skip_for_loop
@@ -1343,7 +1343,7 @@ def Run_Sequential_Actions(
13431343

13441344
# If middle column = action, call action handler
13451345
elif "action" in action_name: # Must be last, since it's a single word that also exists in other action types
1346-
result = Action_Handler(data_set, row) # Pass data set, and action_name to action handler
1346+
result = await Action_Handler(data_set, row) # Pass data set, and action_name to action handler
13471347
if row[0].lower().strip() in ("step exit", "testcase exit"):
13481348
global step_exit_fail_called, step_exit_pass_called
13491349
CommonUtil.ExecLog(sModuleInfo, f"{row[0].lower().strip()} Exit called. Stopping Test Step.", 1)
@@ -1373,12 +1373,12 @@ def Run_Sequential_Actions(
13731373
continue
13741374

13751375
CommonUtil.ExecLog(sModuleInfo, "Action failed. Trying bypass #%d" % (i + 1), 1)
1376-
result = Action_Handler(bypass_data_set[i], bypass_row[i])
1376+
result = await Action_Handler(bypass_data_set[i], bypass_row[i])
13771377
if result in failed_tag_list: # This also failed, so chances are first failure was real
13781378
continue # Try the next bypass, if any
13791379
else: # Bypass passed, which indicates there was something blocking the element in the first place
13801380
CommonUtil.ExecLog(sModuleInfo, "Bypass passed. Retrying original action", 1)
1381-
result = Action_Handler(data_set, row) # Retry failed original data set
1381+
result = await Action_Handler(data_set, row) # Retry failed original data set
13821382
if result in failed_tag_list: # Still a failure, give up
13831383
return "zeuz_failed", skip_for_loop
13841384
break # No need to process more bypasses
@@ -1404,7 +1404,7 @@ def Run_Sequential_Actions(
14041404
return CommonUtil.Exception_Handler(sys.exc_info())
14051405

14061406

1407-
def Loop_Action_Handler(data, row, dataset_cnt):
1407+
async def Loop_Action_Handler(data, row, dataset_cnt):
14081408
""" Performs a sub-set of the data set in a loop, similar to a for or while loop """
14091409

14101410
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
@@ -1903,7 +1903,7 @@ def build_subset(new_step_data):
19031903
return CommonUtil.Exception_Handler(sys.exc_info())
19041904

19051905

1906-
def Conditional_Action_Handler(step_data, dataset_cnt):
1906+
async def Conditional_Action_Handler(step_data, dataset_cnt):
19071907
""" Process conditional actions, called only by Sequential_Actions() """
19081908
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
19091909

@@ -2296,7 +2296,7 @@ def compare_variable_names(set, dataset):
22962296
CommonUtil.compare_action_varnames = {"left": "Left", "right": "Right"}
22972297

22982298

2299-
def Action_Handler(_data_set, action_row, _bypass_bug=True):
2299+
async def Action_Handler(_data_set, action_row, _bypass_bug=True):
23002300
""" Finds the appropriate function for the requested action in the step data and executes it """
23012301

23022302
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
@@ -2417,6 +2417,8 @@ def Action_Handler(_data_set, action_row, _bypass_bug=True):
24172417
elif module in CommonUtil.global_sleep and "_all_" in CommonUtil.global_sleep[module]:
24182418
time.sleep(CommonUtil.global_sleep[module]["_all_"]["pre"])
24192419
result = run_function(data_set) # Execute function, providing all rows in the data set
2420+
if inspect.iscoroutine(result):
2421+
result = await result
24202422
if post_sleep:
24212423
time.sleep(post_sleep)
24222424
elif module in CommonUtil.global_sleep and "_all_" in CommonUtil.global_sleep[module]:

Framework/MainDriverApi.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def terminate_thread(thread): # To kill running thread
281281
raise SystemError("PyThreadState_SetAsyncExc failed")
282282

283283
# call the function of a test step that is in its driver file
284-
def call_driver_function_of_test_step(
284+
async def call_driver_function_of_test_step(
285285
sModuleInfo,
286286
all_step_info,
287287
StepSeq,
@@ -312,6 +312,7 @@ def call_driver_function_of_test_step(
312312
try:
313313
# importing functions from driver
314314
functionTocall = getattr(module_name, step_name)
315+
print(functionTocall)
315316
except Exception as e:
316317
CommonUtil.Exception_Handler(
317318
sys.exc_info(),
@@ -379,12 +380,20 @@ def call_driver_function_of_test_step(
379380
CommonUtil.Exception_Handler(sys.exc_info())
380381
else:
381382
# run sequentially
382-
sStepResult = functionTocall(
383-
test_steps_data,
384-
test_action_info,
385-
simple_queue,
386-
debug_actions,
387-
)
383+
if inspect.iscoroutinefunction(functionTocall):
384+
sStepResult = await functionTocall(
385+
test_steps_data,
386+
test_action_info,
387+
simple_queue,
388+
debug_actions,
389+
)
390+
else:
391+
sStepResult = functionTocall(
392+
test_steps_data,
393+
test_action_info,
394+
simple_queue,
395+
debug_actions,
396+
)
388397
except:
389398
CommonUtil.Exception_Handler(sys.exc_info()) # handle exceptions
390399
sStepResult = "zeuz_failed"
@@ -411,7 +420,7 @@ def call_driver_function_of_test_step(
411420

412421

413422
# runs all test steps of a test case
414-
def run_all_test_steps_in_a_test_case(
423+
async def run_all_test_steps_in_a_test_case(
415424
testcase_info,
416425
test_case,
417426
sModuleInfo,
@@ -606,7 +615,7 @@ def run_all_test_steps_in_a_test_case(
606615
CommonUtil.ExecLog(sModuleInfo, "STEP-%s is skipped" % StepSeq, 2)
607616
sStepResult = "skipped"
608617
else:
609-
sStepResult = call_driver_function_of_test_step(
618+
sStepResult = await call_driver_function_of_test_step(
610619
sModuleInfo,
611620
all_step_info,
612621
StepSeq,
@@ -936,7 +945,7 @@ def check_test_skip(run_id, tc_num, skip_remaining=True) -> bool:
936945
return False
937946

938947

939-
def run_test_case(
948+
async def run_test_case(
940949
TestCaseID,
941950
sModuleInfo,
942951
run_id,
@@ -994,7 +1003,7 @@ def run_test_case(
9941003
if check_test_skip(run_id, tc_num):
9951004
sTestStepResultList = ['SKIPPED' for i in range(len(testcase_info['steps']))]
9961005
else:
997-
sTestStepResultList = run_all_test_steps_in_a_test_case(
1006+
sTestStepResultList = await run_all_test_steps_in_a_test_case(
9981007
testcase_info,
9991008
test_case,
10001009
sModuleInfo,
@@ -1806,7 +1815,7 @@ def download_or_copy(attachment):
18061815

18071816

18081817
# main function
1809-
def main(device_dict, all_run_id_info):
1818+
async def main(device_dict, all_run_id_info):
18101819
try:
18111820
# get module info
18121821
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
@@ -2093,7 +2102,7 @@ def kill(process):
20932102
)
20942103
except Exception as e:
20952104
CommonUtil.ExecLog(sModuleInfo, str(e), 3)
2096-
run_test_case(
2105+
await run_test_case(
20972106
test_case_no,
20982107
sModuleInfo,
20992108
run_id,
@@ -2111,7 +2120,7 @@ def kill(process):
21112120

21122121

21132122
else:
2114-
run_test_case(
2123+
await run_test_case(
21152124
test_case_no,
21162125
sModuleInfo,
21172126
run_id,

node_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ async def response_callback(response: str):
440440
# 3. Call MainDriver
441441
device_info = All_Device_Info.get_all_connected_device_info()
442442
await install_handler.cancel_run()
443-
MainDriverApi.main(
443+
await MainDriverApi.main(
444444
device_dict=device_info,
445445
all_run_id_info=node_json,
446446
)

0 commit comments

Comments
 (0)