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

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

@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

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

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

Building a GET REST request's URI string (URI & Query Parms) Via Groovy Script

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
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors