【问题标题】:how to apply if and OR if or condition in td tag in html file如何在 html 文件的 td 标记中应用 if 和 OR if 或条件
【发布时间】:2018-05-11 15:37:24
【问题描述】:

我将 athaydes 报告用于我的 geb spock html 报告。我正在尝试修改 html 报告以获取测试用例的状态。为此,我添加了新列作为“最后一列” 以下是我正在使用的html:

<table class="summary-table">
    <thead>
    <tr>
        <th>Name</th>
        <th>Features</th>
        <th>Failed</th>
        <th>Errors</th>
        <th>Skipped</th>
        <th>Time</th>
        <th>Final Status</th>
    </tr>
    </thead>
    <tbody>
    <% data.each { name, map ->
    def s = map.stats%>
    <tr class="${s.errors ? 'error' : ''} ${s.failures ? 'failure' : ''} ">
        <td><a href="${name}.html">$name</a></td>
        <td>${s.totalRuns}</td>
        <td>${s.failures}</td>
        <td>${s.errors}</td>
        <td>${s.skipped}</td>
        <td>${fmt.toTimeDuration(s.time)}</td>
        <td if="${s.totalRuns} != 0" ? 'PASS' : 'FAILED >${s.totalRuns = 0 ? 'PASS' : 'FAILED' }</td>
    </tr>
    <% } %>
    </tbody>
</table>

现在我的要求是,如果“${s.failures}”和“${s.errors}”和“${s.skipped}”等于零,那么只有列的值应该是“PASS”,否则它应该是“FAILED” .

我尝试了&lt;td if="${s.totalRuns} != 0" ? 'PASS' : 'FAILED &gt;${s.totalRuns = 0 ? 'PASS' : 'FAILED' }&lt;/td&gt; 之类的方法,但是这个解决方案不起作用,因为我对 html 很陌生。

你能在这方面帮助我吗?谢谢!

【问题讨论】:

    标签: html spock-reports


    【解决方案1】:

    您可以使用以下代码 sn-p,如果发生故障、错误或跳过测试,则显示 FAILED,否则显示 PASS:

    <td>${ s.failures || s.errors || s.skipped ? 'FAILED' : 'PASS' }</td>
    

    由于s.failures等都是整数,所以我们不需要显式检查它们是否大于0。

    如果您还想在s.totalRuns 为零时隐藏该值,您可以添加另一个条件。一般经验法则:&lt;% ... %&gt; 之间的所有内容都可以是任何 Groovy 代码。可能有比这个更清洁的解决方案,但它可以解决问题:

    <td>
        <% if (s.totalRuns) { %>
            ${ s.failures || s.errors || s.skipped ? 'FAILED' : 'PASS' }
        <% } %>
    </td>
    

    【讨论】:

    • 您好 TIlman,非常感谢。您的第一个解决方案对我来说绝对没问题。谢谢!
    猜你喜欢
    • 2016-03-08
    • 1970-01-01
    • 2021-07-04
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2014-08-13
    • 2022-06-10
    相关资源
    最近更新 更多