Ask a Question

How to trim a portion of a response in URL format and add result to Properties test step?

SOLVED
socaltester
Contributor

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...

6 REPLIES 6
socaltester
Contributor

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",""))

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

 

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

log.info 'https://FQDN/otm-console/controllers/ZoneConfig/Wizard/Step1?zone=MGF'.split('\\?').first()


Regards,
Rao.
Wamboo
Community Hero

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:

 

https://regex101.com/

 

and test the solution and then use it for example:

 

https://stackoverflow.com/questions/48964279/slice-a-string-by-regex

 

@richie Thank you. I used the Split option and it works well once I replaced .split('=') with .split('\\?')

 

@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()?

cancel
Showing results for 
Search instead for 
Did you mean: