katalon写一个Groovy脚本,利用java-diff-util库来实现两个文件夹的自动比较,并利用ReportGenerator将比较结果生成HTML。当文件在两个文件夹中都存在,但内容不一致,则将差异数据的行数及内容出力到html中,将具体哪个字段有差异高亮显示出来;当文件只在一个文件夹中存在,将信息输出到html中。要求能识别隔行数据,按文件名生成结果html,html里显示两个文件夹的差异内容
时间: 2024-02-28 09:56:50 浏览: 58
好的,这是一个比较复杂的需求,需要使用到多个工具和库。首先,需要在Katalon Studio中创建一个Groovy脚本,并导入以下依赖库:
```groovy
@Grab(group='com.github.difflib', module='difflib', version='1.3.0')
@Grab(group='com.relevantcodes', module='extentreports', version='2.41.2')
```
其中,`difflib`是Java-Diff-Utils库,用于比较两个文件的差异;`extentreports`是一个报告生成工具,用于生成HTML报告。
接下来,可以按以下步骤编写Groovy脚本:
1. 定义两个文件夹的路径,例如:
```groovy
def folder1Path = "path/to/folder1"
def folder2Path = "path/to/folder2"
```
2. 定义生成HTML报告的路径,例如:
```groovy
def reportPath = "path/to/report.html"
```
3. 遍历两个文件夹,比较文件差异,并将结果保存到一个列表中,例如:
```groovy
import difflib.DiffUtils
import difflib.Patch
def diffList = []
def folder1 = new File(folder1Path)
def folder2 = new File(folder2Path)
folder2.eachFileRecurse { file2 ->
def file1 = new File(folder1Path + "/" + file2.name)
if (file1.exists()) {
def lines1 = file1.readLines()
def lines2 = file2.readLines()
def patch = DiffUtils.diff(lines1, lines2)
if (patch.getDeltas().size() > 0) {
def diff = [:]
diff.put("filename", file2.name)
diff.put("lines", [])
patch.getDeltas().each { delta ->
def range1 = delta.getOriginal()
def range2 = delta.getRevised()
def lines = [:]
lines.put("range1", "${range1.getAnchor()}-${range1.getCount()}")
lines.put("range2", "${range2.getAnchor()}-${range2.getCount()}")
lines.put("content1", range1.getLines().join("\n"))
lines.put("content2", range2.getLines().join("\n"))
diff.lines.add(lines)
}
diffList.add(diff)
}
} else {
def diff = [:]
diff.put("filename", file2.name)
diff.put("lines", [])
file2.eachLine { line ->
def lines = [:]
lines.put("range1", "")
lines.put("range2", file2.getLineNumber())
lines.put("content1", "")
lines.put("content2", line)
diff.lines.add(lines)
}
diffList.add(diff)
}
}
def folder1Files = folder1.listFiles()
folder1Files.each { file1 ->
def file2 = new File(folder2Path + "/" + file1.name)
if (!file2.exists()) {
def diff = [:]
diff.put("filename", file1.name)
diff.put("lines", [])
file1.eachLine { line ->
def lines = [:]
lines.put("range1", file1.getLineNumber())
lines.put("range2", "")
lines.put("content1", line)
lines.put("content2", "")
diff.lines.add(lines)
}
diffList.add(diff)
}
}
```
4. 使用ReportGenerator生成HTML报告,例如:
```groovy
import com.relevantcodes.extentreports.ExtentReports
import com.relevantcodes.extentreports.LogStatus
def report = new ExtentReports(reportPath)
diffList.each { diff ->
def testName = diff.filename
def test = report.startTest(testName)
diff.lines.each { line ->
def content1 = line.content1.replaceAll("\r\n", "\n")
def content2 = line.content2.replaceAll("\r\n", "\n")
def patch = DiffUtils.diff(content1.tokenize('\n'), content2.tokenize('\n'))
if (patch.getDeltas().size() > 0) {
def html = ""
patch.getDeltas().each { delta ->
def range1 = delta.getOriginal()
def range2 = delta.getRevised()
html += range1.getLines().join("\n") + "<br>"
html += "<span style='background-color: #FFFF00;'>${range2.getLines().join("\n")}</span><br>"
}
test.log(LogStatus.WARNING, "Lines ${line.range1} and ${line.range2} differ:", html)
}
}
report.endTest(test)
}
report.flush()
report.close()
```
这样,就可以将比较结果生成为HTML报告,其中差异部分会高亮显示。同时,报告中会按文件名分别显示两个文件夹中文件的差异内容。
阅读全文