Soapui read soap response to dialog for user entry - soap

SoapUI. I have a pretty old version of SoapUI (version 2.0.2)
We know in Soapui, we can present Alert, and input Dialogs..
Which work great..
I have the following SOAP Response and would like to read the question to present as a dialog.
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:Response xmlns:ns1="http://some.name.space.com">
<ns1:payload>
<ns1:Question>
<ns1:questionText>How much is that doggie in the window?</ns1:questionText>
</ns1:Question>
</ns1:payload>
</ns1:Response>
</soapenv:Body>
</soapenv:Envelope>
I would like to read or present the "questionText" to the tester..
def ui = com.eviware.soapui.support.UISupport;
ui.showInfoMessage("Hello World");
But, instead read the question (the questions do change) and to be something like:
def ui = com.eviware.soapui.support.UISupport;
ui.showInfoMessage("testStep.xmlResponse.questionText");
And then have where the tester can input their answer..
def ui = com.eviware.soapui.support.UISupport;
ui.prompt("Input Answer","Answer");
Thanks for the information at - Can I pause for console input in a SOAPUI groovy script?
Here is what I have been able to find so far on this (which does not work at this point):
// pulled information from:
// http://www.soapui.org/Developers-Corner/extending-soapui.html
import com.eviware.soapui.support.XmlHolder;
def ui = com.eviware.soapui.support.UISupport;
def holder = new XmlHolder ( messageExchangeContentAsXml)
holder.namespaces["ns1"] = "http://some.name.space.com"
def node = holder.getDomName ("//ns1:Response[1]/ns1:payload[1]/ns1:Question[1]/ns1:questionText[1]")
ui.showInfoMessage(node.questionText);
The error that I am getting at this point is:
org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No such property: messageExchangeContentAsXml for class: Script12
And I think I should be able to "pass" that on to the next request fairly easily.
Thanks.

I have figured this out..
http://www.soapui.org/Developers-Corner/extending-soapui.html
http://www.soapui.org/Scripting-Properties/tips-a-tricks.html
http://forum.soapui.org/viewtopic.php?t=2597
Reading from properties, we need the following:
def ui = com.eviware.soapui.support.UISupport;
groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
def propId = context.getProperty( "userName", "userName" );
ui.showInfoMessage( propId );
Reading from the Response in SOAPUI, we need to do the following:
def ui = com.eviware.soapui.support.UISupport;
groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
holder = groovyUtils.getXmlHolder("TestRequest#Response");
holder.namespaces["ns1"] = "http://some.namespace";
def responseId = holder.getNodeValue("//ns1:userName");
ui.showInfoMessage( responseId );

Related

How to build soap request with script view?

I'm trying to build a SOAP request with Groovy/Java.
However, I didn't found any information on web and on Katalon documentation.
There is a lot of documentation for REST testing, but not for SOAP ...
https://docs.katalon.com/katalon-studio/docs/create_rest_api_requests_manually.html#introduction
https://docs.katalon.com/katalon-studio/docs/web-services-builder.html
https://api-docs.katalon.com/
My objective is to do something like :
// THIS CODE IS WRONG !!!! It's just to give you an idea of my need
RequestObject requestObject = new SOAPRequestBuilder()
def variables = new HashMap<String, String>()
variables.put('variable', 'some text')
requestObject.setVariables(variables)
def response = WS.sendRequest(requestObject)
Someone has any idea how to do that? Thank you for help.
Update 1
I found this solution but I don't know if it is a best pratice ...
def request = findTestObject('RequestObject', [('variable') : 'some text'])
def response = WS.sendRequest(request)
If you're looking for something product-agnostic, you could use groovy-wslite, which handles both REST and SOAP. For example:
def client = new SOAPClient("http://...")
def response = client.send(SOAPAction: ...) {
body {
...
}
}

How to compare two SOAP responses in SoapUI for discrepancies, using Groovy script or something else

I would like to compare for discrepancies(namespaces, content) two SOAP responses in SoapUI that i get at the same time, and display the differences in the SoapUI log or log.info, currently i got only a piece of code to get the response and put it in a variable:
import java.util.regex.Matcher
import java.util.regex.Pattern
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) ;
def holder = groovyUtils.getXmlHolder( "GetAttachementList#Response" ) ;
log.info(holder.getXml());
You can use soapui assertions.
http://www.soapui.org/Functional-Testing/getting-started-with-assertions.html

