|
1 |
| -const { formatDuration } = require('./utils.js'); |
2 |
| -const imgViewer = require('./imgViewer.js'); |
3 |
| -const templateEnvRow = document.querySelector('#template_environment_row'); |
4 |
| -const templateResult = document.querySelector('#template_results-table__tbody'); |
5 |
| -const aTag = document.querySelector('#template_a'); |
6 |
| -const aTagImg = document.querySelector('#template_img'); |
7 |
| -const listHeader = document.querySelector('#template_results-table__head'); |
8 |
| -const listHeaderEmpty = document.querySelector('#template_results-table__head--empty'); |
| 1 | +const { formatDuration } = require('./utils.js') |
| 2 | +const imgViewer = require('./imgViewer.js') |
| 3 | +const templateEnvRow = document.querySelector('#template_environment_row') |
| 4 | +const templateResult = document.querySelector('#template_results-table__tbody') |
| 5 | +const aTag = document.querySelector('#template_a') |
| 6 | +const listHeader = document.querySelector('#template_results-table__head') |
| 7 | +const listHeaderEmpty = document.querySelector('#template_results-table__head--empty') |
9 | 8 |
|
10 | 9 | function htmlToElements(html) {
|
11 |
| - let temp = document.createElement('template'); |
12 |
| - temp.innerHTML = html; |
13 |
| - return temp.content.childNodes; |
| 10 | + const temp = document.createElement('template') |
| 11 | + temp.innerHTML = html |
| 12 | + return temp.content.childNodes |
14 | 13 | }
|
15 | 14 |
|
16 | 15 | const find = (selector, elem) => {
|
17 |
| - if (!elem) { |
18 |
| - elem = document; |
19 |
| - } |
20 |
| - return elem.querySelector(selector); |
21 |
| -}; |
| 16 | + if (!elem) { |
| 17 | + elem = document |
| 18 | + } |
| 19 | + return elem.querySelector(selector) |
| 20 | +} |
22 | 21 |
|
23 | 22 | const findAll = (selector, elem) => {
|
24 |
| - if (!elem) { |
25 |
| - elem = document; |
26 |
| - } |
27 |
| - return [...elem.querySelectorAll(selector)]; |
28 |
| -}; |
| 23 | + if (!elem) { |
| 24 | + elem = document |
| 25 | + } |
| 26 | + return [...elem.querySelectorAll(selector)] |
| 27 | +} |
29 | 28 |
|
30 | 29 | const insertAdditionalHTML = (html, element, selector) => {
|
31 |
| - Object.keys(html).map((key) => { |
32 |
| - element.querySelectorAll(selector).item(key).insertAdjacentHTML('beforebegin', html[key]); |
33 |
| - }); |
34 |
| -}; |
| 30 | + Object.keys(html).map((key) => { |
| 31 | + element.querySelectorAll(selector).item(key).insertAdjacentHTML('beforebegin', html[key]) |
| 32 | + }) |
| 33 | +} |
35 | 34 |
|
36 | 35 | const dom = {
|
37 |
| - getStaticRow: (key, value) => { |
38 |
| - const envRow = templateEnvRow.content.cloneNode(true); |
39 |
| - const isObj = typeof value === 'object' && value !== null; |
40 |
| - const values = isObj |
41 |
| - ? Object.keys(value).map((k) => `${k}: ${value[k]}`) |
42 |
| - : null; |
43 |
| - |
44 |
| - const valuesElement = htmlToElements( |
45 |
| - values |
46 |
| - ? `<ul>${values.map((val) => `<li>${val}</li>`).join('')}<ul>` |
47 |
| - : `<div>${value}</div>` |
48 |
| - )[0]; |
49 |
| - var td = findAll('td', envRow); |
50 |
| - td[0].textContent = key; |
51 |
| - td[1].appendChild(valuesElement); |
52 |
| - |
53 |
| - return envRow; |
54 |
| - }, |
55 |
| - getListHeader: ({resultsTableHeader}) => { |
56 |
| - const header = listHeader.content.cloneNode(true); |
57 |
| - const sortAttr = localStorage.getItem('sort'); |
58 |
| - const sortAsc = JSON.parse(localStorage.getItem('sortAsc')); |
59 |
| - const sortables = ['outcome', 'nodeid', 'duration']; |
60 |
| - |
61 |
| - sortables.forEach((sortCol) => { |
62 |
| - if (sortCol === sortAttr) { |
63 |
| - header.querySelector(`[data-column-type="${sortCol}"]`).classList.add( |
64 |
| - sortAsc ? 'desc' : 'asc' |
65 |
| - ); |
66 |
| - } |
67 |
| - }); |
68 |
| - |
69 |
| - // Add custom html from the pytest_html_results_table_header hook |
70 |
| - insertAdditionalHTML(resultsTableHeader, header, 'th'); |
71 |
| - |
72 |
| - return header; |
73 |
| - }, |
74 |
| - getListHeaderEmpty: () => listHeaderEmpty.content.cloneNode(true), |
75 |
| - getResultTBody: ({ nodeid, longrepr, duration, extras, resultsTableRow, tableHtml }, outcome) => { |
76 |
| - const outcomeLower = outcome.toLowerCase(); |
77 |
| - const resultBody = templateResult.content.cloneNode(true); |
78 |
| - resultBody.querySelector('tbody').classList.add(outcomeLower); |
79 |
| - resultBody.querySelector('.col-result').innerText = outcome; |
80 |
| - resultBody.querySelector('.col-name').innerText = nodeid; |
81 |
| - resultBody.querySelector('.col-duration').innerText = `${formatDuration(duration)}s`; |
82 |
| - if (['failed', 'error', 'skipped', 'xfailed', 'xpassed'].includes(outcomeLower)) { |
83 |
| - resultBody.querySelector('.log').innerText = longrepr |
84 |
| - ? longrepr.reprtraceback.reprentries[0].data.lines.join('\n') |
85 |
| - : ''; |
86 |
| - } else { |
87 |
| - resultBody.querySelector('.extras-row').classList.add('hidden'); |
88 |
| - } |
89 |
| - const images = [] |
90 |
| - extras && |
91 |
| - extras.forEach(({ name, format_type, content }) => { |
92 |
| - const extraLink = aTag.content.cloneNode(true); |
93 |
| - const extraLinkItem = extraLink.querySelector('a'); |
94 |
| - const folderItems = ['image', 'video', 'text', 'html', 'json']; |
95 |
| - |
96 |
| - extraLinkItem.href = content; |
97 |
| - extraLinkItem.className = `col-links__extra ${format_type}`; |
98 |
| - extraLinkItem.innerText = name; |
99 |
| - resultBody.querySelector('.col-links').appendChild(extraLinkItem); |
100 |
| - |
101 |
| - if (format_type === 'image') { |
102 |
| - images.push({path: content, name}) |
| 36 | + getStaticRow: (key, value) => { |
| 37 | + const envRow = templateEnvRow.content.cloneNode(true) |
| 38 | + const isObj = typeof value === 'object' && value !== null |
| 39 | + const values = isObj ? Object.keys(value).map((k) => `${k}: ${value[k]}`) : null |
| 40 | + |
| 41 | + const valuesElement = htmlToElements( |
| 42 | + values ? `<ul>${values.map((val) => `<li>${val}</li>`).join('')}<ul>` : `<div>${value}</div>`)[0] |
| 43 | + const td = findAll('td', envRow) |
| 44 | + td[0].textContent = key |
| 45 | + td[1].appendChild(valuesElement) |
| 46 | + |
| 47 | + return envRow |
| 48 | + }, |
| 49 | + getListHeader: ({ resultsTableHeader }) => { |
| 50 | + const header = listHeader.content.cloneNode(true) |
| 51 | + const sortAttr = localStorage.getItem('sort') |
| 52 | + const sortAsc = JSON.parse(localStorage.getItem('sortAsc')) |
| 53 | + const sortables = ['outcome', 'nodeid', 'duration'] |
| 54 | + |
| 55 | + sortables.forEach((sortCol) => { |
| 56 | + if (sortCol === sortAttr) { |
| 57 | + header.querySelector(`[data-column-type="${sortCol}"]`).classList.add(sortAsc ? 'desc' : 'asc') |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + // Add custom html from the pytest_html_results_table_header hook |
| 62 | + insertAdditionalHTML(resultsTableHeader, header, 'th') |
| 63 | + |
| 64 | + return header |
| 65 | + }, |
| 66 | + getListHeaderEmpty: () => listHeaderEmpty.content.cloneNode(true), |
| 67 | + getResultTBody: ({ nodeid, longrepr, duration, extras, resultsTableRow, tableHtml }, outcome) => { |
| 68 | + const outcomeLower = outcome.toLowerCase() |
| 69 | + const resultBody = templateResult.content.cloneNode(true) |
| 70 | + resultBody.querySelector('tbody').classList.add(outcomeLower) |
| 71 | + resultBody.querySelector('.col-result').innerText = outcome |
| 72 | + resultBody.querySelector('.col-name').innerText = nodeid |
| 73 | + resultBody.querySelector('.col-duration').innerText = `${formatDuration(duration)}s` |
| 74 | + if (['failed', 'error', 'skipped', 'xfailed', 'xpassed'].includes(outcomeLower)) { |
| 75 | + resultBody.querySelector('.log').innerText = longrepr ? |
| 76 | + longrepr.reprtraceback.reprentries[0].data.lines.join('\n') : '' |
| 77 | + } else { |
| 78 | + resultBody.querySelector('.extras-row').classList.add('hidden') |
103 | 79 | }
|
104 |
| - }); |
105 |
| - imgViewer.setupImgViewer(resultBody, images) |
106 |
| - |
107 |
| - // Add custom html from the pytest_html_results_table_row hook |
108 |
| - resultsTableRow && |
109 |
| - insertAdditionalHTML(resultsTableRow, resultBody, 'td'); |
110 |
| - |
111 |
| - // Add custom html from the pytest_html_results_table_html hook |
112 |
| - tableHtml && |
113 |
| - tableHtml.forEach((item) => { |
114 |
| - resultBody.querySelector('td[class="extra"]').insertAdjacentHTML('beforeend', item); |
115 |
| - }); |
116 |
| - |
117 |
| - return resultBody; |
118 |
| - }, |
119 |
| -}; |
| 80 | + const images = [] |
| 81 | + extras?.forEach(({ name, format_type, content }) => { |
| 82 | + const extraLink = aTag.content.cloneNode(true) |
| 83 | + const extraLinkItem = extraLink.querySelector('a') |
| 84 | + |
| 85 | + extraLinkItem.href = content |
| 86 | + extraLinkItem.className = `col-links__extra ${format_type}` |
| 87 | + extraLinkItem.innerText = name |
| 88 | + resultBody.querySelector('.col-links').appendChild(extraLinkItem) |
| 89 | + |
| 90 | + if (format_type === 'image') { |
| 91 | + images.push({ path: content, name }) |
| 92 | + } |
| 93 | + }) |
| 94 | + imgViewer.setupImgViewer(resultBody, images) |
| 95 | + |
| 96 | + // Add custom html from the pytest_html_results_table_row hook |
| 97 | + resultsTableRow && insertAdditionalHTML(resultsTableRow, resultBody, 'td') |
| 98 | + |
| 99 | + // Add custom html from the pytest_html_results_table_html hook |
| 100 | + tableHtml?.forEach((item) => { |
| 101 | + resultBody.querySelector('td[class="extra"]').insertAdjacentHTML('beforeend', item) |
| 102 | + }) |
| 103 | + |
| 104 | + return resultBody |
| 105 | + }, |
| 106 | +} |
120 | 107 |
|
121 | 108 | exports.dom = dom
|
122 | 109 | exports.htmlToElements = htmlToElements
|
|
0 commit comments