I get a json response from a REST api that looks like below
{
"parentnode1": {
"childnode1": "abc12345-123-1234-1234-64a0251575f9",
"childnode2": "VAL1",
"childnode3": "format/pdf",
"childnode4": "name.pdf",
"base64content": "JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwRU9G"},
"messages": "This is a message"
}
I want to decode the value of "base64content" and then convert that into a pdf file and save it to a local directory. Is this possible in SOAP UI and Groovy ?
I came up with this groovy script but have not tested it in SOAPUI (have never used it). Try this and let me know what happens:
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText (json_response)
String content_decoded = object.base64content.decodeBase64()
def file = new java.io.File("output.pdf")
FileOutputStream fos = new java.io.FileOutputStream(file)
fos.write( content_decoded )
fos.flush()
fos.close()
Related
I am getting a zipped response from a server.
Using CURL and | gunzip I am able to get the unzipped content response, but I would like not to use CURL and decompress it directly via SOAPUI, by a header, or by a script.
I tried to write something like:
def responseBody=testRunner.testCase.getTestStepByName("getZIp").httpRequest.response.responseContent;
InputStream ins = new ByteArrayInputStream(responseBody.getBytes())
log.info responseBody
def outFile = new FileOutputStream(new File('/users/trythis.zip'))
if (ins) {
com.eviware.soapui.support.Tools.writeAll(outFile, ins )
}
ins.close()
outFile.close()
but the data is still compressed.
something straight-forward like:
InputStream ins = new ByteArrayInputStream(responseBody.getBytes())
def outFile = new File('/users/trythis.txt')
new GZIPInputStream( ins ).withReader{ outFile << it }
In SoapUI's preferences, there are options for working with API's which expected compressed payloads and/or send back compressed responses.
I've used this in the past and SoapUI decompresses the response and presents that in the UI so I didn't have to resort to Groovy scripts to read the response.
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"
}
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.
I am trying to assert the response, to check if the response value starts with a certain text. I tried using the function startsWith but it seems like it does not work in SOPAUI script assertion.
This is what I have tried:
import groovy.json.JsonSlurper
//grab the response
def ResponseMessage = messageExchange.response.responseContent.records
//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
//log.info jsonSlurper
assert jsonSlurper.startsWith("Text")
Here is the json response
{
"Name": "Natalie",
"message": "What are you doing"
}
I want to check if the Name starts with "Nat"
From documentation JsonSlurper().parseText(String text) returns:
data structure of lists and maps
so you can not use startsWith directly. To achieve what you want you have to go to the desired object in the path and use startsWith there. Something like must works for your case:
import groovy.json.JsonSlurper
def jsonStr = '{ "Name": "Natalie", "message": "What are you doing" }'
def jsonSlurper = new JsonSlurper().parseText(jsonStr)
assert jsonSlurper.Name.startsWith("Nat")
How can I write the output of an object to a teststep (Soaprequest) in soapUI using XmlNodePrinter.
I have the below groovy script in which I have an input xml file. I perform file operations and then would like to write the object using xmlnodeprinter, to a teststep (soaprequest) in soapUI (highlighted in bold...not sure wat should be going in place of ---)
I tried writing to an external file which works (highlighted in green)
def alert = com.eviware.soapui.support.UISupport;
//Define a file pointer for groovy to handle the file operations.
def inputFile = new File("V:\\Sample\\Sample.xml")
if(!inputFile.exists())
{
//Display an alert if the file is not found.
alert.showInfoMessage("Input File 'Sample.xml' not found!");
}
else
{
xml=new XmlParser().parseText(inputFile.text)
def nodeToDel=xml.A.B.find{it.#C3='1'}
def parent = nodeToDel.parent()
parent.remove(nodeToDel)
//new XmlNodePrinter(new PrintWriter(new FileWriter(new File('V:\\Sample\\e.xml')))).print(parent)
new XmlNodePrinter(new PrintWriter(new FileWriter(---))).print(parent)
}
define a string writer
def sw = new StringWriter()
new XmlNodePrinter(new PrintWriter(sw)).print(parent)
def modifiedXml = sw.toString()
modifiedXml variable will contain the xml with deleted nodes which you can further use for your test step.