Forum Discussion

socaltester's avatar
socaltester
Contributor
5 years ago
Solved

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/40063

  • richie's avatar
    richie
    5 years ago

    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

     

     

6 Replies

  • 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",""))
    • richie's avatar
      richie
      Community Hero

      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

       

       

      • socaltester's avatar
        socaltester
        Contributor

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

         

    • nmrao's avatar
      nmrao
      Champion Level 3
      log.info 'https://FQDN/otm-console/controllers/ZoneConfig/Wizard/Step1?zone=MGF'.split('\\?').first()