cancel
Showing results for 
Search instead for 
Did you mean: 

A bit more groovy help? Populating a REST URI with Properties Set On TestCase?

SOLVED
Highlighted
Community Hero

A bit more groovy help? Populating a REST URI with Properties Set On TestCase?

Hey!

 

I'm currently trying to play around with some groovy snippets people have helped me with to build a REST request using parameters saved as properties on the TestCase object - but surprise, surprise! it doesnt work! 

 

to reiterate - I was thinking about setting a couple of properties on the TestCase object and then grabbing the values to build the REST URI. 

 

So - I was thinking have a groovy step, followed by a REST step - the groovy is as follows:

 

// get CommodityType_Name and EffectiveFrom properties from TestCase - i.e. properties were set at TestCase level level
def prop1 = testRunner.testCase.getPropertyValue( "CommodityType_Name" )
def prop2 = testRunner.testCase.getPropertyValue( "EffectiveFrom" )


// write the CommodityType_Name to the REST Request step
testRunner.testCase.testSteps["REST Request"].setPropertyValue( "CommodityType_Name", prop1 )

log.info("prop1")

// write the EffectiveFrom to the REST Request step
testRunner.testCase.testSteps["REST Request"].setPropertyValue( "EffectiveFrom", prop2 )

log.info("prop2")

First things first - according to some help @Lucian gave me last week - I'm thinking my mistake here is that I need to expand the property first (but I don't know the syntax to do this on my "getProperty line" line) - currently my log.info debug attempt is returning the propertynames rather than the values - would anyone know how to do this?

 

 

Finally - when I run the test case - I was 'hoping' that the CommodityType_Name and EffectiveFrom parms and values would be built into the REST URI - they weren't.  Would anyone know what I'm doing wrong here?

 

 

LowPriority:  Finally - and i know this is a little open subject so shouldnt be asking - but its always so frustrating my having to ask  you guys for coding help....does anyone know any "SoapUI groovy from scratch, no previous coding experience" books/resources/artefacts I can pick up?  There's lots of stuff out there - but its for the person who can already code - I need something that can teach me about it knowing absolutely nothing, the groovy tutorials out there aren't tailored for someone with my minimal coding skillset!

 

Thanks guys - appreciate all/any advice/guidance/help you can give 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
12 REPLIES 12
Highlighted
Community Hero

@richie,

Again thanks for the details.
It appears that every thing can be done in single groovy script.
But leaving it with script from previous reply as that is your approach and not like to intercept or deviate.

I believe now, you have got all the pieces ready what you were looking for with it. Hope you can apply to your test and proceed to the testing.


Regards,
Rao.
Highlighted
Community Hero

Hey @nmrao 

 

I'm stuck on documentation at the moment - so I cant try yet - (I'll be trying late tonight) but I just wanted to get my thanks in right now.

 

Again - you save my life fella - just wish I could return the favour!

 

Anyway - cant thank you enough - Kudos added as always - see your PM!

 

Will test it later and confirm everything's good and I understand and then hit the solution accept button.

 

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

Hey @nmrao 

 

Yay! ok - got it working - I was getting this weird error where instead of a 200 - it was failing and the returned status was 301 (permanently moved) - but I got it working - I ran the exact same test step (using the OTB functionality) and noticed that even though it's http - there must be some FWD or redirect cos the RAW of the OTB rest step indicated it was https.

 

Once I'd changed it to https in the script I started getting the 200!

 

Also - I wanted to parameterize the template parameters (see below) - but it wasn't working - I kept getting unable to find namespace response

 

i.e. i wanted to change this line

def binding = def binding [namespace : 'certification-nomenclature', dataset : 'certification-nomenclature']

 parameterizing the namespace and dataset values so they are picked up from properties set on the testcase

 

i tried 

def binding = [namespace : '${#TestCase#namespace}' , dataset : '${#TestCase#dataset}']

but I still got the 'unable to find namespace' message

 

so (after engaging my brain) I tried 

def binding = [namespace : context.expand( '${#TestCase#namespace}') ,  dataset : context.expand('${#TestCase#dataset}')]

and it worked - yay!

 

SO - just in case anyone else needs to do this - I'm repeating the working script below:

 

import wslite.rest.*
def serviceHost = 'https://endpointvalue' 
//Below to handle template parameter, additional $ required before
def getNamespaceDatasetPath = '''/api/1/${namespace}/${dataset}'''
//define all template param values as shown below - add as many as you want in here - the parameters are saved as properties on the TestCase object in the test
def binding = [namespace : context.expand( '${#TestCase#namespace}') ,  dataset : context.expand('${#TestCase#dataset}')]

def template = new groovy.text.SimpleTemplateEngine().createTemplate(getNamespaceDatasetPath)

def queryParams = [:]
//Get the properties of Property Test step - this code here grabs properties and their values and builds the REST requests Query Parameters (both names and values)
context.testCase.testSteps["Properties"].properties.each {
	queryParams[it.key] = it.value.value
}
def client = new RESTClient(serviceHost)
def response = client.get(path: template.make(binding) as String,
					 accept: ContentType.JSON,
					 query : queryParams
					 )
assert response.statusCode == 200
log.info groovy.json.JsonOutput.prettyPrint(response.text)

Thank you!!!!!!!!

 

marking as solved, more Kudos and changing the title of the post so its a little more concise to enable people searching

 

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