SoapUI - Automatically add custom SOAP headers to outgoing request

So what I want to do is to automatically add SOAP header to every request that is generated in SoapUI as I've got hundreds of them and doing this manually is annoying.
Lets say that this is my example request generated from the WSDL which looks like that:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pol="http://something">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<pol:GetSomething>
<tag1>3504</tag1>
<tag2>ALL</tag2>
</pol:GetSomething>
</soapenv:Body>
</soapenv:Envelope>
and when I make the request I want SoapUI to modify it to look like that:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pol="http://something">
<soapenv:Header>
<token xmlns="ns1">${TOKEN}</token>
<user xmlns="ns2">user</user>
<system xmlns="ns3">system</system>
</soapenv:Header>
<soapenv:Body>
<pol:GetSomething>
<tag1>3504</tag1>
<tag2>ALL</tag2>
</pol:GetSomething>
</soapenv:Body>
</soapenv:Envelope>
Is it possible in SoapUI?
In your testCase you can add a first step of type Groovy Script, in this script you can manipulate each request to add necessary elements on <soap:Header>, I give you an example that works for me:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
def tcase = testRunner.testCase ;
// get total number of testSteps
def countTestSteps = tcase.getTestStepList().size();
// start with 1 to avoid groovy script testStep
for(i=1;i<countTestSteps;i++){
// get testStep
def testStep = tcase.getTestStepAt(i);
// get request
def request = testStep.getProperty('Request').getValue();
// get XML
def xmlReq = groovyUtils.getXmlHolder(request);
// get SOAPHEADER
def soapHeader = xmlReq.getDomNode("declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/'; //soap:Header")
// document to create new elements
def requestDoc = soapHeader.getOwnerDocument()
// create new element
def newElem = requestDoc.createElementNS(null, "element");
// insert in header
soapHeader.insertBefore(newElem, soapHeader.getFirstChild());
// now put your new request in testStep
log.info xmlReq.getXml();
testStep.setPropertyValue('Request', xmlReq.getXml());
}
This sample code only add one new element on the <soap:header>, but you can modify it to add attributes, text content and more nodes. You can also take a look at:
dynamically create elements in a SoapUI request | SiKing

Groovy HTTPBuilder SOAP response not being parsed properly

