This document discusses various ways to use variables and methods in Groovy scripts in soapUI to interact with and retrieve information from the test runner context, test cases, test steps, message exchanges, and XML responses and requests. It provides examples of using the log, context, testRunner, and messageExchange variables, as well as methods for retrieving test suites, test cases, test steps, and property values. It also demonstrates how to read and write XML using the GroovyUtils class, convert between strings and integers, and read the raw request and response contents.
1 of 2
Download to read offline
More Related Content
Soaoui
1. 1. Using Log variable
log.info(¡°Any Text message ¡±+anyVariable)
2. Using Context variable
defmyVar =context.expand( ¡®${#TestCase#SourceTestStep}¡¯) //will
expand TestCaseproperty valueinto the newvariable
context.testCase // returns thecurrent testCasehandle
3. Using TestRunner variable
testRunner.testCase.getTestStepByName(¡°TestStepName¡±)
testRunner.testCase // returnthehandleto currenttestCase
testRunner.testCase.testSuite.project.testSuites[¡°My_TestSuite¡±]
4. Using MessageExchange variable
messageExchange.getEndpoint() //endpointto theselected
teststep
messageExchange.getTimestamp() //timestamp
messageExchange.getTimeTaken() //time taken toprocess the
request/response
5. Using Project, TestSuite, TestCase, TestStepmethods
def project=testRunner.testCase.testSuite.project
def project=context.testCase.testSuite.project
def myTestSuite=project.getTestSuiteAt(IndexNumber)
def myTestSuite =project.getTestSuiteByName(¡°Nameof the
TestSuite¡±)
def myTestCase=myTestSuite.getTestCaseAt(IndexNumber)
def myTestCase=myTestSuite.getTestCaseByName(¡°Nameofthe
TestCase¡±)
def myTestStep=myTestCase.getTestStepAt(IndexNumber)
def myTestStep=myTestCase.getTestStepByName(¡°Name ofthe
TestStep¡±)
9. Reading & Writing user-defined property withGroovy
defuserIdInStep =testRunner.testCase.getTestStepByName(
¡°UserDefinedProperty¡±)
defuserIdStr =userIdInStep.getPropertyValue(
¡°myPropertyName¡±);
userIdInStep.setPropertyValue(¡°myPropertyName¡±,
¡°StringValueAsInput¡±)
7. groovyUtils& XmlHolder
def groovyUtils =new com.eviware.soapui.support.GroovyUtils(
context )
def holder =groovyUtils.getXmlHolder
(¡°Assert_Script#Response¡±)
8. Converting String into Integer & Integer intoString using groovy
anyStringVar =anyIntegerVar.toString()
anyIntegerVar=anyStringVar.toInteger()
6. RawRequest & RawResponse
messageExchange.getRequestContentAsXml.toString()
messageExchange.getResponseContentAsXml.toString()
Go Groovy with soapUI ¨C nifty tool for Webservice testing!
Posted: May 19, 2011 in context, groovy, soapUI, testrunner
Tags: context, groovy,testcase,testrunner,teststep
2
Here i go with first groovy post about using Groovy script in soapUI. groovy comes very handy
when you want to put some script assertion or performing certain task before/after each teststep
execution etc.
First couple of things to remember before we proceed with actual code lines.
# Groovy script can be written in Assertion, Test Step, TestCase/TestSuite level as
TearDown/Setup script.
# To invoke the groovy script at each different level, script editor provides us with the list of
2. variables on top right corner of script editor. So it is possible that the code you have written in
groovy test step or as teardown script would not work properly for teststep script assertion.
In most of the cases we use either ¡°context¡± or ¡°testrunner¡± variable to invoke or create a new
object of any class or to call required method.
I frequently use these 2 standard lines of groovy code, to acheive the required script objective,
are :
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder (java.lang.String xmlPropertyOrString) // where this
xmlProperty can have either ¡®teststepname#response¡¯ ¡®teststepname#request¡¯ or any string value.
After having the object created refer the groovyUtils APIDocs
(http://www.soapui.org/apidocs/com/eviware/soapui/support/GroovyUtils.html) to find out all
the relevant methods which can be called with particular input parameter.
Now below are few code lines for quickly performing certain task :
/*
@Author : Pradeep Bishnoi
@Description : To count the number of nodes under any node in response using xPath & groovy
script.
*/
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder (¡°myTestStepName#Response¡±)
holder.namespaces[¡°namespacename1¡å] = ¡°http://mywebservice.com/soapui/testing”
log.info(¡°Number of nodes :¡± +
holder.getNodeValue(¡°count(//namespacename1:GetSampleMethodResponse/namespacename1:
StudentDetails/namespacename1:Address)¡±))
// Address is comprised of house number, street name, city, country ¨C so result = 4
/*
@Description : To store the response into a simple text file, append this code to the above.
*/
new File(¡®D:/response_myTestStepName.txt¡¯).write(holder.prettyXml)
Do provide your inputs/comments/question on the same blog and share if you have something
interesting. Thanks!