Skip to content

Commit

Permalink
Remove javascript in HtmlLogger (#4744)
Browse files Browse the repository at this point in the history
Use summary tag instead of JavaScript.

---------

Co-authored-by: David Mueller x <david.x.mueller@getinge.com>
  • Loading branch information
daveMueller and David Mueller x committed Nov 14, 2023
1 parent d9e9233 commit 21b9259
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
40 changes: 18 additions & 22 deletions src/Microsoft.TestPlatform.Extensions.HtmlLogger/Html.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@
<h1>Test run details</h1>
<xsl:apply-templates select ="/tp:TestRunDetails"/>
</body>
<script language="javascript">
function ToggleClass(id) {
var elem = document.getElementById(id);
if (elem.style.display == "none") {
elem.style.display = "block";
}
else {
elem.style.display = "none";
}
}
</script>
<style>
<style>
body { font-family: Calibri, Verdana, Arial, sans-serif; background-color: White; color: Black; }
h2 {
margin-top: 15px;
Expand Down Expand Up @@ -132,10 +121,12 @@
<xsl:for-each select ="tp:TestResultCollection">
<xsl:variable name="Source" select="tp:Id" />
<xsl:if test="tp:ResultList!=''">
<div class ="list-row" onclick="ToggleClass('{$Source}')"><xsl:value-of select = "tp:Source" /></div>
<div class ="inner-row" Id="{$Source}" style="display:none;">
<xsl:for-each select ="tp:ResultList/tp:TestResult"><xsl:call-template name ="TestResult"/></xsl:for-each>
</div>
<details>
<summary><xsl:value-of select = "tp:Source" /></summary>
<div class ="inner-row" Id="{$Source}">
<xsl:for-each select ="tp:ResultList/tp:TestResult"><xsl:call-template name ="TestResult"/></xsl:for-each>
</div>
</details>
</xsl:if>
</xsl:for-each>
</xsl:template>
Expand All @@ -144,10 +135,13 @@
<xsl:for-each select ="tp:TestResultCollection">
<xsl:variable name="Source" select="tp:Id" />
<xsl:if test="tp:FailedResultList!=''">
<div class ="list-row" onclick="ToggleClass('{concat($Source,'-failedResult')}')"><xsl:value-of select = "tp:Source" /> </div>
<div class ="inner-row" Id="{concat($Source,'-failedResult')}" style="display:block;">
<xsl:for-each select ="tp:FailedResultList/tp:TestResult"><xsl:call-template name ="TestResult"/></xsl:for-each>
</div>
<details>
<xsl:attribute name="open"/>
<summary><xsl:value-of select = "tp:Source" /></summary>
<div class ="inner-row" Id="{concat($Source,'-failedResult')}">
<xsl:for-each select ="tp:FailedResultList/tp:TestResult"><xsl:call-template name ="TestResult"/></xsl:for-each>
</div>
</details>
</xsl:if>
</xsl:for-each>
</xsl:template>
Expand All @@ -156,8 +150,10 @@
<xsl:variable name="TestResultId" select="tp:TestResultId" />

<xsl:if test ="tp:InnerTestResults!=''">
<div class ="row" onclick="ToggleClass('{concat($TestResultId,'-',name(..))}')"><xsl:call-template name = "Result" /></div>
<a Id="{concat($TestResultId,'-',name(..))}" style="display:none;"><xsl:apply-templates select = "tp:InnerTestResults" /></a>
<details>
<summary><xsl:call-template name = "Result" /></summary>
<a Id="{concat($TestResultId,'-',name(..))}"><xsl:apply-templates select = "tp:InnerTestResults" /></a>
</details>
</xsl:if>

<xsl:if test ="tp:InnerTestResults=''">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ public void HtmlLoggerWithFriendlyNameShouldProperlyOverwriteFile(RunnerInfo run
IsFileAndContentEqual(htmlLogFilePath);
}

[TestMethod]
[TestCategory("Windows-Review")]
[NetFullTargetFrameworkDataSource(inIsolation: true, inProcess: true)]
public void HtmlLoggerWithFriendlyNameContainsExpectedContent(RunnerInfo runnerInfo)
{
SetTestEnvironment(_testEnvironment, runnerInfo);

var arguments = PrepareArguments(GetSampleTestAssembly(), GetTestAdapterPath(), string.Empty, FrameworkArgValue, runnerInfo.InIsolationValue, TempDirectory.Path);
var htmlFileName = "TestResults.html";
arguments = string.Concat(arguments, $" /logger:\"html;LogFileName={htmlFileName}\"");
InvokeVsTest(arguments);

var htmlLogFilePath = Path.Combine(TempDirectory.Path, htmlFileName);
XmlDocument report = new();
report.Load(htmlLogFilePath);

AssertExpectedHtml(report.DocumentElement!);
}

[TestMethod]
[NetCoreTargetFrameworkDataSource]
public void TrxLoggerWithExecutorUriShouldProperlyOverwriteFile(RunnerInfo runnerInfo)
Expand Down Expand Up @@ -163,6 +182,21 @@ public void TrxLoggerResultSummaryOutcomeValueShouldNotChangeIfNoTestsExecutedAn
Assert.AreEqual("Completed", outcomeValue);
}

private static void AssertExpectedHtml(XmlElement root)
{
XmlNodeList elementList = root.GetElementsByTagName("details");
Assert.AreEqual(2, elementList.Count);

foreach (XmlElement element in elementList)
{
Assert.AreEqual("summary", element.FirstChild?.Name);
if (element.HasAttributes)
{
Assert.AreEqual("open", element.Attributes[0].Name);
}
}
}

private static bool IsValidXml(string xmlFilePath)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ <h2>Results</h2>
.duration{float:right;}

</style>
</html>
</html>

0 comments on commit 21b9259

Please sign in to comment.