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