Katalon写一个Groovy脚本,java8利用java-diff-util实现两个文件夹对比,并将比较结果以文件为单位出力成各自的HTML文件。要求:当文件在两个文件夹中都存在时,能识别隔行数据,将两个文件有差异的数据都出力到HTML中,解析两个文件的差异数据并将有差异的字段高亮显示出来;当文件只在一个文件夹中存在的文件,将这个信息出力到HTML中
时间: 2024-03-02 13:52:34 浏览: 14
好的,以下是Katalon Studio中的Groovy脚本示例,它可以基于java-diff-utils库实现两个文件夹对比并输出HTML格式的比较结果:
```groovy
import java.io.File
import java.util.List
import difflib.DiffUtils
import difflib.Patch
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.util.KeywordUtil
// 配置需要对比的源文件夹和目标文件夹
def sourceDir = new File(RunConfiguration.getProjectDir(), "SourceFolder")
def targetDir = new File(RunConfiguration.getProjectDir(), "TargetFolder")
// 遍历源文件夹中的所有文件
sourceDir.eachFileRecurse { sourceFile ->
if (sourceFile.isFile()) {
// 根据源文件路径生成对应的目标文件路径
def targetFile = new File(targetDir, sourceFile.relativePath(sourceDir))
if (targetFile.exists() && targetFile.isFile()) {
// 当文件在两个文件夹中都存在时,执行文件内容比较
compareFiles(sourceFile, targetFile)
} else {
// 当文件只在一个文件夹中存在时,输出文件缺失信息
KeywordUtil.logInfo("File not found in target folder: ${sourceFile.relativePath(sourceDir)}")
}
}
}
/**
* 用java-diff-utils库对两个文件进行内容比较,并输出HTML格式的比较结果
* @param sourceFile 源文件
* @param targetFile 目标文件
*/
void compareFiles(File sourceFile, File targetFile) {
// 读取源文件和目标文件的内容
def sourceLines = sourceFile.readLines()
def targetLines = targetFile.readLines()
// 使用java-diff-utils库对文件内容进行比较
def patch = DiffUtils.diff(sourceLines, targetLines)
// 生成HTML格式的比较结果
def htmlOutput = generateHtmlOutput(patch, sourceFile.relativePath(sourceDir), targetFile.relativePath(targetDir))
// 将比较结果输出到各自的HTML文件中
def sourceHtmlFile = new File(sourceFile.parentFile, "${sourceFile.nameWithoutExtension}_diff.html")
sourceHtmlFile.write(htmlOutput.getSourceHtml())
def targetHtmlFile = new File(targetFile.parentFile, "${targetFile.nameWithoutExtension}_diff.html")
targetHtmlFile.write(htmlOutput.getTargetHtml())
KeywordUtil.logInfo("Generated diff files: ${sourceHtmlFile.absolutePath} ${targetHtmlFile.absolutePath}")
}
/**
* 生成HTML格式的比较结果
* @param patch 两个文件内容的差异对象
* @param sourceFileName 源文件名
* @param targetFileName 目标文件名
* @return HTML格式的比较结果
*/
HtmlOutput generateHtmlOutput(Patch patch, String sourceFileName, String targetFileName) {
def sourceHtml = new StringBuilder()
def targetHtml = new StringBuilder()
sourceHtml.append("<html><head><title>${sourceFileName} diff ${targetFileName}</title></head><body><h1>${sourceFileName} diff ${targetFileName}</h1>")
targetHtml.append("<html><head><title>${sourceFileName} diff ${targetFileName}</title></head><body><h1>${sourceFileName} diff ${targetFileName}</h1>")
// 遍历差异块
patch.getDeltas().each { delta ->
// 获取差异块起始行号和结束行号
def sourceStart = delta.getSource().getPosition()
def sourceEnd = sourceStart + delta.getSource().size()
def targetStart = delta.getTarget().getPosition()
def targetEnd = targetStart + delta.getTarget().size()
// 输出差异块的位置
sourceHtml.append("<p>Diff from line ${sourceStart+1} to ${sourceEnd}:</p>")
targetHtml.append("<p>Diff from line ${targetStart+1} to ${targetEnd}:</p>")
// 遍历差异块中的行
delta.getOriginal().eachWithIndex { sourceLine, i ->
def op = delta.getType(i)
def targetLine = delta.getRevised().get(i)
// 根据操作类型输出HTML格式的差异行
if (op == Patch.Operation.DELETE) {
sourceHtml.append("<p><del>${sourceLine.escapeHtml()}</del></p>")
targetHtml.append("<p>${targetLine.escapeHtml()}</p>")
} else if (op == Patch.Operation.INSERT) {
sourceHtml.append("<p>${sourceLine.escapeHtml()}</p>")
targetHtml.append("<p><ins>${targetLine.escapeHtml()}</ins></p>")
} else {
sourceHtml.append("<p>${sourceLine.escapeHtml()}</p>")
targetHtml.append("<p>${targetLine.escapeHtml()}</p>")
}
}
}
sourceHtml.append("</body></html>")
targetHtml.append("</body></html>")
return new HtmlOutput(sourceHtml.toString(), targetHtml.toString())
}
/**
* HTML格式的比较结果
*/
class HtmlOutput {
private String sourceHtml
private String targetHtml
HtmlOutput(String sourceHtml, String targetHtml) {
this.sourceHtml = sourceHtml
this.targetHtml = targetHtml
}
String getSourceHtml() {
return sourceHtml
}
String getTargetHtml() {
return targetHtml
}
}
/**
* 将字符串中的特殊字符转义为HTML实体
*/
String escapeHtml(String str) {
return str.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'")
}
```
该脚本通过遍历源文件夹中的所有文件,对于每个文件,如果它在目标文件夹中也存在,则调用`compareFiles`方法对两个文件内容进行比较,并将比较结果以HTML格式输出到各自的HTML文件中;如果它在目标文件夹中不存在,则输出该文件缺失的信息。`compareFiles`方法中,首先读取源文件和目标文件的内容,然后使用java-diff-utils库对文件内容进行比较,最后调用`generateHtmlOutput`方法生成HTML格式的比较结果,并将其输出到各自的HTML文件中。`generateHtmlOutput`方法中,遍历差异块和差异行,根据操作类型输出HTML格式的差异行,并将两个文件的差异行分别保存到`sourceHtml`和`targetHtml`中。`HtmlOutput`类是一个简单的POJO类,用于封装HTML格式的比较结果。`escapeHtml`方法用于将字符串中的特殊字符转义为HTML实体,避免在HTML中产生语法错误。