@@ -72,90 +72,33 @@ def _gen_class_actionid_tracking(self):
7272 ]
7373
7474 def _gen_action_start_with_tracking (self , actionId ):
75- # Assign actionId to self._current_actionId before action_start
75+ # Assign actionId (or None) to self._current_actionId before each action
7676 return [
7777 ast .Assign (
7878 targets = [ast_attr ("self._current_actionId" )],
7979 value = ast .Constant (value = actionId , kind = "" )
8080 )
8181 ]
8282
83- def _gen_teardown_with_actionid (self ):
84- # Print the last actionId if an error/failure occurred
85- return ast .FunctionDef (
86- name = "tearDown" ,
87- args = [ast .Name (id = 'self' , ctx = ast .Param ())],
88- body = [
89- ast .Expr (ast_call (
90- func = ast .Name (id = "print" , ctx = ast .Load ()),
91- args = [ast .Constant ("[TAURUS] tearDown called" , kind = "" )],
92- keywords = []
93- )),
94- ast .If (
95- test = ast_attr ("self.driver" ),
96- body = [ast .Expr (ast_call (func = ast_attr ("self.driver.quit" )))],
97- orelse = []
98- ),
99- ast .Assign (
100- targets = [ast .Name (id = "outcome" , ctx = ast .Store ())],
101- value = ast .Call (
102- func = ast .Name (id = "getattr" , ctx = ast .Load ()),
103- args = [ast .Name (id = "self" , ctx = ast .Load ()), ast .Constant ("_outcome" , kind = "" ),
104- ast .Constant (None , kind = "" )],
105- keywords = []
106- )
107- ),
108- ast .Assign (
109- targets = [ast .Name (id = "result" , ctx = ast .Store ())],
110- value = ast .Call (
111- func = ast .Name (id = "getattr" , ctx = ast .Load ()),
112- args = [ast .Name (id = "outcome" , ctx = ast .Load ()), ast .Constant ("result" , kind = "" ),
113- ast .Constant (None , kind = "" )],
114- keywords = []
115- )
116- ),
117- ast .If (
118- test = ast .BoolOp (
119- op = ast .And (),
120- values = [
121- ast .Name (id = "result" , ctx = ast .Load ()),
122- ast .BoolOp (
123- op = ast .Or (),
124- values = [
125- ast .Attribute (
126- value = ast .Name (id = "result" , ctx = ast .Load ()),
127- attr = "errors" ,
128- ctx = ast .Load ()
129- ),
130- ast .Attribute (
131- value = ast .Name (id = "result" , ctx = ast .Load ()),
132- attr = "failures" ,
133- ctx = ast .Load ()
134- )
135- ]
136- )
137- ]
138- ),
139- body = [
140- ast .Expr (ast_call (
141- func = ast .Name (id = "print" , ctx = ast .Load ()),
142- args = [ast .BinOp (
143- left = ast .Constant ("[TAURUS][actionId] " , kind = "" ),
144- op = ast .Add (),
145- right = ast .Call (
146- func = ast .Name (id = "str" , ctx = ast .Load ()),
147- args = [ast_attr ("self._current_actionId" )],
148- keywords = []
149- )
150- )],
151- keywords = []
152- ))
153- ],
154- orelse = []
155- ),
156- ],
157- decorator_list = []
158- )
83+ # def _gen_teardown_with_actionid(self):
84+ # # Print the last actionId if an error/failure occurred
85+ # return ast.FunctionDef(
86+ # name="tearDown",
87+ # args=[ast.Name(id='self', ctx=ast.Param())],
88+ # body=[
89+ # ast.Expr(ast_call(
90+ # func=ast.Name(id="print", ctx=ast.Load()),
91+ # args=[ast.Constant("[TAURUS] tearDown called", kind="")],
92+ # keywords=[]
93+ # )),
94+ # ast.If(
95+ # test=ast_attr("self.driver"),
96+ # body=[ast.Expr(ast_call(func=ast_attr("self.driver.quit")))],
97+ # orelse=[]
98+ # ),
99+ # ],
100+ # decorator_list=[]
101+ # )
159102 BYS = {
160103 'xpath' : "XPATH" ,
161104 'css' : "CSS_SELECTOR" ,
@@ -257,6 +200,7 @@ def __init__(self, scenario, label, wdlog=None, executor=None, ignore_unknown_ac
257200 self .replace_dialogs = True
258201 self .bzm_tdo_settings = bzm_tdo_settings
259202 self .do_testdata_orchestration = False
203+ self .has_action_ids = False
260204
261205 def _parse_action_params (self , expr , name ):
262206 res = expr .match (name )
@@ -337,7 +281,11 @@ def _parse_dict_action(self, action_config):
337281 # actionId is an optional unique identifier for this action, used to track or reference
338282 # the action in the generated code and logs. It is passed through multiple layers of the
339283 # code generation process to maintain traceability of actions.
340- actionId = action_config .get ("actionId" )
284+ if "actionId" in action_config and action_config .get ("actionId" ) not in (None , "" , {}):
285+ actionId = action_config .get ("actionId" )
286+ self .has_action_ids = True
287+ else :
288+ actionId = None
341289 selectors = []
342290 if action_config .get ("locators" ):
343291 selectors = action_config .get ("locators" )
@@ -791,7 +739,7 @@ def _gen_action(self, action_config, parent_request=None, index_label=""):
791739 wrapInTransaction = self ._is_report_inside_actions (parent_request )
792740
793741 action_tracking = []
794- if actionId :
742+ if self . has_action_ids :
795743 action_tracking .extend (self ._gen_action_start_with_tracking (actionId ))
796744
797745 action_elements = []
@@ -880,50 +828,6 @@ def _gen_action(self, action_config, parent_request=None, index_label=""):
880828 action_elements .append (ast_call (func = ast_attr ("waiter" ), args = []))
881829
882830 action_body = [ast .Expr (element ) for element in action_elements ]
883- if actionId :
884- prefixed_error = ast .BinOp (
885- left = ast .BinOp (
886- left = ast .Constant ("[TAURUS][actionId] " , kind = "" ),
887- op = ast .Add (),
888- right = ast .Call (
889- func = ast .Name (id = "str" , ctx = ast .Load ()),
890- args = [ast_attr ("self._current_actionId" )],
891- keywords = []
892- )
893- ),
894- op = ast .Add (),
895- right = ast .BinOp (
896- left = ast .Constant (" | " , kind = "" ),
897- op = ast .Add (),
898- right = ast .Call (
899- func = ast .Name (id = "str" , ctx = ast .Load ()),
900- args = [ast .Name (id = "exc" , ctx = ast .Load ())],
901- keywords = []
902- )
903- )
904- )
905-
906- action_body = [ast .Try (
907- body = action_body ,
908- handlers = [ast .ExceptHandler (
909- type = ast .Name (id = "Exception" , ctx = ast .Load ()),
910- name = "exc" ,
911- body = [ast .Raise (
912- exc = ast .Call (
913- func = ast .Call (
914- func = ast .Name (id = "type" , ctx = ast .Load ()),
915- args = [ast .Name (id = "exc" , ctx = ast .Load ())],
916- keywords = []
917- ),
918- args = [prefixed_error ],
919- keywords = []
920- ),
921- cause = ast .Name (id = "exc" , ctx = ast .Load ())
922- )]
923- )],
924- orelse = [],
925- finalbody = []
926- )]
927831
928832 if wrapInTransaction :
929833 label = self ._create_action_label (parent_request .label , index_label , action )
@@ -1851,7 +1755,8 @@ def _gen_classdef(self):
18511755
18521756 def _gen_class_setup (self ):
18531757 data_sources = [self ._gen_default_vars ()]
1854- data_sources .extend (self ._gen_class_actionid_tracking ())
1758+ if self .has_action_ids :
1759+ data_sources .extend (self ._gen_class_actionid_tracking ())
18551760 for idx in range (len (self .data_sources )):
18561761 data_sources .append (ast .Expr (ast_call (func = ast_attr ("reader_%s.read_vars" % (idx + 1 )))))
18571762
@@ -1929,7 +1834,14 @@ def _gen_class_setup(self):
19291834 return [setup , gen_empty_line_stmt ()]
19301835
19311836 def _gen_class_teardown (self ):
1932- return self ._gen_teardown_with_actionid ()
1837+ body = [
1838+ ast .If (
1839+ test = ast_attr ("self.driver" ),
1840+ body = [ast .Expr (ast_call (func = ast_attr ("self.driver.quit" )))],
1841+ orelse = []
1842+ )
1843+ ]
1844+ return ast .FunctionDef (name = "tearDown" , args = [ast_attr ("self" )], body = body , decorator_list = [])
19331845
19341846 def _nfc_preprocess (self , requests ):
19351847 setup = []
@@ -1999,7 +1911,11 @@ def _gen_test_methods(self):
19991911 if isinstance (request , SetVariables ):
20001912 self .service_methods .append (label ) # for sample excluding
20011913
2002- methods .append (self ._gen_test_method (method_name , body ))
1914+ method_body = body
1915+ if self .has_action_ids and not finally_marker :
1916+ method_body = self ._gen_actionid_exception_wrapped_body (body )
1917+
1918+ methods .append (self ._gen_test_method (method_name , method_body ))
20031919 if finally_marker :
20041920 finally_block_content .append (method_name )
20051921 else :
@@ -2017,13 +1933,79 @@ def _gen_set_vars(self, request):
20171933
20181934 return res
20191935
1936+ def _gen_actionid_exception_wrapped_body (self , body ):
1937+ prefixed_error = ast .BinOp (
1938+ left = ast .BinOp (
1939+ left = ast .BinOp (
1940+ left = ast .Constant (" actionId: " , kind = "" ),
1941+ op = ast .Add (),
1942+ right = ast .Call (
1943+ func = ast .Name (id = "str" , ctx = ast .Load ()),
1944+ args = [ast_attr ("self._current_actionId" )],
1945+ keywords = []
1946+ )
1947+ ),
1948+ op = ast .Add (),
1949+ right = ast .Constant (" | " , kind = "" )
1950+ ),
1951+ op = ast .Add (),
1952+ right = ast .Call (
1953+ func = ast .Name (id = "str" , ctx = ast .Load ()),
1954+ args = [ast .Name (id = "exc" , ctx = ast .Load ())],
1955+ keywords = []
1956+ )
1957+ )
1958+
1959+ final_error = ast .IfExp (
1960+ test = ast .Compare (
1961+ left = ast_attr ("self._current_actionId" ),
1962+ ops = [ast .IsNot ()],
1963+ comparators = [ast .Constant (value = None , kind = "" )]
1964+ ),
1965+ body = prefixed_error ,
1966+ orelse = ast .Call (
1967+ func = ast .Name (id = "str" , ctx = ast .Load ()),
1968+ args = [ast .Name (id = "exc" , ctx = ast .Load ())],
1969+ keywords = []
1970+ )
1971+ )
1972+
1973+ wrapped_try = ast .Try (
1974+ body = body ,
1975+ handlers = [ast .ExceptHandler (
1976+ type = ast .Name (id = "Exception" , ctx = ast .Load ()),
1977+ name = "exc" ,
1978+ body = [ast .Raise (
1979+ exc = ast .Call (
1980+ func = ast .Call (
1981+ func = ast .Name (id = "type" , ctx = ast .Load ()),
1982+ args = [ast .Name (id = "exc" , ctx = ast .Load ())],
1983+ keywords = []
1984+ ),
1985+ args = [final_error ],
1986+ keywords = []
1987+ ),
1988+ cause = ast .Name (id = "exc" , ctx = ast .Load ())
1989+ )]
1990+ )],
1991+ orelse = [],
1992+ finalbody = []
1993+ )
1994+
1995+ if len (body ) == 1 and isinstance (body [0 ], ast .With ):
1996+ with_stmt = body [0 ]
1997+ with_stmt .body = [wrapped_try ]
1998+ return [with_stmt ]
1999+
2000+ return [wrapped_try ]
2001+
20202002 def _gen_master_test_method (self , try_block , finally_block ):
20212003 if not try_block :
20222004 raise TaurusConfigError ("Supported transactions not found, test is empty" )
20232005
2024- body = []
2006+ main_body = []
20252007 for slave_name in try_block :
2026- body .append (ast .Expr (ast_call (func = ast_attr ("self." + slave_name ))))
2008+ main_body .append (ast .Expr (ast_call (func = ast_attr ("self." + slave_name ))))
20272009
20282010 finally_body = []
20292011 for slave_name in finally_block :
@@ -2032,7 +2014,16 @@ def _gen_master_test_method(self, try_block, finally_block):
20322014 if finally_body :
20332015 teardown_marker = ast .Expr (ast_call (func = ast_attr ("apiritif.set_stage" ), args = [self ._gen_expr ("teardown" )]))
20342016 finally_body .insert (0 , teardown_marker )
2035- body = [gen_try_except (try_body = body , final_body = finally_body )]
2017+
2018+ if finally_body :
2019+ body = [ast .Try (
2020+ body = main_body ,
2021+ handlers = [],
2022+ orelse = [],
2023+ finalbody = finally_body
2024+ )]
2025+ else :
2026+ body = main_body
20362027
20372028 name = 'test_' + create_method_name (self .label )
20382029 return self ._gen_test_method (name = name , body = body )
0 commit comments