@@ -54,6 +54,23 @@ def _encode_query_params(params):
54
54
return urllib .parse .urlencode (params )
55
55
56
56
57
+ def _parse_result_table (driver ):
58
+ table = driver .find_element (By .ID , "results-table" )
59
+ headers = table .find_elements (By .CSS_SELECTOR , "thead th" )
60
+ rows = table .find_elements (By .CSS_SELECTOR , "tbody tr.collapsible" )
61
+ table_data = []
62
+ for row in rows :
63
+ data_dict = {}
64
+
65
+ cells = row .find_elements (
9E81
By .TAG_NAME , "td" )
66
+ for header , cell in zip (headers , cells ):
67
+ data_dict [header .text .lower ()] = cell .text
68
+
69
+ table_data .append (data_dict )
70
+
71
+ return table_data
72
+
73
+
57
74
def test_visible (pytester , path , driver ):
58
75
pytester .makepyfile (
59
76
"""
@@ -76,3 +93,45 @@ def test_pass_two(): pass
76
93
)
77
94
result = driver .find_elements (By .CSS_SELECTOR , "tr.collapsible" )
78
95
assert_that (result ).is_length (0 )
96
+
97
+
98
+ def test_custom_sorting (pytester , path , driver ):
99
+ pytester .makeconftest (
100
+ """
101
+ def pytest_html_results_table_header(cells):
102
+ cells.append(
103
+ '<th class="sortable alpha" data-column-type="alpha">Alpha</th>'
104
+ )
105
+
106
+ def pytest_html_results_table_row(report, cells):
107
+ data = report.nodeid.split("_")[-1]
108
+ cells.append(f'<td class="col-alpha">{data}</td>')
109
+ """
110
+ )
111
+ pytester .makepyfile (
112
+ """
113
+ def test_AAA(): pass
114
+ def test_BBB(): pass
115
+ """
116
+ )
117
+ query_params = _encode_query_params ({"sort" : "alpha" })
118
+ driver .get (f"file:///reports{ path ()} ?{ query_params } " )
119
+ WebDriverWait (driver , 5 ).until (
120
+ ec .visibility_of_all_elements_located ((By .CSS_SELECTOR , "#results-table" ))
121
+ )
122
+
123
+ rows = _parse_result_table (driver )
124
+ assert_that (rows ).is_length (2 )
125
+ assert_that (rows [0 ]["test" ]).contains ("AAA" )
126
+ assert_that (rows [0 ]["alpha" ]).is_equal_to ("AAA" )
127
+ assert_that (rows [1 ]["test" ]).contains ("BBB" )
128
+ assert_that (rows [1 ]["alpha" ]).is_equal_to ("BBB" )
129
+
130
+ driver .find_element (By .CSS_SELECTOR , "th[data-column-type='alpha']" ).click ()
131
+ # we might need some wait here to ensure sorting happened
132
+ rows = _parse_result_table (driver )
133
+ assert_that (rows ).is_length (2 )
134
+ assert_that (rows [0 ]["test" ]).contains ("BBB" )
135
+ assert_that (rows [0 ]["alpha" ]).is_equal_to ("BBB" )
136
+ assert_that (rows [1 ]["test" ]).contains ("AAA" )
137
+ assert_that (rows [1 ]["alpha" ]).is_equal_to ("AAA" )
0 commit comments