我有这个 Powershell 功能:
function reportxml {
$logdir = "C:\\Documents\scriptOutput"
$XML_Path = $logdir + '\TR.xml'
$XML_NEW_PATH = $logdir + '\TestResult.xml'
$DLL_PATH = "tests.dll"
$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true
$xmlsettings.IndentChars = "`t"
$XmlWriter = [System.XML.XmlWriter]::Create("$logdir\TestResult.xml", $xmlsettings)
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement('assemblies')
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
$xmlOld = [System.Xml.XmlDocument](Get-Content $XML_Path);
$xmlDoc = [System.Xml.XmlDocument](Get-Content "$logdir\TestResult.xml");
$siteCollectionNode = $xmlDoc.CreateElement("assembly")
$xmlDoc.SelectSingleNode("//assemblies").AppendChild($siteCollectionNode)
$siteCollectionNode.SetAttribute("name", $DLL_PATH)
$siteCollectionNode.SetAttribute("total", $xmlOld.TestRun.ResultSummary.Counters.total)
$siteCollectionNode.SetAttribute("passed", $xmlOld.TestRun.ResultSummary.Counters.passed)
$siteCollectionNode.SetAttribute("failed", $xmlOld.TestRun.ResultSummary.Counters.failed)
$xmlDoc.Save($XML_NEW_PATH)
$siteCollectionNode.AppendChild($xmlDoc.CreateElement("errors"));
$xmlDoc.Save($XML_NEW_PATH)
$subSitesNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("collection"))
$subSitesNode.SetAttribute("Name", "UAT Smoke Tests")
$subSitesNode.SetAttribute("total", $xmlOld.TestRun.ResultSummary.Counters.total)
$subSitesNode.SetAttribute("passed", $xmlOld.TestRun.ResultSummary.Counters.passed)
$subSitesNode.SetAttribute("failed", $xmlOld.TestRun.ResultSummary.Counters.failed)
$xmlDoc.Save($XML_NEW_PATH)
$TestResultNodes = $xmlOld.TestRun.Results.UnitTestResult
$TestResultNodes | ForEach-Object {
$subSiteNameNode = $subSitesNode.AppendChild($xmlDoc.CreateElement("test"));
$subSiteNameNode.SetAttribute("method",$xmlOld.TestRun.Results.UnitTestResult.testName)
$subSiteNameNode.SetAttribute("result", $xmlOld.TestRun.Results.UnitTestResult.outcome)
$subSiteNameNode.AppendChild($xmlDoc.CreateElement("output"))
$xmlDoc.Save($XML_NEW_PATH)
$ListsElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("traits"));
$ListElement = $ListsElement.AppendChild($xmlDoc.CreateElement("trait"));
$ListElement.SetAttribute("name", "Category")
$ListElement.SetAttribute("value", $xmlOld.TestRun.Results.UnitTestResult.testName)
$xmlDoc.Save($XML_NEW_PATH)
}
return $XML_NEW_PATH
}
几乎可以正常工作,它只是将所有 testname 和输出值放在每个 name 和 value 属性中,如下所示:
<collection Name="UAT Smoke Tests" total="12" passed="0" failed="12">
<test method="Should_Search_LeaseByBookingStatusCommenced_Successfully Should_Search_LegalEntity_Successfully Should_Search_SequesnceNumberLease_Successfully Should_Search_PayableInvoice_Successfully Should_Search_CustomerByStatus_Successfully Should_Search_LeaseByBookingStatusPending_Successfully Should_Search_VendorByName_Successfully Should_Search_CustomerByNumber_Successfully Should_Search_VendorByNumber_Successfully Should_Search_CustomerByName_Successfully Should_Search_InvoiceNumber_Successfully Should_Search_CustomerByAlias_Successfully" result="Failed Failed Failed Failed Failed Failed Failed Failed Failed Failed Failed Failed">
<output />
<traits>
<trait name="Category" value="Should_Search_LeaseByBookingStatusCommenced_Successfully Should_Search_LegalEntity_Successfully Should_Search_SequesnceNumberLease_Successfully Should_Search_PayableInvoice_Successfully Should_Search_CustomerByStatus_Successfully Should_Search_LeaseByBookingStatusPending_Successfully Should_Search_VendorByName_Successfully Should_Search_CustomerByNumber_Successfully Should_Search_VendorByNumber_Successfully Should_Search_CustomerByName_Successfully Should_Search_InvoiceNumber_Successfully Should_Search_CustomerByAlias_Successfully" />
</traits>
</test>
</collection>
如何在第一个测试标签方法和值属性中获取第一组值(当然,对于特征标签 Category 属性),第二个标签中的第二组值等等?
我在网上对此进行了研究,但找不到与此案例类似的任何东西,非常感谢您的帮助。