Groovy Question

SoapUI. Export and Import properties to file by script

I need to export and import properties from testcase(SoapUI) to file by java or groovy, anybody had met such task?

Manual option does not suit me.

Rao Rao
Answer

Not sure if there is any built-in API available for the same.

However, here are the scripts to import and export test case properties respectively.

Import a properties files to a test case. Groovy script follows:

/**
 * 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')

Export Test Case properties to a file. Groovy Script follows:

/**
 * 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')
Comments