cancel
Showing results for 
Search instead for 
Did you mean: 

ReadyAPI: How to add query parameters to an URL as a Resource

SOLVED
Frequent Contributor

ReadyAPI: How to add query parameters to an URL as a Resource

Hi,

 

Setup of my testcase is a groovy script that needs to read a URL and find a particular beId.

When found, that parameter is saved on Project Level in 'Customer Property'.

A second part of the code need to fetch that parameter from the Project Level and add it to the same URL.

What I have so far:

 

//cfr.  https://www.leveluplunch.com/groovy/examples/get-webpage-content-url-uri/
//https://community.smartbear.com/t5/SoapUI-Pro/How-to-Parse-a-JSON-Response-Using-Groovy-Examples/td-p/155770
import groovy.json.JsonSlurper

def getConceptSchemes = "https://pdc.dig.ta.belgium.be/api/pdc/concepts/schemes".toURL().getText()
//parse json string using JsonSlurper - basically converts it to a groovy list
def parsedJson = new groovy.json.JsonSlurper().parseText(getConceptSchemes)
//get data
def findAGC = parsedJson.find { it.name == 'ACTIVITY_GROUP' }
//log.info "result for specific beID: " + findAGC
log.info (findAGC.beId)
log.info (findAGC.language)

//put beid in request parameter
//cfr.  https://www.soapui.org/scripting-properties/tips-tricks.html
testRunner.testCase.testSuite.project.setPropertyValue('param_beid', findAGC.beId )
testRunner.testCase.testSuite.project.setPropertyValue('param_lang', findAGC.language )

//add the beId to the URL and put it in the REST request
//cfr; : https://kousenit.org/2011/11/02/converting-groovy-maps-to-query-strings/
def base = "https://pdc.dig.ta.belgium.be/api/pdc/concepts/schemes"
def params = [beId:findAGC.beId]
def sendToURL = base + params.collect{k,v -> '$k=$v'}.join('&')

What I get as result:

the beId in logfile and a popupwindow that only gives me the [$k=$v] in the URL in a script. (see screenshot)

 

What I want:

beId saved on PropertyLevel (=OK)

beId fetched from PropertyLevel and in URL from RESTRequest but as a Resource, it needs to be as a parameter IN the URL AS a resource. So actually I should have something like this:

https://pdc.dig.ta.belgium.be/api/pdc/concepts/schemes?beId=79ebe16a-da8b-4ce4-a388-c8b2287a2238

 

where beId = $k  and the UUID=$v

 

Does anyone have an idea how to do this please?

 

Thanks in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Frequent Contributor

Re: ReadyAPI: How to add query parameters to an URL

@richie  OK, I found the solution on my own!  :-) 

 

Setup a Project in ReadyAPI with 

* 1 TestSuite

* 1 TestCase

* 2 teststeps: 1 Groovy and 1 REST Request

 

copy this code in the groovy (it seems like a lot but it's not.  I've just added a lot of comments for later or someone else)

 

//cfr.  https://www.leveluplunch.com/groovy/examples/get-webpage-content-url-uri/
//https://community.smartbear.com/t5/SoapUI-Pro/How-to-Parse-a-JSON-Response-Using-Groovy-Examples/td-p/155770
import groovy.json.JsonSlurper
/*=========================================================================================================*/
//declare your parameters on Project level. In this case I have declared 2 parameters: beId and language. 
//don't fill in anything.
/*==========================================================================================================*/
def getConceptSchemes = "https://pdc.dig.ta.belgium.be/api/pdc/concepts/schemes".toURL().getText()
//parse json string using JsonSlurper - basically converts it to a groovy list
def parsedJson = new groovy.json.JsonSlurper().parseText(getConceptSchemes)
//get data
def findAGC = parsedJson.find { it.name == 'ACTIVITY_GROUP' }
log.info (findAGC.beId)
log.info (findAGC.language)

//fill in beid-value in PropertyValue  'param_beid'
//cfr.  https://www.soapui.org/scripting-properties/tips-tricks.html
testRunner.testCase.testSuite.project.setPropertyValue('param_beid', findAGC.beId )
testRunner.testCase.testSuite.project.setPropertyValue('param_lang', findAGC.language )

In your 'Rest Request' not necessary to add a parameter. Except if you want it to be added to the parameters of the URL.

Go to the tab 'Projects', select the webservice that is linked to your Project, go to its Endpoint and add this in the resource of the URL:

beId=${#Project#param_beid}

beId = the parameter that should be looked for by the URL

Project = Soapcall, don't change this!

param_beid = the parameter that I defined on Project level for my parameter.

 

If you run the Groovy test step and the request test step you will see the response.

 

EDIT:

This is a possibility for if the file doesn't use rootnames or space declarations!

View solution in original post

6 REPLIES 6
Highlighted
Community Hero

Re: ReadyAPI: How to add query parameters to an URL

Hey @AboveAndBeyond,

I'm responding cos there have been a lot of views but no response yet. As you know im not a scripter, but i can probably work this out if i reproduce on my machine so i'll have a go...Rao, msiadak or one of the other coders will probably come back with an answer before i do though.

But question: your level of coding is about equivalent to mine, so why are you trying to use groovy to do the whole thing rather than use the native functionality? I get the enjoyment of getting a script working, but i always thought it was easier to build your scripting skills (if youre not a coder) by extending the functionality with groovy snippets a bit at a time rather than completely replacing the use of the functionality with code ?

In your description you mention submitting request to retrieve the beId and save at project level as Customer Property....according to your script there isnt a 'Customer Property' defined...is this is 'param_beid' and maybe 'param_lang'....is that correct?

Looking at the screenshot it looks like youve got a null for this parm....is this correct?

Cheers,

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 title of the post to something more descriptive? This will help people when searching for problems.
Frequent Contributor

Re: ReadyAPI: How to add query parameters to an URL

Hi @richie 

Thanks for your time an good effort! I appreciate this.

To answer your questions:

1)  I did started with groovy snippets step by step where as I came up to this result. The functionality can't do that as far as I know. Also you need to bear in mind how many times you need to repeat those actions. As I have 27 different codes suite to setup, that's a lot of repeats.... (I am on a very heavy project, actually to heavy for ReadyAPI, but as I'm not a java coder I want to use ReadyAPI to test the API's and do as much as possible all the rest....)

