2

出于某种原因,以下脚本在执行时不仅会在日志中打印输出,还会在信息弹出对话框中打印输出。有人可以向我解释为什么会发生这种情况以及如何防止它发生吗?

import groovy.io.FileType;
import org.custommonkey.xmlunit.*;

def file1 = "somepath/file1.xml"
def file2 = "somepath/file2.xml"

def xml1 = new FileReader(file1)
def xml2= new FileReader(file2)
XMLUnit.setIgnoreWhitespace(true)
XMLUnit.setIgnoreComments(true)
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true)
XMLUnit.setNormalizeWhitespace(true)

DetailedDiff myDiff = new DetailedDiff(new Diff(xml1, xml2));
List allDifferences = myDiff.getAllDifferences();

allDifferences.each { difference ->
    log.info (difference)
}

编辑:通过实验,我发现以下行:

List allDifferences = myDiff.getAllDifferences();

这就是弹出对话的原因。我猜测 getAllDiffenes() 方法会导致对话框弹出。

我仍然需要一些帮助来确定可行的替代方案,因为我正在尝试比较两个 xml 文件并打印文件中的差异。

4

1 回答 1

6

这不是 XMLUnit 类(等等)的问题DiffDetailedDIff这是groovy与 SOAPUI 中的 Groovy 脚本测试步骤结合使用的行为:在groovy语言中,如果您没有指定 areturn那么最后评估的表达式是默认返回值,因此,当 Groovy 脚本测试步骤单独执行时,SOAPUI 会捕获return并且如果它不null打印字符串对象表示(如果您将 groovy 测试步骤作为测试用例的一部分执行,则不会发生这种情况,因为 SOAPUI 脚本不会捕获并显示return)。即,如果您在 SOAPUI 上执行下一个 groovy 脚本:

def a = 3 + 1

groovy正在添加return,所以你真的有:

def a = 3 + 1
return a

SOAPUI 捕捉到这一点return并显示下一个对话框:

Groovy 脚本对话框

因此,您可以修复此行为null,最后将 a 分配给某个变量,或者return明确添加任何内容,如下所示:

import groovy.io.FileType;
import org.custommonkey.xmlunit.*;

...

DetailedDiff myDiff = new DetailedDiff(new Diff(xml1, xml2));
List allDifferences = myDiff.getAllDifferences();

allDifferences.each { difference ->
    log.info (difference)
}

// ADD THIS LINE TO AVOID DIALOG BOX
def dontCare = null; // groovy add return dontCare :)

或者:

import groovy.io.FileType;
import org.custommonkey.xmlunit.*;

...

DetailedDiff myDiff = new DetailedDiff(new Diff(xml1, xml2));
List allDifferences = myDiff.getAllDifferences();

allDifferences.each { difference ->
    log.info (difference)
}

// ADD THIS LINE TO AVOID DIALOG BOX
return;

希望这可以帮助,

于 2014-07-07T22:13:17.527 回答