Search code examples
htmlspock-reports

how to apply if and OR if or condition in td tag in html file


I am using athaydes reports for my geb spock html report. I am trying to modify the html report to get the status of the test case. For that I have added new column as 'Final Column' Below is html I am using:

<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>

Now my requirement is that if "${s.failures}" and "${s.errors}" and "${s.skipped}" are equal to zero, then only the value for column should come as "PASS" else it should be "FAILED".

I tried something like <td if="${s.totalRuns} != 0" ? 'PASS' : 'FAILED >${s.totalRuns = 0 ? 'PASS' : 'FAILED' }</td> however this solution is not working as I am very new to html.

Can you please help me on this front. Thanks!


Solution

  • You can use the following code snippet, to show FAILED if failures, errors or skipped tests occured, and PASS if not:

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

    As s.failures and the others are integers, we don't need to explicitly check if they are more than 0.

    If you do also want to hide the value if s.totalRuns is zero, you could add another condition. General rule of thumb: Everything between <% ... %> can be any Groovy code. There might be a cleaner solutions than this one, but it does the trick:

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