Forum Discussion
- SmartBear_SuppoSmartBear Alumni (Retired)Hi,
yes - a script assertion will be needed - have a look at the examples at http://soapui.org/Functional-Testing/sc ... tions.html
regards!
/Ole
SmartBear Software - Target_MN_-_soaOccasional ContributorThat post does not explain how to capture header values in service response. I'm trying to validate some variable value in message header?
- SmartBear_SuppoSmartBear Alumni (Retired)Hi,
this example is what I meant:
assert messageExchange.responseHeaders["x-amz-id-1"] != null
perhaps you could use
def headers = messageExchange.responseHeaders["<name of your response header>"]
assert headers != null && headers[0] != null && headers[0].indexOf( "<some substring>" ) != -1
!?
regards,
/Ole
SmartBear Software - Target_MN_-_soaOccasional ContributorThanks, that worked as a script assertion.
How could I do the same if I wanted to do it in a separate groovy script step and not as a part of script assertion?
Thanks! - anallagundlaContributorYou can capture the script assertion header Value to properties and use those properties in another groovy script
- SmartBear_SuppoSmartBear Alumni (Retired)Hi,
in a script step directly after the request step you would do
// get results of previous teststep
def result = testRunner.results[testRunner.results.size()-1]
// assert desired header
def headers = result.responseHeaders["<name of your response header>"]
assert headers != null && headers[0] != null && headers[0].indexOf( "<some substring>" ) != -1
regards,
/Ole
SmartBear Software - NecraatallNew Contributor
try this sample code (valuesin UpperCases change):
//Validate request nodes REST
// the library import
import groovy.json.JsonSlurper//Validate response nodes REST
//def requestMessage = message.Exchange.request.requestContent
def responseMessage = messageExchange.response.responseContent
def json = new JsonSlurper().parseText(responseMessage)
//assert node values
// syntax: assert json.JSONPATH.ELEMENT.toString() COMPARE SYMBOLS LIKE IN IF (>,<,>=,<=,==,!=,you may use as comparing value "null")
def headers = messageExchange.getResponseHeaders()
assert ['HTTP/1.1 200 OK'] == headers["#status#"] : "Test Case HTTP Erorr: " + headers["#status#"]
log.info "Header status" + headers["#status#"]
assert ["YOURSERVER"] == headers["Server"] : "Test Case Server Erorr: " + headers["Server"]
log.info "Server: " + headers["Server"]assert json.response.responseHeader.toString() != null : "responseHeader is empty: " + json.response.responseHeader //validation on responseHeader is not empty
log.info "responseHeader is: " + json.response.responseHeader
assert json.response.responseHeader.ELEMENT.toString() != null : "ELEMENT is empty: " + json.response.responseHeader.ELEMENT //validation on ELEMENT is not empty
log.info "providerId is: " + json.response.responseHeader.providerId
assert json.response.responseBody.returnCode.toString() == "0" : "returnCode is not 0: " + json.response.responseBody.returnCode
log.info "returnCode is: " + json.response.responseBody.returnCode
//check for attachments
assert messageExchange.responseAttachments.length == 0 : "Response Attachments is not NULL"
log.info "Number of atachments " + (messageExchange.responseAttachments.length)// some other thing
/*
testStepName = messageExchange.modelItem.testStep.name //to get the Test Step Name
log.info testStepName
xmlHold = messageExchange.responseContentAsXml.toString() //to store the response as Xml
*/
Related Content
- 10 years ago
- 4 years ago
- 12 years ago