Skip to content

Commit 1b8ec70

Browse files
committed
fix: Table hooks
1 parent 079681d commit 1b8ec70

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

src/pytest_html/nextgen.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,25 @@ class Cells:
3737
def __init__(self):
3838
self._html = {}
3939

40+
def __delitem__(self, key):
41+
# This means the item should be removed
42+
self._html = None
43+
4044
@property
4145
def html(self):
4246
return self._html
4347

4448
def insert(self, index, html):
49+
# backwards-compat
50+
if not isinstance(html, str):
51+
if html.__module__.startswith("py."):
52+
warnings.warn(
53+
"The 'py' module is deprecated and support "
54+
"will be removed in a future release.",
55+
DeprecationWarning,
56+
)
57+
html = str(html)
58+
html = html.replace("col", "data-column-type")
4559
self._html[index] = html
4660

4761
class Report:
@@ -219,6 +233,7 @@ def pytest_sessionstart(self, session):
219233

220234
header_cells = self.Cells()
221235
session.config.hook.pytest_html_results_table_header(cells=header_cells)
236+
222237
self._report.set_data("resultsTableHeader", header_cells.html)
223238

224239
self._report.set_data("runningState", "Started")
@@ -257,6 +272,13 @@ def pytest_runtest_logreport(self, report):
257272
"when": report.when,
258273
}
259274

275+
row_cells = self.Cells()
276+
self._config.hook.pytest_html_results_table_row(report=report, cells=row_cells)
277+
if row_cells.html is None:
278+
return
279+
280+
data["resultsTableRow"] = row_cells.html
281+
260282
test_id = report.nodeid
261283
if report.when != "call":
262284
test_id += f"::{report.when}"
@@ -268,10 +290,6 @@ def pytest_runtest_logreport(self, report):
268290

269291
data["result"] = _process_outcome(report)
270292

271-
row_cells = self.Cells()
272-
self._config.hook.pytest_html_results_table_row(report=report, cells=row_cells)
273-
data["resultsTableRow"] = row_cells.html
274-
275293
table_html = []
276294
self._config.hook.pytest_html_results_table_html(report=report, data=table_html)
277295
data["tableHtml"] = table_html

src/pytest_html/scripts/dom.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,28 @@ const dom = {
5252
const header = listHeader.content.cloneNode(true)
5353
const sortAttr = storageModule.getSort()
5454
const sortAsc = JSON.parse(storageModule.getSortDirection())
55-
const sortables = ['result', 'testId', 'duration']
55+
56+
const regex = /data-column-type="(\w+)/
57+
const cols = Object.values(resultsTableHeader).reduce((result, value) => {
58+
if (value.includes("sortable")) {
59+
const matches = regex.exec(value)
60+
if (matches) {
61+
result.push(matches[1])
62+
}
63+
}
64+
return result
65+
}, [])
66+
const sortables = ['result', 'testId', 'duration', ...cols]
67+
68+
// Add custom html from the pytest_html_results_table_header hook
69+
insertAdditionalHTML(resultsTableHeader, header, 'th')
5670

5771
sortables.forEach((sortCol) => {
5872
if (sortCol === sortAttr) {
5973
header.querySelector(`[data-column-type="${sortCol}"]`).classList.add(sortAsc ? 'desc' : 'asc')
6074
}
6175
})
6276

63-
// Add custom html from the pytest_html_results_table_header hook
64-
insertAdditionalHTML(resultsTableHeader, header, 'th')
65-
6677
return header
6778
},
6879
getListHeaderEmpty: () => listHeaderEmpty.content.cloneNode(true),

src/pytest_html/scripts/storage.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const possibleFiltes = ['passed', 'skipped', 'failed', 'error', 'xfailed', 'xpassed', 'rerun']
1+
const possibleFilters = ['passed', 'skipped', 'failed', 'error', 'xfailed', 'xpassed', 'rerun']
22

33
const getVisible = () => {
44
const url = new URL(window.location.href)
55
const settings = new URLSearchParams(url.search).get('visible') || ''
66
return settings ?
7-
[...new Set(settings.split(',').filter((filter) => possibleFiltes.includes(filter)))] : possibleFiltes
7+
[...new Set(settings.split(',').filter((filter) => possibleFilters.includes(filter)))] : possibleFilters
88
}
99
const hideCategory = (categoryToHide) => {
1010
const url = new URL(window.location.href)
1111
const visibleParams = new URLSearchParams(url.search).get('visible')
12-
const currentVisible = visibleParams ? visibleParams.split(',') : [...possibleFiltes]
12+
const currentVisible = visibleParams ? visibleParams.split(',') : [...possibleFilters]
1313
const settings = [...new Set(currentVisible)].filter((f) => f !== categoryToHide).join(',')
1414

1515
url.searchParams.set('visible', settings)
@@ -21,15 +21,15 @@ const showCategory = (categoryToShow) => {
2121
return
2222
}
2323
const url = new URL(window.location.href)
24-
const currentVisible = new URLSearchParams(url.search).get('visible')?.split(',') || [...possibleFiltes]
24+
const currentVisible = new URLSearchParams(url.search).get('visible')?.split(',') || [...possibleFilters]
2525
const settings = [...new Set([categoryToShow, ...currentVisible])]
26-
const noFilter = possibleFiltes.length === settings.length || !settings.length
26+
const noFilter = possibleFilters.length === settings.length || !settings.length
2727

2828
noFilter ? url.searchParams.delete('visible') : url.searchParams.set('visible', settings.join(','))
2929
history.pushState({}, null, unescape(url.href))
3030
}
3131
const setFilter = (currentFilter) => {
32-
if (!possibleFiltes.includes(currentFilter)) {
32+
if (!possibleFilters.includes(currentFilter)) {
3333
return
3434
}
3535
const url = new URL(window.location.href)

testing/test_integration.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def run(pytester, path="report.html", *args):
3131
pytester.runpytest("-s", "--html", path, *args)
3232

3333
chrome_options = webdriver.ChromeOptions()
34-
chrome_options.add_argument("--headless")
34+
# chrome_options.add_argument("--headless")
3535
chrome_options.add_argument("--window-size=1920x1080")
3636
# chrome_options.add_argument("--allow-file-access-from-files")
3737
driver = webdriver.Remote(
@@ -49,6 +49,9 @@ def run(pytester, path="report.html", *args):
4949
# End workaround
5050

5151
driver.get(f"file:///reports{path}")
52+
from time import sleep
53+
54+
sleep(200)
5255
return BeautifulSoup(driver.page_source, "html.parser")
5356
finally:
5457
driver.quit()
@@ -476,3 +479,27 @@ def test_xdist(self, pytester):
476479
pytester.makepyfile("def test_xdist(): pass")
477480
page = run(pytester, "report.html", "-n1")
478481
assert_results(page, passed=1)
482+
483+
def test_table_header_hook_insert(self, pytester):
484+
pytester.makeconftest(
485+
"""
486+
from py.xml import html
487+
def pytest_html_results_table_header(cells):
488+
cells.insert(2, html.th("Description"))
489+
cells.insert(1, html.th("Time", class_="sortable time", col="time"))
490+
# cells.pop()
491+
492+
def pytest_html_results_table_row(report, cells):
493+
cells.insert(2, html.td("A description"))
494+
cells.insert(1, html.td("A time", class_="col-time"))
495+
# cells.pop()
496+
"""
497+
)
498+
pytester.makepyfile("def test_pass(): pass")
499+
run(pytester)
500+
501+
def test_table_row_hook_insert(self):
502+
pass
503+
504+
def test_table_row_hook_delete(self):
505+
pass

0 commit comments

Comments
 (0)