Groovy - Save off hashmapped split() values to a Property
Hey,
I'm back working with ReadyAPI! and I've forgotten loads in the last 2 years....I'm going through my groovy notes and I'm struggling.
I have a full URL with 2 query parms - token and value and I'm using the split method to write out the values to the logging window. - the script is below:
//code originally provided by nmrao as an answer to another question I posted
def str = 'https://whatevs.azurewebsites.net/path?token=eyJ0eXAiOiJKV1Qi&value=123456789'
def map = new URL(str).query?.split('&').collectEntries{ [(it.split('=').first()): it.split('=').last()]}
log.info map.token
log.info map.value
log.info map
so the logging window writes
Sat Dec 16 21:46:36 GMT 2023: INFO: eyJ0eXAiOiJKV1Qi
Sat Dec 16 21:46:36 GMT 2023: INFO: 123456789
Sat Dec 16 21:46:36 GMT 2023: INFO: {token=eyJ0eXAiOiJKV1Qi, value=123456789}
However - I want to use the query parm values - I don't want to just log them
'token' and 'value' aren't actually properties/variables - so I can't write the associated property values out to a Properties step - cos those token and value values are just part of the value that makes up the whole string of '{token=eyJ0eXAiOiJKV1Qi, value=123456789}'
I was thinking about passing the whole 'map' variable to the Properties step and try using some substring method like split() or something else - but I have a couple of problems:
firstly - the map variable is a hashmap right? It's not like a normal property that I can pass to a properties step and then use groovyscript to manipulate - when I tried the following
def str = 'https://whatevs.azurewebsites.net/path?token=eyJ0eXAiOiJKV1Qi&value=123456789'
def map = new URL(str).query?.split('&').collectEntries{ [(it.split('=').first()): it.split('=').last()]}
log.info map.token
log.info map.value
log.info map
def propertiesStep = context.testCase.testSteps["Properties"]
propertiesStep.setPropertyValue("map", map)
I got an error warning of a problem - also - I noticed that in the logging - log.info map returns a whole string within curly braces - unlike when property values are normally written to the logging.
Ok - that's it - could anyone advise how I can extract the two query parm values please?
If your answers could add a bit of explaining (or just good commenting maybe?) so I can then read up - I'm reading up on hashmaps and the split() method at the moment - cos I don't really understand what the 'def map' line in the script is actually doing - I can see the result - I just don't understand how it;s getting there.
Anyway -that's it - as always - I appreciate all and any help that anyone is kind enough to provide!
Cheers,
rich
In this use case, there is no need even to create map, then loop thru it and put the entries in Properties test step. Can be directly added to Properties step after the first split('&').
Here it goes the simplified further:
def properties = context.testCase.testSteps["Properties"] def str = 'https://whatevs.azurewebsites.net/path?token=eyJ0eXAiOiJKV1Qi&value=123456789' new URL(str).query?.split('&').each { properties.setPropertyValue(it.split('=').first(), it.split('=').last()) }