ADS

Sunday, April 28, 2013

Just a simple Groovy test flow - Data Driven

My previous post Just a simple Groovy test flow - Update, Execute and Assert states a simple flow of a simple test case, but in that scenario everything was hard-coded in the code, means we have provided value in code only but what if we have to execute the same test for different set of values, then we can easily use data driven technique i.e., we can use excel file - store our test data over there and then execute the groovy code.

Yes, I know this might have already made you jumped out of your seat, so without delaying further let me also jump straight to the point.

For accessing data driven using Excel file we will be using JExcelApi. You need to download the jxl.jar file and place it in you /SOAPUI-Installation folder/lib folder and then make sure you also need to restart your SOAPUI.

Modifying the code we have created for the previous post, our new code will look like this.
import jxl.*

// 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/"

// Opening the Excel file

Workbook testData = Workbook.getWorkbook(new File("D:\\soapui practise\\test.xls"))
// Opening the first sheet
Sheet sheet1 = testData.getSheet(0)
// Retrieving the data

Cell a1 = sheet1.getCell(0,0) // Retrieve the fromCurrency
Cell a2 = sheet1.getCell(1,0) // Retrieve the toCurrency

def fromCurrency = a1.getContents()
def toCurrency = a2.getContents()

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 new one.

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 = "//ConversionRateResponse/ConversionRateResult"
assertion.expectedContent = <type in here whatever the content you want to match with>

There is one point which you need to take care of creating test data in Excel file, which is, JExcelAPI only works till Excel 2003 and not there after, so you need to create Excel file in Excel 2003 or below.

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.

Tuesday, April 2, 2013

Handling SOAP Request and Response from Groovy script

In this post, we will be learning how we can access SOAP Requests and Response using Groovy Scripts.

Let us take the following example
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>

Now we want to access the content of above SOAP Request and Response, then we can use the following code:
import com.eviware.soapui.support.XmlHolder

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def step = context.testCase.getTestStepAt(0)
if( step instanceof com.eviware.soapui.model.testsuite.SamplerTestStep && step.testRequest.response != null ) // this step indicate that retrieved TestStep is a SOAP request and response is present for it
{
def response = groovyUtils.getXmlHolder( step.name +"#Response")
def request = groovyUtils.getXmlHolder(step.name + "#Request")
response.namespaces["ns"] = "http://www.w3.org/2001/XMLSchema"
log.info (request.getNodeValue("//ConversionRateResponse/ConversionRateResult")) 
log.info response.isEmpty()
log.info response.getNamespaces()

For more functions which you can do with SOAP Request and Response can be found at http://www.soapui.org/apidocs/index.html and go to package com.eviware.soapui.support and XmlHolder class in it.

Monday, April 1, 2013

Basic Groovy Scripts commands for SOAPUI

Using Groovy scripts in SOAPUI can help you do lots of stuff but beginning with this feature can be at times difficult for new users. Following are the commands that you can find really useful for starting with Groovy scripts in SOAPUI.

Go to the project level using the command
def project = testRunner.testCase.testSuite.project

Access the Test Suite using the following commands
def testsuite = project.getTestSuiteAt(index)
def testsuite = project.getTestSuiteByName("TestSuite Name")

Access the Test Case using the following commands
def testcase = testsuite.getTestCaseAt(index)
def testcase = testsuite.getTestCaseByName("TestCase Name")

Access the Test Steps using the following commands
def teststep = testcase.getTestStepAt(index)
def teststep = testcase.getTestStepByName("TestStep Name")

Run a Test Step using the following command
teststep.run(testRunner,context)

set property value at different level of hierarchy

set property at Project level
project.setPropertyValue("property_name","value") 


set property at TestSuite level
testsuite.setPropertyValue("property_name","value") 

set property at TestCase level
testcase.setPropertyValue("property_name","value") 

get property value from different level of hierarchy

get property from Project level
project.getPropertyValue("property_name")


get property from TestSuite level
testsuite.getPropertyValue("property_name")

get property from TestCase level
testcase.getPropertyValue("property_name") 


get the count of number of TestSuites, TestCases and TestSteps
project.testSuiteCount // get TestSuite count in the project
testsuite.testCaseCount // get TestCase count in the TestSuite
testcase.testStepCount // get TestSteps count in the TestCase
Any other basic commands which you feel should be added in the list please provide in the comment section.