How to create an object in Custom properties using groovy? - groovy

i'm new in soapui and groovy.
i'm trying to create an object in Custom Properties, the value of the object is already retrived with a groovy code from json response after a first request.
How can i create the object in Custom Properties?
Here is the first Groovy code which extract the value of "id" from the json response:
import groovy.json.JsonSlurper
responseContent = testRunner.testCase.getTestStepByName("Request_1").getPropertyValue("response")
slurperresponse = new JsonSlurper().parseText(responseContent)
log.info (slurperresponse.id)
Output:
INFO:100
I tried this:
def Id = context.expand('${Id#Id}')
context.getTestCase().setPropertyValue('Id', Id);
The value of the variable "Id" in Custom Properties of the TestCase is empty
What i was expecting is to get Id=100
THank you

Related

How to assert if item from response is equal to item from properties

I'd like to make assert to check that data which I sent by REST request with json is equal to item from test case properties. I dont know how to deliver it from test request properties.
Initialy I am traying wrote script assertion like below, but probbaly getProperty doesnt work:
import groovy.json.JsonSlurper
def responseMessage = messageExchange.response.responseContent
def json = new JsonSlurper().parseText(responseMessage)
assert json.items[0].agreementTypeID == testRunner.testCase.getPropertyValue('agreementTypeID').toInteger()
// Script assertion only context will work
log.info context.testCase.getPropertyValue("agreementTypeID")
// Script assertion testrunner will not work
log.info testRunner.testCase.getPropertyValue('agreementTypeID')
So if you replace the testRunner with context it should work in Script Assertion .
TestRunner is available in Groovy step and there is a special way of making testRunner available in Script Assertion , but above is better

How to pass params to a HTTP request (teststep) in a SOAP UI Test case using groovy and run it

I am writing a groovy script to execute/automate my test suite. In one test case i have a HTTPRequest where i have a request URL, parameters( username and password) and method( GET) to get a token-id and then i would pass that token id to my next step( a SOAP request)to get the data.
I am stuck at a point where i need to pass the params(username and password), request URL and method(GET) using groovy.
I have a test step created manaully under a test case, i just need to pass the params
as i search online i got to know how to pass headers,url to a SOAP request which is like below
def headers = new StringToStringMap()
testRunner = new com.eviware............WsdlTestCaseRunner(myTestCase,null);
testStepContext = new com.eviware.soapui........WsdlTestRunContext(testsetp);
headers.put("apikey", "abcd")
teststep.getTestRequest().setRequestHeaders(headers)
teststep.getHttpRequest().setEndpoint(encpointurl);
testsetp.run(testRunner ,testStepContext )
but i looking to know how to pass params to a http request(test step) and run it.
Add a Properties teststep to your testcase. Just let it keep the default "Properties" name.
Add the properties to the Properties teststep, that you need to transfer
Inside your groovy teststep, you may set the properties using something like:
def properties = testRunner.testCase.getTestStepByName("Properties");
properties.setPropertyValue("name", "value");
Add the parameters directly in your request using variables in the format ${Properties#name} and replace "name" with the actual parameter name. This can be done both in the request body and in the URL if you should wish to do so.
It can be done completely in groovy by using groovy.json.JsonBuilder Class.
def body = new StringToStringMap()
def jsonbildr = new JsonBuilder()
body.put("username","Hackme")
body.put("password","LockUout")
def root = jsonbildr body
jsonbildr = jsonbildr.toPrettyString()
log.info(jsonbildr)
testStep.setPropertyValue("Request", jsonbildr)
Output :
{
"password": "LockUout",
"username": "Hackme"
}

Can't extract data from RESTClient response

I am writing my first Groovy script, where I am calling a REST API.
I have the following call:
def client = new RESTClient( 'http://myServer:9000/api/resources/?format=json' )
That returns:
[[msr:[[data:{"level":"OK"}]], creationDate:2017-02-14T16:44:11+0000, date:2017-02-14T16:46:39+0000, id:136]]
I am trying to get the field level, like this:
def level_value = client.get( path : 'msr/data/level' )
However, when I print the value of the variable obtained:
println level_value.getData()
I get the whole JSON object instead of the field:
[[msr:[[data:{"level":"OK"}]], creationDate:2017-02-14T16:44:11+0000, date:2017-02-14T16:46:39+0000, id:136]]
So, what am I doing wrong?
Haven't looked at the docs for RESTClient but like Tim notes you seem to have a bit of a confusion around the rest client instance vs the respons object vs the json data. Something along the lines of:
def client = new RESTClient('http://myServer:9000/api/resources/?format=json')
def response = client.get(path: 'msr/data/level')
def level = response.data[0].msr[0].data.level
might get you your value. The main point here is that client is an instance of RESTClient, response is a response object representing the http response from the server, and response.data contains the parsed json payload in the response.
You would need to experiment with the expression on the last line to pull out the 'level' value.

How do I set a value in JSON response in Groovy script in SOAP UI

I have two REST request which I am trying to run in SOAP UI. I want to feed response from one REST request to the other REST request. I am using property transfer for this. I first want to modify a Value in Response from 1st request and then feed it . I am trying this
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
// request
def request = '''{"data":{
"supplementaryMessages" : [ ],
"requisitionHeaders" : [ {
"dataOperation" : "UPDATE"
} ]
}}'''
// parse the request
def jsonReq = new JsonSlurper().parseText(request);
// get the response where you've the values you want to get
// using the name of your test step
responseContent = testRunner.testCase.getTestStepByName("Fetch1").getPropertyValue("response")
// parse response
def jsonResp = new JsonSlurper().parseText(responseContent)
// get the values from your first test response
// and set it in the request of the second test
jsonResp.data.requisitionHeaders.dataOperation = jsonReq.data.requisitionHeaders.dataOperation
When I run this I am getting the Type Mismatch error .It could be very simple issue but I am new to Groovy .

How to set Project property value using Groovy?

I want to set property in Project level, with my groovy code I can set a property in TestCase level. How can do the same to set property in Project level
Here is my code:
import groovy.json.JsonSlurper
responseContent = testRunner.testCase.getTestStepByName("TestStepName").getPropertyValue("response")
slurperresponse = new JsonSlurper().parseText(responseContent)
slurperresponse.id.toString()
log.info (slurperresponse.id.toString())
property_name = 'a'
def idProperty = setupTestCase.getProperty(property_name).toString()
setupTestCase.setPropertyValue('a',slurperresponse.a.toString())
Here I'm setting the TestCase property "a" from the response of a TestStepName.
I'm using Soapui.
Thank you
You can add a project property in SOAPUI using groovy with the follow code:
testRunner.testCase.testSuite.project.setPropertyValue("yourProp", yourValue )
Using your code:
import groovy.json.JsonSlurper
responseContent = testRunner.testCase.getTestStepByName("TestStepName").getPropertyValue("response")
slurperresponse = new JsonSlurper().parseText(responseContent)
slurperresponse.id.toString()
log.info (slurperresponse.id.toString())
testRunner.testCase.testSuite.project.setPropertyValue("a",slurperresponse.a.toString())
Hope this helps,

Resources