SOAPUI 使用 groovy 从文件加载自定义属性
SOAPUI Load Custom Properties from file using groovy
我正在尝试编写一个 groovy 脚本,该脚本使用属性文件中的信息加载测试套件的自定义属性。
属性文件有大约 6 个不同的属性
我看过很多不同的方法,即从属性测试步骤加载并尝试使用 groovy 扩展属性,但没有成功。
如果有人可以就如何实现这一点提出建议,我们将不胜感激。
提前致谢。
这里是 groovy 脚本,它读取 属性 文件并将它们设置在 test suite level
:
def props = new Properties()
//replace the path with your file name below. use / instead of \ as path separator even on windows platform.
new File("/absolute/path/of/test.properties").withInputStream { s ->
props.load(s)
}
props.each {
context.testCase.testSuite.setPropertyValue(it.key, it.value)
}
当前套件的上述脚本加载测试套件级别,其中存在 groovy 脚本。
不幸的是,在我的例子中,我希望属性的顺序与输入文件的顺序相同,即。已排序,此方法不起作用。
我想加载一个包含排序属性的 'Project properties' 文件,每次我使用这种方法时,它都会以未排序的方式存储它们。
我不得不使用更直接的方法(见下文)。如果有人知道更多 elegant/practical 的方法,我很感兴趣
def filename = context.expand( '${#TestCase#filename}' )
def propertiesFile = new File(filename)
assert propertiesFile.exists(), "$filename does not exist"
project = testRunner.testCase.testSuite.project
//Remove properties
project.propertyNames.collect{project.removeProperty(it)}
//load the properties of external file
propertiesFile.eachLine {
line->
firstIndexOf = line.indexOf('=') // properties as set as key=value in the file
key = line.substring(0, firstIndexOf)
value = line.substring(firstIndexOf+1)
project.setPropertyValue(key, value)
}
我正在尝试编写一个 groovy 脚本,该脚本使用属性文件中的信息加载测试套件的自定义属性。 属性文件有大约 6 个不同的属性 我看过很多不同的方法,即从属性测试步骤加载并尝试使用 groovy 扩展属性,但没有成功。
如果有人可以就如何实现这一点提出建议,我们将不胜感激。
提前致谢。
这里是 groovy 脚本,它读取 属性 文件并将它们设置在 test suite level
:
def props = new Properties()
//replace the path with your file name below. use / instead of \ as path separator even on windows platform.
new File("/absolute/path/of/test.properties").withInputStream { s ->
props.load(s)
}
props.each {
context.testCase.testSuite.setPropertyValue(it.key, it.value)
}
当前套件的上述脚本加载测试套件级别,其中存在 groovy 脚本。
不幸的是,在我的例子中,我希望属性的顺序与输入文件的顺序相同,即。已排序,此方法不起作用。 我想加载一个包含排序属性的 'Project properties' 文件,每次我使用这种方法时,它都会以未排序的方式存储它们。 我不得不使用更直接的方法(见下文)。如果有人知道更多 elegant/practical 的方法,我很感兴趣
def filename = context.expand( '${#TestCase#filename}' )
def propertiesFile = new File(filename)
assert propertiesFile.exists(), "$filename does not exist"
project = testRunner.testCase.testSuite.project
//Remove properties
project.propertyNames.collect{project.removeProperty(it)}
//load the properties of external file
propertiesFile.eachLine {
line->
firstIndexOf = line.indexOf('=') // properties as set as key=value in the file
key = line.substring(0, firstIndexOf)
value = line.substring(firstIndexOf+1)
project.setPropertyValue(key, value)
}