cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
Community Hero

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

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
2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
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}' )


If my answer helped please click on the 'Accept as Solution' button.

View solution in original post

Highlighted
Community Hero

You need to expand the values.

 

So instead of using:

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

you should be using:

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


If my answer helped please click on the 'Accept as Solution' button.

View solution in original post

7 REPLIES 7
Highlighted
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}' )


If my answer helped please click on the 'Accept as Solution' button.

View solution in original post

Highlighted
Community Leader

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

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
Community Hero

Thanks Guys,

 

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

 

Cheers,

 

richie

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
Highlighted
Community Hero

Hey Guys,

 

I was just looking at this again and I noticed something - I have the following script

 

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

def namePropertyvaluePartial = namePropertyvalue[-5..-1]

log.info(namePropertyvaluePartial)

And this does what I want - i.e. it grabs the last 5 digits of the Name property value in the Properties step and writes the value out. - i.e the Name value was 'Kevin Spacey' and the returned value was 'pacey'

 

HOWEVER -if I execute the following (using alternative method to parse the Name property value)

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

def namePropertyvaluePartial = namePropertyvalue[-5..-1]

log.info(namePropertyvaluePartial)

The returned value was 'Name}' which I'm guessing is cos this is the last 5 digits of the 'namepropertyvalue' variable.

 

I thought from response the following were alternative options to do this same thing - but their behaviour indicates otherwise??????

 

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

OR

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

 

AmI just being dumb?  did I misunderstand?

 

thanks guys,

 

richie

 

 

 

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
Highlighted
Community Hero

You need to expand the values.

 

So instead of using:

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

you should be using:

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


If my answer helped please click on the 'Accept as Solution' button.

View solution in original post

Highlighted
Community Hero

god I'm dumb sometimes!

 

Cheers @Lucian 🙂

 

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
Highlighted
Community Hero

Don't worry about it. I am also the same sometimes Smiley Very Happy



If my answer helped please click on the 'Accept as Solution' button.
New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Want a fun and easy way to learn ReadyAPI and show off your skills? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get a full Raw JMS request

Participate!

Compare an expected JSON value and actual response in Events

Participate!

Fetch value/data from JSON response using Groovy Script

See replies!

Get data from Petstore and add it to Excel sheets

Participate!
Top Kudoed Authors