katalon写一个Groovy脚本,利用java-diff-util库来实现两个文件夹的自动比较,并利用ReportGenerator类将比较结果生成HTML。当文件在两个文件夹中都存在,但内容不一致,则将差异数据的行数及内容出力到html中,将具体哪个字段有差异高亮显示出来;当文件只在一个文件夹中存在,将信息输出到html中。要求能识别隔行数据,按文件名生成结果html,html里显示两个文件夹的差异内容
时间: 2024-02-28 16:57:03 浏览: 12
好的,以下是 Groovy 脚本代码:
```groovy
@Grab(group='com.googlecode.java-diff-utils', module='diffutils', version='1.3.0')
import difflib.*
import java.nio.file.*
// 定义两个文件夹
def folder1 = Paths.get("path/to/folder/1")
def folder2 = Paths.get("path/to/folder/2")
// 获取两个文件夹中的所有文件的路径
def files1 = Files.walk(folder1).filter{!Files.isDirectory(it)}.collect{it.toAbsolutePath().toString()}
def files2 = Files.walk(folder2).filter{!Files.isDirectory(it)}.collect{it.toAbsolutePath().toString()}
// 比较两个文件夹中的所有文件
def html = ""
files1.each{ file1 ->
def relativePath = folder1.relativize(Paths.get(file1))
def file2 = folder2.resolve(relativePath)
if(Files.exists(file2)){
def diff = new DiffRowGenerator.Builder().showInlineDiffs(true).build()
def text1 = Files.readAllLines(file1).join("\n")
def text2 = Files.readAllLines(file2).join("\n")
def rows = diff.generateDiffRows(text1.tokenize() as List<String>, text2.tokenize() as List<String>)
def diffRows = rows.findAll{it.tag != Tag.EQUAL}.collect{"<tr><td>${it.lineNumber}</td><td>${it.tag}</td><td>${it.oldLine}</td><td>${it.newLine}</td></tr>"}.join("\n")
if(diffRows){
html += "<h3>${relativePath}</h3><table><thead><tr><th>Line Number</th><th>Tag</th><th>${folder1.getFileName()}</th><th>${folder2.getFileName()}</th></tr></thead><tbody>${diffRows}</tbody></table>"
}
} else {
html += "<h3>${relativePath}</h3><p>Only in ${folder1.getFileName()}: ${relativePath}</p>"
}
}
files2.each{ file2 ->
def relativePath = folder2.relativize(Paths.get(file2))
def file1 = folder1.resolve(relativePath)
if(!Files.exists(file1)){
html += "<h3>${relativePath}</h3><p>Only in ${folder2.getFileName()}: ${relativePath}</p>"
}
}
// 生成 HTML 报告
class ReportGenerator {
static String generateReport(String html){
return "<html><head><title>Folder Comparison Report</title></head><body>${html}</body></html>"
}
}
def report = ReportGenerator.generateReport(html)
// 将 HTML 报告写入文件
def reportFile = new File("comparisonReport.html")
reportFile.write(report)
```
这个脚本会比较两个文件夹中的所有文件,并将比较结果以 HTML 报告的形式输出。如果两个文件夹中有相同的文件但内容不同,会将差异数据的行数及内容高亮显示出来;如果一个文件只在一个文件夹中存在,会将信息输出到 HTML 报告中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)