I don't understand why XmlSlurper is apparently not working on the result.
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
def String WSDL_URL = ...
def http = new HTTPBuilder( WSDL_URL , ContentType.XML )
String soapEnvelope =
"""<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetTerritories xmlns="...">
<State>AZ</State>
<ZipCode>85203</ZipCode>
</GetTerritories>
</soap12:Body>
</soap12:Envelope>"""
http.request( POST, XML ) {
headers."Content-Type" = "application/soap+xml; charset=utf-8"
headers."Accept" = "application/soap+xml; charset=utf-8"
body = soapEnvelope
response.success = { resp, xml ->
println "XML was ${xml}"
println "Territories were ${xml.Territories}"
println "State were ${xml.Territories.State}"
println "City was ${xml.Territories.Territory.City}"
println "County was ${xml.Territories.Territory.County}"
}
response.failure = { resp, xml ->
xml
}
}
leads to
XML was <Territories><State>AZ</State><ZipCode>85203</ZipCode><Territory><City>Mesa</City><County>Maricopa</County>...</Territory></Territories>
Territories were
State were
City was
County was
UPDATE: Thanks to John Wagenleitner's insight, I did a little more digging.
When I add that assert, I see an issue:
assert "Territories" == xml.name()
| | |
| | Envelope
| <Territories><State>AZ</State><ZipCode>85203</ZipCode</Territories>
false
Changing the request parameters from POST, XML to POST, TEXT is revealing:
XML was <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetTerritoriesResponse xmlns="...">
<GetTerritoriesResult><Territories><State>AZ</State><ZipCode>85203</ZipCode><Territory><City>Mesa</City><County>Maricopa</County>...</Territory></Territories>
</GetTerritoriesResult>
</GetTerritoriesResponse>
</soap:Body>
</soap:Envelope>
...
So it looks like the XmlSlurper, when the variable is being printed out, is throwing away the SOAP stuff and evaluating the innermost node () while not actually navigating to that node. Is this expected behavior?
I have been unable to find a a more complete and modern SOAP call and parse using httpBuilder, so I assumed XML would be the right content type. But it looks like I'll just have to accept TEXT and parse the body myself, which seems lame. Is there a better way of handling SOAP responses with httpBuilder?
I would recommend printing the raw text of the response:
println "XML was ${resp.data.text}"
Assuming that the printed XML line is what you expect (though odd since there is no Envelope or Body nodes), then you should be able to remove Territories from your references to xml. When parsed with XmlSlurper the root node is the GPathResult.
assert "Territories" == xml.name()
println "State were ${xml.State.text()}"
println "City were ${xml.Territory.City.text()}"
println "County were ${xml.Territory.County.text()}"
Also, just wanted to point out that the SOAP 1.2 media type is "application/soap+xml".
UPDATE:
So it looks like the XmlSlurper, when the variable is being printed
out, is throwing away the SOAP stuff and evaluating the innermost node
() while not actually navigating to that node. Is this expected
behavior?
Yes, the toString() method for a GPathResult just prints all text nodes and not the actual elements or attributes. With HTTPBuilder you can print out the raw response text by using:
println resp.data.text
I have been unable to find a a more complete and modern SOAP call and
parse using httpBuilder, so I assumed XML would be the right content
type. But it looks like I'll just have to accept TEXT and parse the
body myself, which seems lame. Is there a better way of handling SOAP
responses with httpBuilder?
The ContentType.XML is fine, the issue is with how the SOAP response that your web service returns is formed. The web service is sending back the Territories results as an encoded string in the GetTerritoriesResult element and not as part of the actual XML response that HTTPBuilder automatically parses for you (this is not a problem with the way HTTPBuilder is handling it). Because the data you really want is in that encoded string you will need to parse the text node of the GetTerritoriesResult yourself.
response.success = { resp, xml ->
println "XML was ${resp.data.text}"
def territories = new XmlSlurper().parseText(
xml.Body.GetTerritoriesResponse.GetTerritoriesResult.text()
)
println "State were ${territories.State}"
println "City was ${territories.Territory.City}"
println "County was ${territories.Territory.County}"
}

Test RESTful JSON Grails Webservice

I want to test my secured webservice to the following:
UrlMapping correct, so are the following services available or not?
Test GET/POST/PUT/DELETE and their rendered feedback as well as errors
Test error messages when logged in and not logged in
Can somebody give me some hints how to do this? I have no clue how accessing the grails security service and as well running tests against my controllers when logged in and when not. As well I need some Mock Server or something to test against my controllers or?
Sorry I am very new to this topic but I want to go in the right direction before loosing control over my webservices.
Thank you for your help!
We use the REST Client plugin along with the functional testing plugin to test all our web services.
For example...
void testCreateTag() {
def name = 'Test Name'
def jsonText = """
{
"class":"Tag",
"name":"${name}"
}
"""
post('/api/tag') {
headers['x-user-external-id'] = securityUser.externalId
headers['x-user-api-key'] = securityUser.apiKey
headers['Content-type'] = 'application/json'
body {
jsonText
}
}
def model = this.response.contentAsString
def map = JSON.parse(model)
assertNotNull(map.attributes.id)
Tag.withNewSession {
def tag = Tag.get(map.attributes.id)
assertNotNull(tag)
assertEquals(name, tag.name)
}
}
I have similar code which uses the built in (groovy 1.8) JsonSlurper which I think might be more reliable and only needs the functional test plugin but not the REST Client plugin.
String baseUrlString = 'http://localhost:8080/**YOURAPP**'
baseURL = baseUrlString
post('/j_spring_security_check?')
assertStatus 200
assertContentDoesNotContain('Access Denied')
get("/*your test URL*/")
def jsonObj = new JsonSlurper().parseText(this.response.contentAsString)
assertEquals(jsonObj.your.object.model, **yourContent**)