如何使用 powershell 从 html 获取通过和失败计数

How to get the Pass and Fail count from html using powershell

有人可以帮助我如何使用 powershell 从 html 文件中获取通过和失败计数。

html file

<div class="navbar-header">
          <div class="project-name visible-md visible-lg">Protractor Test Execution Report</div>
          <div class="label-container">
            <span class="label label-success" title="TestCases">Passed: 1508</span>
            <span class="label label-danger" title="TestCases">Failed: 256</span> 
            
            <span class="label label-info">Time Elapsed: 10h 32min 26s</span>
          </div>
        </div>

或者,

我试图在下面的行中获取 FAILED 的值。 Executed 336 of 336 specs[31m (10 FAILED)[39m in 1 hour 3 secs.。 我使用了下面的代码 $output='C:\Users\viuppulu\Desktop\consoloe.txt' $version = ($output | Select-String -Pattern "(?:\d+\FAILED)").Matches.Value 但不工作.. 你能帮忙得到失败值(10)和执行值(336)

在上面的问题中,我能够得到通过和失败的计数。使用这些数据,我再创建一个 html 来制作可读格式。但是html上没有如何获取Pass count和Fail count is 0 if failed is 0 or failed的数据。

<div class="navbar navbar-default navbar-static-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <div class="project-name visible-md visible-lg">Protractor Test Execution Report</div>
          <div class="label-container">
            <span class="label label-success" title="TestCases">Passed: 329</span>
            
            
            <span class="label label-info">Time Elapsed: 1h 24min 37s</span>
          </div>
        </div>
      </div>
    </div>

关于您的第一个问题,使用正则表达式解析 html 中的内容可能很棘手,因为浏览器不关心这些行是否一行一行地整齐地书写。

总之,你可以试试

$html = Get-Content -Path 'D:\Test\test.html' -Raw

$passed = $failed = 0
$regex = '<span class="label label-success"[^>]+>Passed: (\d+)</span>\s*<span class="label label-danger"[^>]+>Failed: (\d+)</span>'
if ($html -match $regex) {
    $passed = [int]$matches[1]
    $failed = [int]$matches[2]
}

$passed   # --> 1508
$failed   # -->  256

对于你的第二个问题,你可以这样做:

Select-String -Path 'C:\Users\viuppulu\Desktop\consoloe.txt' -Pattern 'Executed\s+(\d+).*(?:\((\d+) FAILED\))' | 
Select-Object @{Name = 'Executed'; Expression = { $_.Matches.Groups[1].Value }},
              @{Name = 'Failed'; Expression = { $_.Matches.Groups[2].Value }}

输出类似于:

Executed Failed
-------- ------
336      10    
536      0     
136      7 

根据您编辑的问题,html 中可能不存在 Parsed 或 Failed 的跨度(我假设它们一直存在..)

您可以像这样获得两个值:

$html = Get-Content -Path 'D:\Test\test.html' -Raw

$passed = $failed = 0
# test Passed and Failed separate. When 0 or not available, the resulting value is 0
if ($html -match '<span class="label label-success"[^>]+>Passed: (\d+)</span>') { $passed = [int]$matches[1] }
if ($html -match '<span class="label label-danger"[^>]+>Failed: (\d+)</span>')  { $failed = [int]$matches[1] }

# $passed  --> 329
# $failed  -->   0