0

I'm currently trying to 'clean up' custom properties from a SOAP UI test case, using groovy script. From this other post, I tried to do so but I'm facing a problem : I can't access the removeProperty method.

I get my data :

data = context.testCase.testSuite.getTestCaseByName("Test multi TT");

and from it I can only use a removePropertyChangeListener method.

I tried to use data.getPropertyAt() function to get a suitable object but it does not return the correct data class.

How can I get a PropertyChangeListener parameter from my custom property that I could use to remove it programmatically ? All the posts I've been through provide answers with removeProperty and I can't find any that mentions removePropertyChangeListener

any help appreciated

EDIT: Based on the discussion over chat with OP, OP wants to remove the existing properties and add properties from external file to test case level custom properties.

Rao
  • 20,781
  • 11
  • 57
  • 77
A.Joly
  • 2,317
  • 2
  • 20
  • 25
  • Are you trying to remove a suite level property? – Rao Oct 12 '17 at 13:38
  • no, at test case level. I want to remove custom properties. Do you know if it is possible, programmatically, to reload properties from an external file ? this could be a convenient workaround ... – A.Joly Oct 12 '17 at 13:43
  • Yes it is possible. What is your use case? Are you running the tests from command line using `testrunner` and you want override them or just enough to read from external file and load them at test case level using groovy script step? – Rao Oct 12 '17 at 13:48
  • actually both case would fit, so far I'm building my tests and I'd like to do it within a groovy step, but I'll run them with testrunner in near future – A.Joly Oct 12 '17 at 13:52
  • but if I read from external file, in my groovy step, how will it remove the properties not in file (like through the GUI) ? – A.Joly Oct 12 '17 at 13:57
  • Depends on how you want it. Do you just want to override the values, and remove other properties? or just leave the other properties (which are not there in the file, but available at test case level) as there are? – Rao Oct 12 '17 at 14:00
  • I want to remove the properties that are not in the file. At the beginning of the test I would write the properties in an external file then, during the test create 'temporary' custom properties for my test purposes and, a the end of the test (in tear down script) reload the external file in order to remove the temporary custom properties, to avoid having them on the next run. – A.Joly Oct 12 '17 at 14:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156568/discussion-between-rao-and-a-joly). – Rao Oct 12 '17 at 14:12

2 Answers2

1

Below is the Setup Script for the soapui test case. And this does the following (after discussion with OP on the chat):

  1. Remove existing properties
  2. Add the properties from file to test case level custom properties.

Setup Script:

//Change external properties file path as needed
def filename = 'C:/Users/apps/Documents/test.properties'
def properties = new Properties()
def propertiesFile = new File(flename)
assert propertiesFile.exists(), "$filename does not exists"
propertiesFile.withInputStream { properties.load(it) }
//Remove properties
testCase.propertyNames.collect { testCase.removeProperty(it) }

//load the properties of external file
properties.collect { k, v -> testCase.setPropertyValue(k, v) }
Rao
  • 20,781
  • 11
  • 57
  • 77
0

Here is a solution with no external file. The aim is to remove only the new properties I created for testCase purpose, in the teardown script:

import java.util.regex.Pattern

data = context.testCase.testSuite.getTestCaseByName("myTestCase");
log.info " ********************** old props ***********************"
String[] customProps = new String[data.getPropertyCount()];
customProps = data.getPropertyNames();

Pattern myRegex = ~/maProp_/  // I name my new properties with the same pattern and an index

for (propertyName in customProps){
    log.info "info = " + propertyName
    myMatcher = propertyName =~ /$myRegex/
    if (myMatcher.count != 0){
        match = myMatcher[0] == 'maProp_'
        //log.info "match ? " + match // to check only my maProp_xx properties are matching

        context.getTestCase().removeProperty(propertyName)
    }
}


// verification
newProps = data.getPropertyNames();

log.info " ********************** new props ***********************"
for (i in newProps){
     log.info "info = " +i
}
A.Joly
  • 2,317
  • 2
  • 20
  • 25