groovy 如何将Geb屏幕截图和HTML快照工件嵌入到renatoathaydes/spock-reports中?

62o28rlo  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(80)

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

dly7yett

dly7yett1#

对于我的解决方案,我继续做了下面的代码片段,在cleanup步骤中使用reportInfo()来完成它。这对我来说很好,因为我有一个SpecBase类,它被继承并在所有规范中使用。
你可以粘贴方法和代码,并添加到你自己的路径,它应该工作。我知道这不是最优雅的解决方案,可以改进或可能作为一个配置选项添加到这个项目中,但现在这里是一个解决方案,为那些希望报告和失败被嵌入。
它基本上是找到geb工件并将它们移动到spock-reports目录中,以使用创建的html字符串,该字符串与reportInfo()一起插入以进行嵌入。
我愿意接受任何建议,以获得这作为公关或改善它。我还没有建立一个测试项目来展示这一点,所以不幸的是,我没有一个例子图像给给予只是还没有!

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

字符串

相关问题