From 55b91367179ab97f8b3cc4971746f8919a0999ec Mon Sep 17 00:00:00 2001 From: Yaroslav Afenkin <91559310+yaroslavafenkin@users.noreply.github.com> Date: Fri, 29 Nov 2024 13:11:08 +0200 Subject: [PATCH 1/4] [JENKINS-74897] Extract inline handlers from `TestNGTestResultBuildAction/reportDetails.groovy` --- .../reportDetail.groovy | 22 +++++++++++-------- src/main/webapp/js/toggle_mthd_summary.js | 20 ++++++++++++++++- src/main/webapp/js/toggle_table.js | 12 +++++++++- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/main/resources/hudson/plugins/testng/TestNGTestResultBuildAction/reportDetail.groovy b/src/main/resources/hudson/plugins/testng/TestNGTestResultBuildAction/reportDetail.groovy index dad3d400..854f97fd 100644 --- a/src/main/resources/hudson/plugins/testng/TestNGTestResultBuildAction/reportDetail.groovy +++ b/src/main/resources/hudson/plugins/testng/TestNGTestResultBuildAction/reportDetail.groovy @@ -8,13 +8,13 @@ l = namespace(lib.LayoutTagLib) t = namespace("/lib/hudson") st = namespace("jelly:stapler") -script(src: "${app.rootUrl}/plugin/testng-plugin/js/toggle_table.js") -script(src: "${app.rootUrl}/plugin/testng-plugin/js/toggle_mthd_summary.js") +script(src: "${resURL}/plugin/testng-plugin/js/toggle_table.js") +script(src: "${resURL}/plugin/testng-plugin/js/toggle_mthd_summary.js") h2("Failed Tests") if (my.result.failCount != 0) { - a(href: "javascript:toggleTable('fail-tbl')") { + a(class: "testng-toggle-table", "data-toggle-table-id": "fail-tbl") { text("hide/expand the table") } table(id:"fail-tbl", border:"1px", class:"pane sortable") { @@ -34,10 +34,12 @@ if (my.result.failCount != 0) { def failedTestSafeUpUrl = Functions.jsStringEscape(failedTest.upUrl) tr() { td(align: "left") { - a(id: "${failedTest.id}-showlink", href:"javascript:showStackTrace('${failedTestSafeId}', '${failedTestSafeUpUrl}/summary')") { + a(id: "${failedTest.id}-showlink", class: "testng-show-stack-trace", + "data-failed-test-safe-id": "${failedTestSafeId}", "data-failed-test-safe-up-url": "${failedTestSafeUpUrl}/summary") { text(">>>") } - a(style: "display:none", id: "${failedTest.id}-hidelink", href:"javascript:hideStackTrace('${failedTestSafeId}')") { + a(style: "display:none", id: "${failedTest.id}-hidelink", class: "testng-hide-stack-trace", + "data-failed-test-safe-id": "${failedTestSafeId}") { text("<<<") } text(" ") @@ -76,7 +78,7 @@ if (my.result.skippedConfigCount != 0) { h2("All Tests (grouped by their packages)") -a(href:"javascript:toggleTable('all-tbl')") { +a(class: "testng-toggle-table", "data-toggle-table-id": "all-tbl") { text("hide/expand the table") } @@ -158,7 +160,7 @@ table(id:"all-tbl", border:"1px", class:"pane sortable") { * @return nothing */ def printMethods(type, tableName, methodList, showMoreArrows) { - a(href: "javascript:toggleTable('${tableName}')") { + a(class: "testng-toggle-table", "data-toggle-table-id": "${tableName}") { text("hide/expand the table") } table(id:tableName, border:"1px", class:"pane sortable") { @@ -176,10 +178,12 @@ def printMethods(type, tableName, methodList, showMoreArrows) { tr() { td(align: "left") { if (showMoreArrows) { - a(id: "${method.id}-showlink", href:"javascript:showStackTrace('${methodSafeId}', '${methodSafeUpUrl}/summary')") { + a(id: "${method.id}-showlink", class: "testng-show-stack-trace", + "data-failed-test-safe-id": "${methodSafeId}", "data-failed-test-safe-up-url": "${methodSafeUpUrl}/summary") { text(">>>") } - a(style: "display:none", id: "${method.id}-hidelink", href:"javascript:hideStackTrace('${methodSafeId}')") { + a(style: "display:none", id: "${method.id}-hidelink", class: "testng-hide-stack-trace", + "data-failed-test-safe-id": "${methodSafeId}") { text("<<<") } text(" ") diff --git a/src/main/webapp/js/toggle_mthd_summary.js b/src/main/webapp/js/toggle_mthd_summary.js index abe9352c..b28361e1 100644 --- a/src/main/webapp/js/toggle_mthd_summary.js +++ b/src/main/webapp/js/toggle_mthd_summary.js @@ -14,4 +14,22 @@ function hideStackTrace(id) { document.getElementById(id).style.display = "none"; document.getElementById(id + "-showlink").style.display = ""; document.getElementById(id + "-hidelink").style.display = "none"; -} \ No newline at end of file +} + +document.addEventListener("DOMContentLoaded", () => { + document.querySelectorAll(".testng-show-stack-trace").forEach((button) => { + button.addEventListener("click", (event) => { + const { failedTestSafeId, failedTestSafeUpUrl } = event.target.closest(".testng-show-stack-trace").dataset; + + showStackTrace(failedTestSafeId, failedTestSafeUpUrl); + }); + }); + + document.querySelectorAll(".testng-hide-stack-trace").forEach((button) => { + button.addEventListener("click", (event) => { + const { failedTestSafeId } = event.target.closest(".testng-hide-stack-trace").dataset; + + hideStackTrace(failedTestSafeId); + }); + }); +}); diff --git a/src/main/webapp/js/toggle_table.js b/src/main/webapp/js/toggle_table.js index a37e35d7..451ab349 100644 --- a/src/main/webapp/js/toggle_table.js +++ b/src/main/webapp/js/toggle_table.js @@ -5,4 +5,14 @@ function toggleTable(id) { } else if (document.getElementById(id).style.display == "") { document.getElementById(id).style.display = "none"; } -} \ No newline at end of file +} + +document.addEventListener("DOMContentLoaded", () => { + document.querySelectorAll(".testng-toggle-table").forEach((toggle) => { + toggle.addEventListener("click", (event) => { + const { toggleTableId } = event.target.dataset; + + toggleTable(toggleTableId); + }); + }); +}); \ No newline at end of file From 40183ba642b923b01d3489e0617f6ffa9b1c2cc8 Mon Sep 17 00:00:00 2001 From: Yaroslav Afenkin <91559310+yaroslavafenkin@users.noreply.github.com> Date: Fri, 29 Nov 2024 13:52:58 +0200 Subject: [PATCH 2/4] [JENKINS-74897] Extract inline scripts from `PackageResult/reportDetail.groovy` --- .../results/PackageResult/report-detail.js | 21 ++++++++++++++ .../results/PackageResult/reportDetail.groovy | 28 ++++--------------- 2 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 src/main/resources/hudson/plugins/testng/results/PackageResult/report-detail.js diff --git a/src/main/resources/hudson/plugins/testng/results/PackageResult/report-detail.js b/src/main/resources/hudson/plugins/testng/results/PackageResult/report-detail.js new file mode 100644 index 00000000..0623112d --- /dev/null +++ b/src/main/resources/hudson/plugins/testng/results/PackageResult/report-detail.js @@ -0,0 +1,21 @@ +// Loads data for all the methods +function showAllExecMthds() { + thisPkgResult.getAllSortedTestMethodsByStartTime(function (t) { + document.getElementById("sortedMethods").innerHTML = t.responseObject(); + }); + document.getElementById("showAllLink").style.display = "none"; +} + +document.addEventListener("DOMContentLoaded", () => { + // following script loads the initial table data + thisPkgResult.getFirstXSortedTestMethodsByStartTime(function(t) { + document.getElementById('sortedMethods').innerHTML = t.responseObject(); + }); + + const showAllButton = document.querySelector(".testng-show-all-exec-methods"); + if (showAllButton !== null) { + showAllButton.addEventListener("click", () => { + showAllExecMthds(); + }); + } +}); diff --git a/src/main/resources/hudson/plugins/testng/results/PackageResult/reportDetail.groovy b/src/main/resources/hudson/plugins/testng/results/PackageResult/reportDetail.groovy index 2db07f9c..1e56797f 100644 --- a/src/main/resources/hudson/plugins/testng/results/PackageResult/reportDetail.groovy +++ b/src/main/resources/hudson/plugins/testng/results/PackageResult/reportDetail.groovy @@ -7,20 +7,13 @@ l = namespace(lib.LayoutTagLib) t = namespace("/lib/hudson") st = namespace("jelly:stapler") -script(src:"${app.rootUrl}/plugin/testng-plugin/js/toggle_table.js") +script(src:"${resURL}/plugin/testng-plugin/js/toggle_table.js") //see https://issues.jenkins-ci.org/browse/JENKINS-18867 & https://issues.jenkins-ci.org/browse/JENKINS-18875 -st.bind(var:"thisPkgResult", value:my) -script() { - text("//Loads data for all the methods") - text("\nfunction showAllExecMthds() {") - text("\nthisPkgResult.getAllSortedTestMethodsByStartTime(function(t) {") - text("\ndocument.getElementById('sortedMethods').innerHTML = t.responseObject();") - text("\n})") - text("\ndocument.getElementById(\"showAllLink\").style.display = \"none\"; }") -} +st.bind(var:"thisPkgResult", value: my) +st.adjunct(includes: "hudson.plugins.testng.results.PackageResult.report-detail") h2("All Classes") -a(href:"javascript:toggleTable('allClasses')") { +a(class: "testng-toggle-table", "data-toggle-table-id": "allClasses") { text("hide/expand the table") } @@ -100,13 +93,13 @@ if (my.sortedTestMethodsByStartTime) { div(id:"showAllLink") { p() { text("Showing only first ${my.MAX_EXEC_MTHD_LIST_SIZE} test methods. ") - a(href:"javascript:showAllExecMthds()") { + a(class: "testng-show-all-exec-methods") { text("Click to see all") } } } } - a(href:"javascript:toggleTable('exec-tbl')") { + a(class: "testng-toggle-table", "data-toggle-table-id": "exec-tbl") { text("hide/expand the table") } table(border:"1px", class:"pane sortable", id:"exec-tbl") { @@ -136,12 +129,3 @@ if (my.sortedTestMethodsByStartTime) { } else { div("No Tests found or all Tests were skipped") } - -//following script loads the initial table data -script() { - text("\nvar foo = ") - st.bind(value:my) - text("\nfoo.getFirstXSortedTestMethodsByStartTime(function(t) {") - text("\ndocument.getElementById('sortedMethods').innerHTML = t.responseObject();") - text("\n})") -} \ No newline at end of file From ab01c4e543f6ae144e94539b96027665254849ed Mon Sep 17 00:00:00 2001 From: Yaroslav Afenkin <91559310+yaroslavafenkin@users.noreply.github.com> Date: Fri, 29 Nov 2024 15:23:00 +0200 Subject: [PATCH 3/4] [JENKINS-74897] Extract inline JavaScript from `ClassResult/reportDetail.groovy` --- .../testng/results/ClassResult/reportDetail.groovy | 2 +- src/main/webapp/js/show_more.js | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/resources/hudson/plugins/testng/results/ClassResult/reportDetail.groovy b/src/main/resources/hudson/plugins/testng/results/ClassResult/reportDetail.groovy index 0407d5b5..4ae2c84d 100644 --- a/src/main/resources/hudson/plugins/testng/results/ClassResult/reportDetail.groovy +++ b/src/main/resources/hudson/plugins/testng/results/ClassResult/reportDetail.groovy @@ -53,7 +53,7 @@ for (group in my.testRunMap.values()) { if (method.groups || method.testInstanceName || method.parameters?.size() > 0) { div(id:"${method.safeName}_1", style:"display:inline") { text(" (") - a(href:"javascript:showMore(\"${methodJsSafeName}\")") { + a(class: "testng-show-more", "data-method-name": "${methodJsSafeName}") { raw("…") } text(")") diff --git a/src/main/webapp/js/show_more.js b/src/main/webapp/js/show_more.js index 4ac04298..9eb5e8fb 100644 --- a/src/main/webapp/js/show_more.js +++ b/src/main/webapp/js/show_more.js @@ -1,4 +1,14 @@ function showMore(id) { document.getElementById(id + "_1").style.display = "none"; document.getElementById(id + "_2").style.display = ""; -} \ No newline at end of file +} + +document.addEventListener("DOMContentLoaded", () => { + document.querySelectorAll(".testng-show-more").forEach((button) => { + button.addEventListener("click", (event) => { + const { methodName } = event.target.dataset; + + showMore(methodName); + }) + }); +}); From 9bea838f779ff4675fe9d5804915525554fc6557 Mon Sep 17 00:00:00 2001 From: Yaroslav Afenkin <91559310+yaroslavafenkin@users.noreply.github.com> Date: Fri, 29 Nov 2024 15:29:56 +0200 Subject: [PATCH 4/4] [JENKINS-74897] Restore hrefs on `a` tags for `cursor: pointer` --- .../reportDetail.groovy | 14 +++++++------- .../testng/results/ClassResult/reportDetail.groovy | 2 +- .../testng/results/PackageResult/report-detail.js | 3 ++- .../results/PackageResult/reportDetail.groovy | 6 +++--- src/main/webapp/js/show_more.js | 1 + src/main/webapp/js/toggle_mthd_summary.js | 2 ++ src/main/webapp/js/toggle_table.js | 3 ++- 7 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/resources/hudson/plugins/testng/TestNGTestResultBuildAction/reportDetail.groovy b/src/main/resources/hudson/plugins/testng/TestNGTestResultBuildAction/reportDetail.groovy index 854f97fd..84e1dc69 100644 --- a/src/main/resources/hudson/plugins/testng/TestNGTestResultBuildAction/reportDetail.groovy +++ b/src/main/resources/hudson/plugins/testng/TestNGTestResultBuildAction/reportDetail.groovy @@ -14,7 +14,7 @@ script(src: "${resURL}/plugin/testng-plugin/js/toggle_mthd_summary.js") h2("Failed Tests") if (my.result.failCount != 0) { - a(class: "testng-toggle-table", "data-toggle-table-id": "fail-tbl") { + a(href: "", class: "testng-toggle-table", "data-toggle-table-id": "fail-tbl") { text("hide/expand the table") } table(id:"fail-tbl", border:"1px", class:"pane sortable") { @@ -34,11 +34,11 @@ if (my.result.failCount != 0) { def failedTestSafeUpUrl = Functions.jsStringEscape(failedTest.upUrl) tr() { td(align: "left") { - a(id: "${failedTest.id}-showlink", class: "testng-show-stack-trace", + a(href: "", id: "${failedTest.id}-showlink", class: "testng-show-stack-trace", "data-failed-test-safe-id": "${failedTestSafeId}", "data-failed-test-safe-up-url": "${failedTestSafeUpUrl}/summary") { text(">>>") } - a(style: "display:none", id: "${failedTest.id}-hidelink", class: "testng-hide-stack-trace", + a(href: "", style: "display:none", id: "${failedTest.id}-hidelink", class: "testng-hide-stack-trace", "data-failed-test-safe-id": "${failedTestSafeId}") { text("<<<") } @@ -78,7 +78,7 @@ if (my.result.skippedConfigCount != 0) { h2("All Tests (grouped by their packages)") -a(class: "testng-toggle-table", "data-toggle-table-id": "all-tbl") { +a(href: "", class: "testng-toggle-table", "data-toggle-table-id": "all-tbl") { text("hide/expand the table") } @@ -160,7 +160,7 @@ table(id:"all-tbl", border:"1px", class:"pane sortable") { * @return nothing */ def printMethods(type, tableName, methodList, showMoreArrows) { - a(class: "testng-toggle-table", "data-toggle-table-id": "${tableName}") { + a(href: "", class: "testng-toggle-table", "data-toggle-table-id": "${tableName}") { text("hide/expand the table") } table(id:tableName, border:"1px", class:"pane sortable") { @@ -178,11 +178,11 @@ def printMethods(type, tableName, methodList, showMoreArrows) { tr() { td(align: "left") { if (showMoreArrows) { - a(id: "${method.id}-showlink", class: "testng-show-stack-trace", + a(href: "", id: "${method.id}-showlink", class: "testng-show-stack-trace", "data-failed-test-safe-id": "${methodSafeId}", "data-failed-test-safe-up-url": "${methodSafeUpUrl}/summary") { text(">>>") } - a(style: "display:none", id: "${method.id}-hidelink", class: "testng-hide-stack-trace", + a(href: "", style: "display:none", id: "${method.id}-hidelink", class: "testng-hide-stack-trace", "data-failed-test-safe-id": "${methodSafeId}") { text("<<<") } diff --git a/src/main/resources/hudson/plugins/testng/results/ClassResult/reportDetail.groovy b/src/main/resources/hudson/plugins/testng/results/ClassResult/reportDetail.groovy index 4ae2c84d..5e5fe7f4 100644 --- a/src/main/resources/hudson/plugins/testng/results/ClassResult/reportDetail.groovy +++ b/src/main/resources/hudson/plugins/testng/results/ClassResult/reportDetail.groovy @@ -53,7 +53,7 @@ for (group in my.testRunMap.values()) { if (method.groups || method.testInstanceName || method.parameters?.size() > 0) { div(id:"${method.safeName}_1", style:"display:inline") { text(" (") - a(class: "testng-show-more", "data-method-name": "${methodJsSafeName}") { + a(href: "", class: "testng-show-more", "data-method-name": "${methodJsSafeName}") { raw("…") } text(")") diff --git a/src/main/resources/hudson/plugins/testng/results/PackageResult/report-detail.js b/src/main/resources/hudson/plugins/testng/results/PackageResult/report-detail.js index 0623112d..24f27acf 100644 --- a/src/main/resources/hudson/plugins/testng/results/PackageResult/report-detail.js +++ b/src/main/resources/hudson/plugins/testng/results/PackageResult/report-detail.js @@ -14,7 +14,8 @@ document.addEventListener("DOMContentLoaded", () => { const showAllButton = document.querySelector(".testng-show-all-exec-methods"); if (showAllButton !== null) { - showAllButton.addEventListener("click", () => { + showAllButton.addEventListener("click", (event) => { + event.preventDefault(); showAllExecMthds(); }); } diff --git a/src/main/resources/hudson/plugins/testng/results/PackageResult/reportDetail.groovy b/src/main/resources/hudson/plugins/testng/results/PackageResult/reportDetail.groovy index 1e56797f..971d27bf 100644 --- a/src/main/resources/hudson/plugins/testng/results/PackageResult/reportDetail.groovy +++ b/src/main/resources/hudson/plugins/testng/results/PackageResult/reportDetail.groovy @@ -13,7 +13,7 @@ st.bind(var:"thisPkgResult", value: my) st.adjunct(includes: "hudson.plugins.testng.results.PackageResult.report-detail") h2("All Classes") -a(class: "testng-toggle-table", "data-toggle-table-id": "allClasses") { +a(href: "", class: "testng-toggle-table", "data-toggle-table-id": "allClasses") { text("hide/expand the table") } @@ -93,13 +93,13 @@ if (my.sortedTestMethodsByStartTime) { div(id:"showAllLink") { p() { text("Showing only first ${my.MAX_EXEC_MTHD_LIST_SIZE} test methods. ") - a(class: "testng-show-all-exec-methods") { + a(href: "", class: "testng-show-all-exec-methods") { text("Click to see all") } } } } - a(class: "testng-toggle-table", "data-toggle-table-id": "exec-tbl") { + a(href: "", class: "testng-toggle-table", "data-toggle-table-id": "exec-tbl") { text("hide/expand the table") } table(border:"1px", class:"pane sortable", id:"exec-tbl") { diff --git a/src/main/webapp/js/show_more.js b/src/main/webapp/js/show_more.js index 9eb5e8fb..c343a826 100644 --- a/src/main/webapp/js/show_more.js +++ b/src/main/webapp/js/show_more.js @@ -6,6 +6,7 @@ function showMore(id) { document.addEventListener("DOMContentLoaded", () => { document.querySelectorAll(".testng-show-more").forEach((button) => { button.addEventListener("click", (event) => { + event.preventDefault(); const { methodName } = event.target.dataset; showMore(methodName); diff --git a/src/main/webapp/js/toggle_mthd_summary.js b/src/main/webapp/js/toggle_mthd_summary.js index b28361e1..2783eca9 100644 --- a/src/main/webapp/js/toggle_mthd_summary.js +++ b/src/main/webapp/js/toggle_mthd_summary.js @@ -19,6 +19,7 @@ function hideStackTrace(id) { document.addEventListener("DOMContentLoaded", () => { document.querySelectorAll(".testng-show-stack-trace").forEach((button) => { button.addEventListener("click", (event) => { + event.preventDefault(); const { failedTestSafeId, failedTestSafeUpUrl } = event.target.closest(".testng-show-stack-trace").dataset; showStackTrace(failedTestSafeId, failedTestSafeUpUrl); @@ -27,6 +28,7 @@ document.addEventListener("DOMContentLoaded", () => { document.querySelectorAll(".testng-hide-stack-trace").forEach((button) => { button.addEventListener("click", (event) => { + event.preventDefault(); const { failedTestSafeId } = event.target.closest(".testng-hide-stack-trace").dataset; hideStackTrace(failedTestSafeId); diff --git a/src/main/webapp/js/toggle_table.js b/src/main/webapp/js/toggle_table.js index 451ab349..267389a4 100644 --- a/src/main/webapp/js/toggle_table.js +++ b/src/main/webapp/js/toggle_table.js @@ -10,9 +10,10 @@ function toggleTable(id) { document.addEventListener("DOMContentLoaded", () => { document.querySelectorAll(".testng-toggle-table").forEach((toggle) => { toggle.addEventListener("click", (event) => { + event.preventDefault(); const { toggleTableId } = event.target.dataset; toggleTable(toggleTableId); }); }); -}); \ No newline at end of file +});