Forum Discussion

richie's avatar
richie
Community Hero
6 years ago
Solved

Bit of Groovy (I'm almost there!) to grab a bit of the string value (like substring)

Hey fellas,

 

 

as my recent posts have indicated - I have a get request hitting a ReferenceData database that lots of different projects will use.  So the GET request's query parms represent certain SQL like operations and I need to test these work correctly - stuff like

 

value starts with value of 'ste' (sql would be a like --> where attribute like 'ste%';)

value ends with value of 'ste' (sql would be a like --> where attribute like '%ste';)

value contains value of 'ste' (sql would be a like --> where attribute like '%ste%';)

 

 

 

I've been digging around and I found a bit of java I could groovyfy - but it doesn't quite do what I want.

 

 

 

GET REST step
Prop Transfer (grabs certain Name attribute value of 'Andrew James' from GET REST step results)
Properties (hold the property value of 'Andrew James' grabbed by the transfer)
GroovyStep (to parse the Andrew James property, grab say the first 3 chars and store in a new property)
JDBC step (select * from table where attribute = '${Properties#new property}'

 

 

So I have an initial property entitled 'Name' with value 'Andrew James'

 

I have the following groovy

 

def namePropertyvalue = ('${Properties 1#Name}')
def namePropertyvaluePartial = namePropertyvalue[-5..-1]

log.info(namePropertyvaluePartial)

and I thought this would grab the last 5 characters of the value of  'Andrew James' but instead its actually returning the following: 

 

Sun Dec 16 17:34:42 GMT 2018: INFO: ${Properties 1#Name}

I was so excited when I didn't get a compilation error straight away when I ran it for the first time and started getting results!

 

Then I noticed it wasnt returning the value at all - just the bit of code representing my property placeholder! :(

 

thanks guys - really appreciate if anyone can help me!

 

richie

  • You can't just pick up variables like that in a groovy script. You need to expand the values.

     

    So instead of

    def namePropertyvalue = ('${Properties 1#Name}')

    use

    def namePropertyvalue = context.expand( '${Properties 1#Name}' )
  • You need to expand the values.

     

    So instead of using:

    def namePropertyvalue = ('${Properties#Name}')

    you should be using:

    def namePropertyvalue = context.expand( '${Properties#Name}' )

7 Replies

  • Lucian's avatar
    Lucian
    Community Hero

    You can't just pick up variables like that in a groovy script. You need to expand the values.

     

    So instead of

    def namePropertyvalue = ('${Properties 1#Name}')

    use

    def namePropertyvalue = context.expand( '${Properties 1#Name}' )
    • HimanshuTayal's avatar
      HimanshuTayal
      Community Hero

      Agreed Lucian,

       

      Correct syntax for fetching value from property step can be 

      def namePropertyvalue = context.expand( '${Properties 1#Name}' )

      or

      def namePropertyvalue = testRunner.testCase.getTestStepByName("Properties 1").getPropertyValue("Name")

       

      • richie's avatar
        richie
        Community Hero

        Thanks Guys,

         

        sorry about the delay in replying - I got stuck writing documentation all week - thanks so much for helping!

         

        Cheers,

         

        richie