2) Apparently for calling the parameters from property level you need to use this line:

testRunner.testCase.testSuite.project.setPropertyValue('param_beid', findAGC.beId )

where "param_beid" is the name you gave to the property saved on Project Level and "findAGC.beId" is the value. In my case I' m searching for the value by parsing the json answer for a particular element (in this case, not the whole JSON file) and calling upon the value expected, for me the "beId".

3) For language I indeed receive an "null" message what is correct in my case.

 

Hope this helped a bit.

Cheers!

AAB

Frequent Contributor

Re: ReadyAPI: How to add query parameters to an URL

@richie  OK, I found the solution on my own!  :-) 

 

Setup a Project in ReadyAPI with 

* 1 TestSuite

* 1 TestCase

* 2 teststeps: 1 Groovy and 1 REST Request

 

copy this code in the groovy (it seems like a lot but it's not.  I've just added a lot of comments for later or someone else)

 

//cfr.  https://www.leveluplunch.com/groovy/examples/get-webpage-content-url-uri/
//https://community.smartbear.com/t5/SoapUI-Pro/How-to-Parse-a-JSON-Response-Using-Groovy-Examples/td-p/155770
import groovy.json.JsonSlurper
/*=========================================================================================================*/
//declare your parameters on Project level. In this case I have declared 2 parameters: beId and language. 
//don't fill in anything.
/*==========================================================================================================*/
def getConceptSchemes = "https://pdc.dig.ta.belgium.be/api/pdc/concepts/schemes".toURL().getText()
//parse json string using JsonSlurper - basically converts it to a groovy list
def parsedJson = new groovy.json.JsonSlurper().parseText(getConceptSchemes)
//get data
def findAGC = parsedJson.find { it.name == 'ACTIVITY_GROUP' }
log.info (findAGC.beId)
log.info (findAGC.language)

//fill in beid-value in PropertyValue  'param_beid'
//cfr.  https://www.soapui.org/scripting-properties/tips-tricks.html
testRunner.testCase.testSuite.project.setPropertyValue('param_beid', findAGC.beId )
testRunner.testCase.testSuite.project.setPropertyValue('param_lang', findAGC.language )

In your 'Rest Request' not necessary to add a parameter. Except if you want it to be added to the parameters of the URL.

Go to the tab 'Projects', select the webservice that is linked to your Project, go to its Endpoint and add this in the resource of the URL:

beId=${#Project#param_beid}

beId = the parameter that should be looked for by the URL

Project = Soapcall, don't change this!

param_beid = the parameter that I defined on Project level for my parameter.

 

If you run the Groovy test step and the request test step you will see the response.

 

EDIT:

This is a possibility for if the file doesn't use rootnames or space declarations!

View solution in original post

Community Hero

Re: ReadyAPI: How to add query parameters to an URL

Hey @AboveAndBeyond,

Sorry i havent had a chance to look at this yet, but i will start this afternoon.

Yes, youre correct the command youre using sets the property at project level.

Can you just explain the original issue the reasons why youve moved to 100% groovy script....you never described the 27codes issue before.
Obviously you dont want to throw away all the work youve done so far, but if you can give me some background this can only help my understanding if im looking at your groovyscript (im dumb that way....i need to understand 100% before my brain starts working). For example....i dont know these 27 codes are and how they fit in to what you are testing...are these the ACTIVITY_CODE you mentioned in your groovy?



Cheers,

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 title of the post to something more descriptive? This will help people when searching for problems.
Frequent Contributor

Re: ReadyAPI: How to add query parameters to an URL

@richie 

Hey richie, in short

I need to setup 27 'different' RestCalls for 27 different KBO-Codes. They're Belgium codes that will be present when we show our API's to the users. They will search on Companies (public search) and will get all information about that company included the ISO-Codes and the KBO Codes.

 

Now, I actually will clone 26 times the first TestSuite so I don't loose to much time in setting up. They are all 27 different but in this case the basics stays. Only the Assertions are different and their beId. But knowing that that beId will NEVER change, I'm setting them up separately.

So each TestSuite needs to fetch it's own beId from a JSON URL response. (That JSON response is filled with ALL concepts existing in our environment and they all have their own beId. But to see the content of each of them I need to search on their personal beId). 

So for my exemple I've setup ActivityGroupCodes for the 1st concept with it's own steps:

  • Groovyscript: GetbeId
  • Rest Request
  • Property Transfer
  • DataSink

Why the other steps? because I still need to save the result in a textfile for comparison later. (Remember? a few months ago I struggled with a comparison code to compare 2 different environments in two different extensions.... and actually all I've asked for here at SmartBear was in function of my project. And it's a big ass project!!!!  :-)   )

Does this make a little more sence?

 

Greatings,

AAB

Community Hero

Re: ReadyAPI: How to add query parameters to an URL

Hey @AboveAndBeyond 

 

I'm sorry - you've fixed the problem yourself! - I didnt see your post saying this (sorry - I tend to always use my phone to check the site and somehow I missed your post saying you had fixed your issue

 

Cheers,

 

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 title of the post to something more descriptive? This will help people when searching for problems.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors