ADS

Saturday, April 27, 2013

Just a simple Groovy test flow - Update, Execute and Assert

So after long study of SOAPUI, I thought of giving Groovy script a try with a real webservice test flow.

In this script we will do the following with Groovy:

  • Update the Request in the SOAP webservice 
  • Execute the SOAP webservice
  • Add xPath assertion
So let's get started and have a look at the sample webservice we will be using the following:

Request:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">
<soap:Header/>
<soap:Body>
<web:ConversionRate>
<web:FromCurrency>SEK</web:FromCurrency>
<web:ToCurrency>DKK</web:ToCurrency>
</web:ConversionRate>
</soap:Body>
</soap:Envelope>

Response:
<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>
<ConversionRateResponse xmlns="http://www.webserviceX.NET/">
<ConversionRateResult>5.7187</ConversionRateResult>
</ConversionRateResponse>
</soap:Body>
</soap:Envelope>

These two are our Request and Response. Now lets get started with our Groovy script what we want to achieve
// Step 1: Update the Request in the SOAP webservice

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def request = groovyUtils.getXmlHolder("<TestStepName>#Request")
request.namespaces["ns1"] = "http://www.webserviceX.NET/"

def fromCurrency = "SEK"
def toCurrency = "DKK"

request.setNodeValue("//ns1:ConversionRate/ns1:FromCurrency",fromCurrency)
request.updateProperty()
request.setNodeValue("//ns1:ConversionRate/ns1:ToCurrency",toCurrency)
request.updateProperty()

// Step 2: Execute the SOAP webservice.
testRunner.runTestStepByName("<TestStepName>")

// Retrieve Response
def response = groovyUtils.getXmlHolder("<TestStepName>#Response")
response.namespaces["ns2"] = "http://www.w3.org/2001/XMLSchema"

// Step 3: Add xPath assertion

def testst = testRunner.testCase.getTestStepByName("<TestStepName>")

// Following pseudo code will just get if any assertion of name xPath exists or not, if no assertion of xPath exists then it will procede to create a new xPath assertion else it will remove pre-existing before and then will create a nw one.
// Also I found very useful line in the SOAPUI forum
// Alls types of assertions:
// CrossSiteScriptAssertion, GroovyScriptAssertion, HttpDownloadAllResourcesAssertion, InvalidHttpStatusCodesAssertion, JdbcStatusAssertion, JdbcTimeoutAssertion, JMSStatusAssertion, JMSTimeoutAssertion, NotSoapFaultAssertion, ResponseSLAAssertion, SchemaComplianceAssertion, SensitiveInfoExposureAssertion, SimpleContainsAssertion, SimpleNotContainsAssertion, SoapFaultAssertion, SoapRequestAssertion, SoapResponseAssertion, ValidHttpStatusCodesAssertion, WSARequestAssertion, WSAResponseAssertion, WsdlMessageAssertion, WSSStatusAssertion, XPathContainsAssertion, XQueryContainsAssertion

def asserting = testst.getAssertionByName("XPath Match")
if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
{
testst.removeAssertion(asserting)
}

def assertion = testst.addAssertion("XPath Match")
assertion.path = "declare namespace ns1='http://www.webserviceX.NET/';\n //ns1:ConversionRateResponse/ns1:ConversionRateResult"
assertion.expectedContent = <type in here whatever the content you want to match with>

Hope it will be useful information for newbie.

1 comment: