I'm new to SOAPUI and have several simple XML requests chained together. I want to use Groovy script to update an existing node on the Request side.
For example, I have GetRefData that starts:
<soapenv:Envelope xmlns:soapenv="aaa" xmlns:abc="bbb">
<soapenv:Header>
<abc:RequestHeader>
<CountryCode>US</CountryCode>
<MsgType>GetRefRq</MsgType>
</abc:RequestHeader>
</soapenv:Header>
etc...
I read the responses with no issues:
def GetRequestID = context.expand( '${GetRefData#Response#declare namespace abc=\'bbb\'; //abc:GetRefRq/MsgRqHeader/RequestId[1]}' )
How do I update the RequestId on the initial Request from 12345 to 53421? I've tried:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def request = context.expand('GetRefData#Request');
def requestHolder = groovyUtils.getXmlHolder(request);
requestHolder.namespaces["abc"] = "bbb";
def mypath = "//abc:GetRefRq/MsgRqHeader/RequestId[1]"
requestHolder.setNodeValue( mypath, "54321" )
But when I run it, although I get no errors and no updates.
Can someone please point this noob in the correct direction?
Try this :)
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def request = context.expand('GetRefData#Request');
def requestHolder = groovyUtils.getXmlHolder(request);
//namespace declaration
// assuming your node is <bbb:RequestId>12345</bbb:RequestId>
def ns = "bbb"
requestHolder.setNodeValue("//"+ns+":RequestId", "54321" )
//to verify
log.info requestHolder.getNodeValue("//ns:RequestId" )
Related
I have a question on how to assert the element_count equals to the number of objects from response.
The link to the API is https://api.nasa.gov/neo/rest/v1/feed?start_date=2019-05-10&end_date=2019-05-16&api_key=*******
I tried using the below code but did not have any luck trying to count the objects from the JSON response using grrovy script.
import groovy.json.JsonSlurper
def ResponseMessage = messageExchange.response.responseContent
def response = new JsonSlurper().parseText(ResponseMessage)
def elementCount = response.element_count
def idCount = response.count { it.equals('neo_reference_id') }
I was trying to count the number of neo_reference_id which should equal element_count. Any help would be great.
def url = new URL('https://api.nasa.gov/neo/rest/v1/feed?start_date=2019-05-10&end_date=2019-05-16&api_key=***')
def response = new groovy.json.JsonSlurper().parse( url )
def neo_references = response.near_earth_objects.collectMany{date,objects-> objects.collect{it.neo_reference_id} }
println neo_references
println neo_references.size()
assert response.element_count == neo_references.size()
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")
}
}
}
}
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
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).
I try to remove all head:book elements from following request excerpt:
<head:bookstore>
<head:book>9</head:book>
<head:book>10</head:book>
</head:bookstore>
requestHolder.getDomNodes("//head:bookstore/head:book").each {
requestNode.removeChild(it)
}
What am I doing wrong here?
updatE:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def requestHolder = groovyUtils.getXmlHolder("openBook#Request")
Here below the solution to remove the nodes of the XML, please notice that you can get the entire xml and you may need to edit the nodes name of the below script
// Get the entire request
def request = context.expand( '${Test Request#Request#declare namespace soap=\'http://www.w3.org/2003/05/soap-envelope\'; //soap:Envelope[1]}' )
// or create the XML
def BOOKS = '''
<bookstore>
<book>9</book>
<book>10</book>
</bookstore>
'''
def booksParser = new XmlParser().parseText(BOOKS)
def allBooks = booksParser.children()
booksParser.remove(allBooks)
new XmlNodePrinter().print(booksParser)
log.info booksParser