了SoapUI。按脚本将属性导出和导入到文件

时间:2016-08-30 13:54:39

标签: java groovy soapui

我需要通过java或groovy将testcase(SoapUI)中的属性导出并导入到文件中,是否有人遇到过这样的任务?

手动选项不适合我。

1 个答案:

答案 0 :(得分:0)

不确定是否有可用的内置API。

但是,以下是分别导入和导出测试用例属性的脚本。

将属性文件导入到测试用例。 Groovy脚本如下:

/**
 * this method imports properties to a test case from a file.
 * @param context
 * @param filePath
 */
def importPropertiesToTestCase(def context,String filePath) {
    def  props = new Properties()
    def propFile = new File(filePath)
    //load the properties files into properties object
    props.load(propFile.newDataInputStream())
    //loop thru the properties and set them at test case level
    props.each {
        context.testCase.setPropertyValue(it.key, it.value.toString())
    }
}
//How to use above method. Make sure you have file with properties, change path if needed.
importPropertiesToTestCase(context, 'D:/Temp/testCase.properties')

导出测试用例属性到文件。 Groovy脚本如下:

/**
 * this method exports test case properties into a file.
 * @param context
 * @param filePath
 */
def exportTestCaseProperties(def context, String filePath) {
    def  props = new Properties()
    //Get all the property names of test cases
    def names = context.testCase.getPropertyNames()
    //loop thru names and set Properties object
    if (names) {
        names.each { name ->
            log.info "Set property ${name}"
            props.setProperty(name, context.testCase.getPropertyValue(name))
        }
        //Write properties object to a file
        def propFile = new File(filePath)
        props.store(propFile.newWriter(), null)
        log.info "Check the properties file: ${filePath}"
    } else {
        log.info "There does not seem to have any test case properties to write, check it."
    }
}
//How to use above method
exportTestCaseProperties(context, 'D:/Temp/testCase1.properties')