@@ -92,14 +92,29 @@ def wrap(*args, **kwargs):
92
92
return wrap
93
93
94
94
95
- # noinspection PyPep8Naming
95
+ def process_keyword_names_and_tags (matcher_list : List [KeywordMatch ], pattern_str : str ) -> None :
96
+ """Pick name or pattern string, parse it and add to the matcher list.
97
+
98
+ :param matcher_list: List of keyword matchers to add to
99
+ :param pattern_str: Pattern string passed to `--remove-keywords` or `--flatten-keywords` argument
100
+ """
101
+ if ":" in pattern_str :
102
+ pattern_type , pattern = pattern_str .split (":" , 1 )
103
+ pattern_type = pattern_type .strip ().upper ()
104
+ if "NAME" == pattern_type .upper ():
105
+ matcher_list .append (KeywordNameMatch (pattern .strip ()))
106
+ elif "TAG" == pattern_type .upper ():
107
+ matcher_list .append (KeywordTagMatch (pattern .strip ()))
108
+
109
+
96
110
class listener :
97
111
"""Robot Framework listener interface for reporting to ReportPortal."""
98
112
99
113
_items : LifoQueue [Union [Keyword , Launch , Suite , Test ]]
100
114
_service : Optional [RobotService ]
101
115
_variables : Optional [Variables ]
102
116
_remove_keyword_filters : List [KeywordMatch ] = []
117
+ _flatten_keyword_filters : List [KeywordMatch ] = []
103
118
_remove_all_keyword_content : bool = False
104
119
_remove_data_passed_tests : bool = False
105
120
ROBOT_LISTENER_API_VERSION = 2
@@ -321,13 +336,28 @@ def _process_keyword_remove(self):
321
336
else :
322
337
self ._remove_keyword_filters .append (WHILE_KEYWORD_MATCH )
323
338
continue
324
- if ":" in pattern_str :
325
- pattern_type , pattern = pattern_str .split (":" , 1 )
326
- pattern_type = pattern_type .strip ().upper ()
327
- if "NAME" == pattern_type .upper ():
328
- self ._remove_keyword_filters .append (KeywordNameMatch (pattern .strip ()))
329
- elif "TAG" == pattern_type .upper ():
330
- self ._remove_keyword_filters .append (KeywordTagMatch (pattern .strip ()))
339
+ process_keyword_names_and_tags (self ._remove_keyword_filters , pattern_str )
340
+
341
+ def _process_keyword_flatten (self ):
342
+ if not self .variables .flatten_keywords :
343
+ return
344
+
345
+ self ._flatten_keyword_filters = []
346
+ current_context = EXECUTION_CONTEXTS .current
347
+ if current_context :
348
+ # noinspection PyProtectedMember
349
+ for pattern_str in set (current_context .output ._settings .flatten_keywords ):
350
+ pattern_str_upper = pattern_str .upper ()
351
+ if pattern_str_upper in {"FOR" , "WHILE" , "ITERATION" , "FORITEM" }:
352
+ if pattern_str_upper == "FOR" :
353
+ self ._flatten_keyword_filters .append (FOR_KEYWORD_MATCH )
354
+ elif pattern_str_upper == "WHILE" :
355
+ self ._flatten_keyword_filters .append (WHILE_KEYWORD_MATCH )
356
+ else :
357
+ self ._flatten_keyword_filters .append (FOR_KEYWORD_MATCH )
358
+ self ._flatten_keyword_filters .append (WHILE_KEYWORD_MATCH )
359
+ continue
360
+ process_keyword_names_and_tags (self ._flatten_keyword_filters , pattern_str )
331
361
332
362
def start_launch (self , attributes : Dict [str , Any ], ts : Optional [Any ] = None ) -> None :
333
363
"""Start a new launch at the ReportPortal.
@@ -336,6 +366,7 @@ def start_launch(self, attributes: Dict[str, Any], ts: Optional[Any] = None) ->
336
366
:param ts: Timestamp(used by the ResultVisitor)
337
367
"""
338
368
self ._process_keyword_remove ()
369
+ self ._process_keyword_flatten ()
339
370
340
371
launch = Launch (self .variables .launch_name , attributes , self .variables .launch_attributes )
341
372
launch .doc = self .variables .launch_doc or launch .doc
@@ -410,6 +441,9 @@ def start_test(self, name: str, attributes: Dict, ts: Optional[Any] = None) -> N
410
441
test .rp_item_id = self .service .start_test (test = test , ts = ts )
411
442
self ._add_current_item (test )
412
443
444
+ def _log_keyword_content_removed (self , item_id : str , timestamp : str ) -> None :
445
+ self ._log_data_removed (item_id , timestamp , REMOVED_KEYWORD_CONTENT_LOG )
446
+
413
447
@check_rp_enabled
414
448
def end_test (self , _ : Optional [str ], attributes : Dict , ts : Optional [Any ] = None ) -> None :
415
449
"""Finish started test case at the ReportPortal.
@@ -437,14 +471,20 @@ def _log_data_removed(self, item_id: str, timestamp: str, message: str) -> None:
437
471
msg .timestamp = timestamp
438
472
self .__post_log_message (msg )
439
473
440
- def _log_keyword_content_removed (self , item_id : str , timestamp : str ) -> None :
441
- self ._log_data_removed (item_id , timestamp , REMOVED_KEYWORD_CONTENT_LOG )
442
-
443
474
def _do_start_keyword (self , keyword : Keyword , ts : Optional [str ] = None ) -> None :
444
475
logger .debug (f"ReportPortal - Start Keyword: { keyword .robot_attributes } " )
445
476
keyword .rp_item_id = self .service .start_keyword (keyword = keyword , ts = ts )
446
477
keyword .posted = True
447
478
479
+ def _should_remove (self , keyword : Keyword ) -> Optional [KeywordMatch ]:
480
+ for matcher in self ._remove_keyword_filters :
481
+ if matcher .match (keyword ):
482
+ return matcher
483
+ return None
484
+
485
+ def _should_flatten (self , keyword : Keyword ) -> bool :
486
+ return any (matcher .match (keyword ) for matcher in self ._flatten_keyword_filters )
487
+
448
488
@check_rp_enabled
449
489
def start_keyword (self , name : str , attributes : Dict , ts : Optional [Any ] = None ) -> None :
450
490
"""Start a new keyword(test step) at the ReportPortal.
@@ -453,22 +493,21 @@ def start_keyword(self, name: str, attributes: Dict, ts: Optional[Any] = None) -
453
493
:param attributes: Dictionary passed by the Robot Framework
454
494
:param ts: Timestamp(used by the ResultVisitor)
455
495
"""
456
- kwd = Keyword (name , attributes , self .current_item )
457
496
parent = self .current_item
497
+ kwd = Keyword (name , attributes , parent )
458
498
remove_kwd = parent .remove_data
459
499
skip_data = self ._remove_all_keyword_content or self ._remove_data_passed_tests
460
500
kwd .remove_data = remove_kwd or skip_data
461
501
462
502
if kwd .remove_data :
463
- kwd .matched_filter = getattr ( parent , "matched_filter" , None )
464
- kwd .skip_origin = getattr ( parent , "skip_origin" , None )
503
+ kwd .remove_filter = parent . remove_filter
504
+ kwd .remove_origin = parent . remove_origin
465
505
else :
466
- for m in self ._remove_keyword_filters :
467
- if m .match (kwd ):
468
- kwd .remove_data = True
469
- kwd .matched_filter = m
470
- kwd .skip_origin = kwd
471
- break
506
+ m = self ._should_remove (kwd )
507
+ if m :
508
+ kwd .remove_data = True
509
+ kwd .remove_filter = m
510
+ kwd .remove_origin = kwd
472
511
473
512
if remove_kwd :
474
513
kwd .rp_item_id = str (uuid .uuid4 ())
@@ -477,7 +516,7 @@ def start_keyword(self, name: str, attributes: Dict, ts: Optional[Any] = None) -
477
516
else :
478
517
self ._do_start_keyword (kwd , ts )
479
518
if skip_data :
480
- kwd .skip_origin = kwd
519
+ kwd .remove_origin = kwd
481
520
if self ._remove_data_passed_tests :
482
521
parent .skipped_keywords .append (kwd )
483
522
@@ -497,7 +536,7 @@ def end_keyword(self, _: Optional[str], attributes: Dict, ts: Optional[Any] = No
497
536
"""
498
537
kwd = self .current_item .update (attributes )
499
538
500
- if kwd .matched_filter is WUKS_KEYWORD_MATCH and kwd .skip_origin is kwd :
539
+ if kwd .remove_filter is WUKS_KEYWORD_MATCH and kwd .remove_origin is kwd :
501
540
skipped_keywords = kwd .skipped_keywords
502
541
skipped_keywords_num = len (skipped_keywords )
503
542
if skipped_keywords_num > 2 :
@@ -516,8 +555,8 @@ def end_keyword(self, _: Optional[str], attributes: Dict, ts: Optional[Any] = No
516
555
self ._do_end_keyword (last_iteration , ts )
517
556
518
557
elif (
519
- (kwd .matched_filter is FOR_KEYWORD_MATCH ) or (kwd .matched_filter is WHILE_KEYWORD_MATCH )
520
- ) and kwd .skip_origin is kwd :
558
+ (kwd .remove_filter is FOR_KEYWORD_MATCH ) or (kwd .remove_filter is WHILE_KEYWORD_MATCH )
559
+ ) and kwd .remove_origin is kwd :
521
560
skipped_keywords = kwd .skipped_keywords
522
561
skipped_keywords_num = len (skipped_keywords )
523
562
if skipped_keywords_num > 1 :
@@ -529,7 +568,7 @@ def end_keyword(self, _: Optional[str], attributes: Dict, ts: Optional[Any] = No
529
568
last_iteration = kwd .skipped_keywords [- 1 ]
530
569
self ._post_skipped_keywords (last_iteration )
531
570
self ._do_end_keyword (last_iteration , ts )
532
- elif kwd .posted and kwd .remove_data and kwd .skip_origin is kwd :
571
+ elif kwd .posted and kwd .remove_data and kwd .remove_origin is kwd :
533
572
if (self ._remove_all_keyword_content or not self ._remove_data_passed_tests ) and (
534
573
kwd .skipped_keywords or kwd .skipped_logs
535
574
):
0 commit comments