How to trim a portion of a response in URL format and add result to Properties test step?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to trim a portion of a response in URL format and add result to Properties test step?
I need to trim a portion of an href pulled from a test response that has been stored into a Properties test step.
Example Response: https://FQDN/otm-console/controllers/ZoneConfig/Wizard/Step1?zone=MGF
In this specific example, I need to remove '?zone=MGF' and store the result into a Properties test step.
I've tried the following Groovy script without success:
import com.eviware.soapui.support.GroovyUtils; def str = ( '${Properties#@href}' ) //str = 'https://FQDN/otm-console/controllers/ZoneConfig/Wizard/Step1?zone=MGF' //Get the URL def trimmedURL = str.substring(str.lastIndexOf('?')+1, str.size()) //Save it at test case level custom property for later use context.testCase.setPropertyValue('trimmedURL', trimmedURL)
FYI. I'm new to Groovy.
I've checked out the following post as well. https://community.smartbear.com/t5/SoapUI-Open-Source/Extract-substring-from-response-body/td-p/4006...
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I solved it, but is there a better way?
import com.eviware.soapui.support.GroovyUtils; def string = 'https://FQDN/otm-console/controllers/ZoneConfig/Wizard/Step1?zone=MGF' def sub = string.substring(string.indexOf("h")-0, string.length()) log.info("before trim: " + sub) log.info("after trim: " + sub .replace("?zone=MGF",""))
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hey @socaltester
I'm not a coder to say what's better, but I've used 2 alternatives when trying to do this sort of thing - some sort of count function before now and I've used split() too.
Split option
//Split method example
//where URL property is https://whatevs.azurewebsites.net/?token=eyJ0eXAiOiJKV1Qi def fullURL = testRunner.testCase.getTestStepByName("Properties").getPropertyValue("URL") def extractedValue = fullURL.split('=')[1] //if [0] is used it grabs the value in the string BEFORE the ? if [1] is used it grabs the value after the ?
log.info(extractedValue)
Count thingy option
//Count thingy I found //where URL property is https://whatevs.azurewebsites.net/?token=eyJ0eXAiOiJKV1Q def fullURL = testRunner.testCase.getTestStepByName("Properties").getPropertyValue("URL") def extractedValue = fullURL[56..-22] log.info(extractedValue) //the left hand numeric is the last numbered digit you want to grab. //the right hand numeric is the first numbered digit you want to grab //counting starts from right hand side - so if you want to grab the full URL
//upto the ?, the array values would be [56..-22] - this should grab
//'https://whatevs.azurewebsites.net/' from the URL property value
The count option is what I used when I was just getting into playing with strings - it works fine - but only if the length of the value isn't going to change over time - if it does - then the string extracted will be incorrect - so the split option is definitely better for this reason.
How the split option compares with what you've specified I couldn't say - the coders on this thing will have opinions (and most likely - better ways of doing it) - but I don't! 🙂
Cheers,
rich
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
log.info 'https://FQDN/otm-console/controllers/ZoneConfig/Wizard/Step1?zone=MGF'.split('\\?').first()
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello!
In this case I would use a regular expression that would help you find a certain part of the string (from your post an example)
I write in JS but below I send an example of a regular expression that finds a piece of your string:
\?(.*)
Go to the website:
and test the solution and then use it for example:
https://stackoverflow.com/questions/48964279/slice-a-string-by-regex
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@richie Thank you. I used the Split option and it works well once I replaced .split('=') with .split('\\?')
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@nmrao Thank you for the response. I used Richie's Split example which provided info on grabbing the PropertyValue from the "Properties" step and replaced his .split('=') portion with your .split('\\?') example. Both [0] and .first() works. If I had used [1] the result would be the value post ? , so what would be the equivalent for .first()? .last()?
