Forum Discussion

richie's avatar
richie
Community Hero
6 months ago

Groovy To Extract Comma Separated List Into Separate Properties

Hi,

So this is on the end of one of my previous posts - nmrao spoonfed me until I got there (so all cred to him) - this is the next step.

I have a string of comma separated values stored as a custom testSuite level property.  I want to extract the individual values from the single property into multiple separate properties for later use.

I've managed to do it - but it's shockingly bad groovy - so I'm wondering if anyone can guide me into a way  of doing it more efficiently

The testSuite level 'MY_SUBLIST' property has the value of:  3K684,3K686,3K688,3U3750

As stated above, I want to separate out each comma separated value into it's own testSuite level property.  As stated above - I've managed it - but it's poor groovy.  Please see as follows:

//get the comma separated list in my testSuite level property entitled MY_SUBLIST and save it to the mySubListToString variable
def mySubListToString = context.testCase.testSuite.getPropertyValue('MY_SUBLIST')
log.info mySubListToString 
// log response was --> Thu May 23 00:20:30 BST 2024: INFO: 3K684,3K686,3K688,3U3750


//use split() to extract the first route_code value and save this value to the splitString1 property
def splitString1 = mySubListToString.split(',')[0].toString()
log.info(splitString1)
//log response was --> Thu May 23 00:20:30 BST 2024: INFO: 3K684

//save splitString1 value to a new testSuite level property entitled splitString1
testRunner.testCase.testSuite.setPropertyValue( "splitString1", splitString1)

//use split() to extract the second route_code value and save this value to the splitString2 property
def splitString2 = mySubListToString.split(',')[1].toString()
log.info(splitString2)
//log response was --> Thu May 23 00:20:30 BST 2024: INFO: 3K686

//save splitString2 value to a new testSuite level property entitled splitString2
testRunner.testCase.testSuite.setPropertyValue( "splitString2", splitString2)

//use split() to extract the third route_code value and save this value to the splitString3 property
def splitString3 = mySubListToString.split(',')[2].toString()
log.info(splitString3)
//log response was --> Thu May 23 00:20:30 BST 2024: INFO: 3K688

//save splitString3 value to a new testSuite level property entitled splitString3
testRunner.testCase.testSuite.setPropertyValue( "splitString3", splitString3)

//use split() to extract the fourth route_code value and save this value to the splitString4 property
def splitString4 = mySubListToString.split(',')[3].toString()
log.info(splitString4)
//log response was --> Thu May 23 00:20:30 BST 2024: INFO: 3U3750

//save splitString4 value to a new testSuite level property entitled splitString4
testRunner.testCase.testSuite.setPropertyValue( "splitString4", splitString4)

 

As you can see - I'm not going to win any awards with the code above.

Can anyone advise on a more efficient/effective way of doing it?  It's rubbish to have to explicitly define a variable and a new property for each comma separated value - so I'd welcome if anyone has a more efficient way I can do this.  As you can see - what I've done gets the job done - but it's a shonky way of doing it!

Cheers!

rich

  • nmrao's avatar
    nmrao
    Champion Level 3

    The actual question should be  "what do you want to do after storing them in separate properties"? I just wanted to understand the actual usage of the data which was saved for later use.

    In earlier question, the solution was there how to extract the same. If you say what it should be used for, then everything can done in the same script without putting into separate properties test step.

    And additional issue with the use of properties is that, at least in this case, there are no keys, only values are there and it is unnatural here to use properties. And in this context of your use case, the number of collected IDs may differ in size and no proper key name to access the right data.

    Properties is preferred when we know the each of its key to access the right value.

    • richie's avatar
      richie
      Community Hero

      Hey nmrao,  

      Apologies for not responding sooner.  I've been dealing with a family emergency.  I'll be responding tomorrow.

      Cheers,

      Rich