SOAP UI Groovy JsonNull Handling - groovy

Hello I'm new to this Groovy Script thing in SOAP UI can anyone tell me what i'm doing wrong? i can't do a if null condition with this code
//Getting Request
def RequestMessage=context.request
log.info RequestMessage
def jsonSlurper = new JsonSlurper().parseText(RequestMessage)
try{
if(jsonSlurper.iso8583Request.iso8583Detail.bit127){
log.info "127 is null"
}else{
testRunner.testCase.setPropertyValue("revBit127", "${jsonSlurper.iso8583Request.iso8583Detail.bit127}")
}catch{
log.info "127 is null"
}
Any advice to handle if null condition from this Json object?

You're not far off.
Firstly, I don't chain when using JSON Slurper, I tend to use it like this...
import groovy.json.JsonSlurper;
def response = context.expand( '${SOME REST Request#Response#$[\'message\']}' )
// Create a slurper object.
def slurper = new groovy.json.JsonSlurper();
// Create the JSON
def json = slurper.parseText(response);
In your example, I think this is wrong...
if(jsonSlurper.iso8583Request.iso8583Detail.bit127){
log.info "127 is null"
You're actually checking it exists, instead try...
if(!jsonSlurper.iso8583Request.iso8583Detail.bit127){
log.info "127 is null"
}else{
testRunner.testCase.setPropertyValue("revBit127", "${jsonSlurper.iso8583Request.iso8583Detail.bit127}")
}

Related

How to Capture Request and Response in testSuite TearDown using groovy

I have a testSuite with lets say 5 cases, i run the cases from my testSuite.
In my tear down script I want to capture all request and response of all testcases all test testSteps.
Below is the code I have written in tearDown TestSuite, problem in context.expand is returning empty. I assume testCase context is require, or not sure where i am going wrong.
tc_list = testSuite.getTestCaseList()
tc_count = tc_list.size()
for(i=0;i<tc_list.size();i++){
if(!tc_list[i].isDisabled()){
ts_list = tc_list[i].getTestStepList()
for(j=0;j<ts_list.size();j++){
req = testSuite.getPropertyValue("reportpath")+'/'+testSuite.getName()+'/'+tc_list[i].getName()+'/'+ts_list[j].getName()+'_RequestData.txt'
res = testSuite.getPropertyValue("reportpath")+'/'+testSuite.getName()+'/'+tc_list[i].getName()+'/'+ts_list[j].getName()+'_ResponseData.txt'
def request_expand = context.expand('${'+ts_list[j].getName()+'#Request}')
log.info '${'+ts_list[j].getName()+'#Response}'+tc_list[i].getName()
def response_expand = context.expand('${'+ts_list[j].getName()+'#Response}')
log.info response_expand
/* def req_file = new File(req)
req_file.write(request_expand,"UTF-8")
def res_file = new File(res)
res_file.write(response_expand,"UTF-8") */
}
}
}
#Ragesh kr
Whenever any time in Soap ui or Ready API you need RawRequest or RawResponse
you can just replace Request with RawRequest
and Response with RawResponse
i just did in your code and it worked
def request_expand = testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getPropertyValue("RawRequest")
def response_expand = testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getPropertyValue("RawResponse")
Some other examples to help everyone
When we just need request and response in soapui we can use below
When we just need RawRequest and RawResponse in soapui/ReadyaPI via groovy we can use below
req=context.expand('${RequestStepName#RawRequest}')
log.info req
res=context.expand('${RequestStepName#RawResponse}')
log.info res
The Below code worked for me. But I am still trying to capture rawRequest and rawResponse, which is still not achieved
tc_list = testSuite.getTestCaseList()
tc_count = tc_list.size()
for(i=0;i<tc_list.size();i++){
if(!tc_list[i].isDisabled()){
ts_list = tc_list[i].getTestStepList()
for(j=0;j<ts_list.size();j++){
req = testSuite.getPropertyValue("reportpath")+'/'+testSuite.getName()+'/'+tc_list[i].getName()+'/'+ts_list[j].getName()+'_RequestData.txt'
res = testSuite.getPropertyValue("reportpath")+'/'+testSuite.getName()+'/'+tc_list[i].getName()+'/'+ts_list[j].getName()+'_ResponseData.txt'
def request_expand = testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getPropertyValue("Request")
def response_expand = testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getPropertyValue("Response")
def req_file = new File(req)
def res_file = new File(res)
log.info testSuite.getTestCaseByName(tc_list[i].getName()).getName()+' '+testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getName()+' '+response_expand
if(request_expand!=null && response_expand!=null){
log.info testSuite.getTestCaseByName(tc_list[i].getName()).getName()+' '+testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getName()+' '+response_expand
req_file.write(request_expand,"UTF-8")
res_file.write(response_expand,"UTF-8")
}
}
}
}

How to check if an xml element is displayed using a script assertion

I want to perform an assertion within SOAP UI to check if the 'BookingCode' is displayed but I am not sure how to do it. I am using .size() but it keeps failing whether there is or isn't any booking code:
Below is an xml example but I xxx out the information:
<soap:xxx">
<soap:Body>
<getBookingsResponse xmlns="http://xxx/">
<Bookings>
<Booking Id="xxx" BookingCode="xxx" >
Below is the script assertion itself:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def serviceResponse = context.expand( '${getBookings#Response}' )
def xml = new XmlSlurper().parseText( serviceResponse )
def BookingRef = xml.'soap:Body'.getBookingsResponse[0].Bookings[0].Booking.#BookingCode
assert BookingRef.size() != 0
Thank you
Here you go, comments in-line:
Script Assertion
//Pass the xml string to parseText method
def pxml = new XmlSlurper().parseText(context.response)
//Get the BookingCode attributes
def codes = pxml.'**'.findAll{ it.name() == 'Booking' }*.#BookingCode*.text()
log.info codes
assert codes.size !=0

How to parse a URL to fetch it's parameters in groovy?

I am new to groovy scripting and looking on to parse the URL and print it's parameter.
This url is : https://www.google.com/?aaa=111&bbb=222&ccc=33&dd=1484088989_b23f248ac6e5d9a9b47475526bb92ee1
How can i fetch dd parameter from the URL?
I appreciate your help!
You need to add a groovy script.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
def testCase = context.testCase;
def testStep = testCase.getTestStepByName("NAME_TESTStepRequest");
def endpoint =testStep.getPropertyValue('Endpoint');
log.info endpoint;
def url = new URL(endpoint)
//def url = new URL("https://www.google.com/?aaa=111&bbb=222&ccc=33&dd=1484088989_b23f248ac6e5d9a9b47475526bb92ee1")
// get all query params as list
def queryParams = url.query?.split('&') // safe operator for urls without query params
// transform the params list to a Map spliting
// each query param
def mapParams = queryParams.collectEntries { param -> param.split('=').collect { URLDecoder.decode(it) }}
// assert the expected values
log.info mapParams['aaa']
//assert mapParams['aaa'] == '111'
log.info mapParams['bbb']
//assert mapParams['bbb']== 'abc'
log.info mapParams['dd']
//assert mapParams['dd']=='023423'
Please you check this post.Get query params from request url soapui using groovy

Soap UI: Groovy Script to call an API if the response is true

I am very new to use SoapUI. Writing test cases for my project APIs.
My requirement is to run a groovy script after an API call and if the response text of this API is "true", another API should call.
I found myself stuck to do this. Can anyone guide me to do this.
Thanks in advance!!!
I found answer but forgot to inform over here. I did asserted an script like this in TestStep:
def slurper = new groovy.json.JsonSlurper()
def responseJson = slurper.parseText(messageExchange.getResponseContent())
assert responseJson instanceof Map
assert responseJson.containsKey('authToken')
def id = "Bearer "+responseJson['authToken']
log.info(id.toString())
testRunner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(context.testCase.testSuite.project.getTestSuiteByName("TestSuite").getTestCaseByName("TestCas"), null)
def tcase = testRunner.testCase
def tstep = tcase.getTestStepByName("TestStep")
testContext = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext(tstep)
runner = tstep.run(testRunner, testContext)
Little idea for this :
def response = context.expand( '${TestRequest1#Response}' )
if ( response == 'true' )
{
testRunner.runTestStepByName( "TestRequest2")
}
Disable your first test Step(TestRequest1).

Using XMLParser on SoapUI response

Trouble using XmlParser in SoapUI Pro
Hi, I am trying to use 'xml parser' to validate my xml responses in SoapUI Pro.
I have been playing around with this in a groovy script and I can access the tags if I declare and assign my xml within the groovy script like so
This works if I declare the xml in the script ..
def xml = """
<NS1:createShipmentResponse xmlns:NS1="http://www.royalmailgroup.com/api/ship/V1">
<NS1:integrationHeader>
<dateTime xmlns="http://www.royalmailgroup.com/integration /core/V1">2013-12-24T22:20:34</dateTime>
<version xmlns="http://www.royalmailgroup.com/integration/core/V1">1</version>
<identification xmlns="http://www.royalmailgroup.com/integration/core/V1">
<applicationId>111111113</applicationId>
<transactionId>420642961</transactionId>
</identification>
</NS1:integrationHeader>
<NS1:completedShipmentInfo>
//xml not complete, other info in here.
</NS1:completedShipmentInfo>
<NS1:integrationFooter>
<warnings xmlns="http://www.royalmailgroup.com/integration/core/V1">
<warning>
<warningCode>W0022</warningCode>
<warningDescription>The customerReference specified is longer than 12 characters and has been truncated</warningDescription>
</warning>
<warning>
<warningCode>W0026</warningCode>
<warningDescription>The departmentReference specified is invalid and will be ignored</warningDescription>
</warning>
</warnings>
</NS1:integrationFooter>
</NS1:createShipmentResponse>
"""
def parser = new XmlParser().parseText(xml)
parser.'NS1:integrationFooter'.warnings.warning.warningCode.each{
log.info it.text()
}
But it doesn't seem to work within a running test instance when I instantiate the xmlParser variable from my Soap response as below.
def response = context.expand( '${createShipment_v04#Response}' );
I know that the parser variable has been assigned the xml response because when I can print it to the log ..
i.e. log.info parser prints ...
Wed Jan 08 16:33:38 GMT 2014:INFO:{http://schemas.xmlsoap.org/soap/envelope /}Envelope[attributes={}; value=[{http://schemas.xmlsoap.org/soap/envelope/}Body[attributes={}; value=[{http://www.royalmailgroup.com/api/ship/V1}createShipmentResponse[attributes={}; value=[{http://www.royalmailgroup.com/api/ship/V1}integrationHeader[attributes={}; .......
But below code does not print anything when I instantiate the xmlParser request from the soap response.
parser.'NS1:integrationFooter'.warnings.warning.warningCode.each{
log.info it.text()
}
Any help would be much appreciated.
I believe you are working at the wrong level.
parser.Body….
Ok. It turns out I don't need the 'NS1:' part. The below works ..
slurper.Body.createShipmentResponse.integrationFooter.warnings.warning.warningCode.each{
log.info it.text()
}
Following should work:
def response = context.expand( '${createShipment_v04#Response}' );
def parser = new XmlSlurper().parseText(response)
def warningCodes = parser.'**'.findAll {
it.name()=='warningCode'
}
warningCodes.each {
log.info it
}

Resources