I am attempting to use the contents of a variable to replace the body of a SOAP request in the Object Repository as in:
import com.kms.katalon.core.testobject.RequestObject as RequestObject
RequestObject request = findTestObject(‘New SOAP Request’)
strSOAPBody = ‘’‘<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“XML Schema” xmlns:soap12=“http://www.w3.org/2003/05/soap-envelope”>
soap12:Body
1.1.1.1
</soap12:Body>
</soap12:Envelope>’‘’
‘Change SOAP Body’
request.setSoapBody(strSOAPBody)
The problem is that the SOAP Request Object in the repository requires body text to be present (either ‘hard coded’ or from a file) in order to work. That text is always used instead of what is defined in the variable and ‘set’ via .setSoapBody(strSOAPBody). How can I replace that text with what is in the variable?
Mario,
I have tried the setSoapBody() function and saw that the Body could be set to new value.
RequestObject request = findTestObject(‘SOAP_ConvertWeight’);
request.setSoapBody(“should be fail”);
Does it only happen with variable or could you replace the variable with a new soapbody?
It fails using text directly too as in
request.setHttpBody("""<?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
8.8.8.8
</soap12:Body>
</soap12:Envelope>""")
I think the multi-lined XML with embedded quotes just isn’t handled well by the repository object. This is a shame, because tools like Jmeter allow you to substitute freely as in ${strIPAddress}. Groovy supports this too. Reading in from a file works, but I can’t substitute anything. I am trying to do data driven API testing (like I can with Jmeter).
Mario,
I have following script working. Could you have a look on it and adjust with your case? In the example, I replace the variable “weight” in the script mode, print the body out for verification before submit the result.
‘Send a SOAP request and returns its response’
RequestObject request = findTestObject(‘SOAP_convertWeight’)
String body = “”"<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/”>
soap:Body
<ConvertWeight xmlns="http://www.webserviceX.NET/">
**<Weight>%s</Weight>**
<FromUnit>Kilograms</FromUnit>
<ToUnit>Grams</ToUnit>
</ConvertWeight>
</soap:Body>
</soap:Envelope>
“”"
request.setSoapBody(String.format(body, weight))
log.logInfo(request.getSoapBody())
//def response = WS.sendRequest(findTestObject(‘SOAP_ConvertWeight’))
def response = WS.sendRequest(request)
log.logInfo(response.getResponseBodyContent())
‘Verify converted weight after sending request is correct or not’
WS.verifyElementText(response, ‘ConvertWeightResult’, ‘3000’)
request.setSoapBody(String.format(strSOAPBody)) worked.
response = WS.sendRequest(request) worked.
WS.verifyElementText(response, ‘GetGeoIPResponse.GetGeoIPResult.IP’, ‘8.8.8.8’). worked.
I can substitute parameters by playing with the XML string.
OK, thanks for this.
BTW … request.setSoapBody(strSOAPBody) worked too … String.format was not required.
Final script that works (variables BOLD)!
RequestObject request = findTestObject(‘New SOAP Request - Copy’)
// read Object Repository (request), substitute, and send local object (request) not repository object
// then compare against response and not repository object
// use repository object as a template or abstract class (create new based on it)
def strSOAPBody = “”"<?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
${strIPAddress}
</soap12:Body>
</soap12:Envelope>"""
‘Set Soap Body equal to XML string’
request.setSoapBody(strSOAPBody)
‘Send IP address and get response.’
response = WS.sendRequest(request)
‘Expect 200 HTTP response (found).’
WS.verifyResponseStatusCode(response, 200)
‘Verify return IP address sent.’
WS.verifyElementText(response, ‘GetGeoIPResponse.GetGeoIPResult.IP’, strIPAddress)
‘Verify location country of IP address returned.’
WS.verifyElementText(response, ‘GetGeoIPResponse.GetGeoIPResult.CountryName’, strLocation)