如何将屏幕截图和 HTML 快照嵌入到 Spock 报告中?

问题描述 投票:0回答:1

有没有一种好方法可以将 Geb 屏幕截图和 HTML 快照工件嵌入到创建的 spock-report 中?除了 geb-spock-reports 之外,似乎没有一个好的解决方案,现已弃用。

selenium-webdriver groovy geb spock-reports
1个回答
1
投票

对于我的解决方案,我继续制作了如下所示的代码片段,以在

cleanup
步骤中并使用
reportInfo()
来完成此操作。这对我来说效果很好,因为我有一个
SpecBase
类,它可以在所有规范中继承和使用。

您可以粘贴方法和代码并添加您自己的路径,它应该可以工作。我知道这不是最优雅的解决方案,可以改进或可能作为配置选项添加到该项目中,但现在这是为那些希望嵌入报告和故障的人提供的解决方案。

它基本上找到geb工件并将它们移动到

spock-reports
目录中以使用创建的html字符串,该字符串通过
reportInfo()
插入用于嵌入。

我愿意接受任何将其作为 PR 或改进它的建议。我还没有设置一个测试项目来展示这一点,所以不幸的是我还没有提供示例图像!


    void cleanup() {
        def gebArtifactsPath = "build/reports/geb/path/to/specs/here/"
        File[] files = new File("$gebArtifactsPath$specificationContext.currentSpec.name").listFiles()

        insertReportArtifacts(files)
        insertFailureArtifacts(files)
    }

    /**
     * Finds all report artifacts taken. Moves them into the spock-report directory.
     * Then creates the html to insert into the reportInfo() which embeds them into the report
     *
     * @param files - geb artifacts from specs
     */
    def insertReportArtifacts(File[] files) {
        List<File> screenshots = files.findAll { !it.name.contains("failure.png") && it.name.contains(".png") }
        List<File> htmlSnapshots = files.findAll { !it.name.contains("failure.html") && it.name.contains(".html") }

        boolean reportFound = screenshots != null || htmlSnapshots != null
        if (reportFound) {
            def reportDir = new File('build/spock-reports')
            if (!reportDir.exists()) reportDir.mkdirs()

            htmlSnapshots.each { it.renameTo("$reportDir/$it.name") }
            screenshots.each { it.renameTo("$reportDir/$it.name") }

            def reportArtifactHtml = ""
            def screenshotIndex = 0

            htmlSnapshots.each { html ->
                def screenshot = screenshots[screenshotIndex]

                reportArtifactHtml += """                
                <table style="border:1px solid black;">
                    <thead>
                        <tr>
                            <th>[Html Report] ${html.name.replace('.html', '')}</th>
                        </tr>
                    </thead>
                    <tr>
                        <td><a href="${html.name}" target="_blank">html report</a></td>
                    </tr>
                    <thead>
                        <tr>
                            <th>[Image Report] ${screenshot.name.replace('.png', '')}</th>
                        </tr>
                        <tr>
                            <td><a href="${screenshot.name}" target="_blank"><img src="${screenshot.name}" width="400" height="400" align="center"></a></td>
                        </tr>
                    </thead>
                </table>    
                """
                screenshotIndex++
            }

            reportInfo(reportArtifactHtml)
        }
    }

    /**
     * Finds failure artifacts taken. Moves them into the spock-report directory.
     * Then creates the html to insert into the reportInfo() which embeds them into the report
     *
     * @param files - geb artifacts from specs
     */
    def insertFailureArtifacts(File[] files) {
        File screenshot = files.find { it.name.contains("failure.png") }
        File htmlSnapshot = files.find { it.name.contains("failure.html") }
        boolean failureFound = screenshot != null || htmlSnapshot != null

        if (failureFound) {
            def reportDir = new File('build/spock-reports')
            if (!reportDir.exists()) reportDir.mkdirs()

            htmlSnapshot.renameTo("$reportDir/$htmlSnapshot.name")
            screenshot.renameTo("$reportDir/$screenshot.name")

            def failureArtifactHtml = """                 
            <table style="border:1px solid black;">    
                <thead>
                    <tr>
                        <th>[Html Fail]</th>
                    </tr>
                </thead>                
                    <tr>
                        <td><a href="$htmlSnapshot.name" target="_blank">html failure</a></td>
                    </tr>
                <thead>
                    <tr>
                        <th>[Image Fail]</th>
                    </tr>
                    <tr>
                        <td><a href="$screenshot.name" target="_blank"><img src="$screenshot.name" width="400" height="400" align="center"></a></td>
                    </tr>
                </thead>                
            </table>"""

            reportInfo(failureArtifactHtml)
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.