Groovy 脚本从文件读取并保存在 属性

GroovyScript Read from file and save in Property

我正在尝试从文件中读取内容,然后将其保存在 SoapUi 的 属性 中。

文件的外观(test.txt):

1231434324
1231414144
2413131231
4142131231
2131231231

我的代码:

File files = new File("/Temp/test.txt") // File
def lines = files.readLines(); // 

lines.each  { 
System.out.println it
 testRunner.testCase.testSteps["Properties"].setPropertyValue( "test", it )
};

出于某种原因,它只保存 属性 中的最后一个值 (1231434324)。

下面的示例实际上将所有值保存到 属性,但它还在 属性 值的开头和结尾插入了方括号。

[123123123123, 123124234353, 231231231241, 213123123123]

File files = new File("/Temp/test.txt")
def lines = files.readLines();
testRunner.testCase.testSteps["Properties"].setPropertyValue( "test",        "$lines" )

可以先将整个文件内容读入String,然后一次性全部设置:

String value = new File('/Temp/test.txt').text
testRunner.testCase.testSteps['Properties'].setPropertyValue('test', value)

更新

要获取文件的总行数,您可以这样做:

int count = value.split('\n').size()