SoapUI Groovy has built-in variables that can be used to access the SoapUI object model programmatically. For each location inside the Project structure, the SoapUI Groovy console will advise which built-in variables are accessible from that location.
log // use to print things to console
context // holds data about the current TestSuite
testRunner // provides a reference to the project, test suite or test case object
messageExchange // for scripted Assertions only. Captures the TestStep results
SoapUI Object Model Documentation
There are 3 ways that Groovy can be used in SoapUI --
- Groovy TestStep
- Script Assertion
- Setup | Teardown (for Project, TestSuite or TestCase)
Groovy TestSteps are a blank slate and can be scripted to perform any task offered by regular SoapUI TestSteps in the gui.
The most common usages for Groovy TestSteps in SoapUI Open Source are
- Read data from a file (in ReadyAPI, this is the
DataSourceTestStep) - Write data to a file (in ReadyAPI, this is the
DataSinkTestStep) - Loop through a file and use each row of data to run a TestCase (
DataLooperin ReadyAPI) - Perform custom transformations between requests
- connect to databases
- Transfer data between different SoapUI components
Groovy TestSteps can also be used for utility tasks, such as creating a report from the previous test run. In this case, the TestSteps would be placed into a "Library" TestSuite and called as part of the teardown process for an active TestSuite.
- An example of this kind of usage can be see in this Dzone.com article. A mirror reference is here
Sample - Create DataSource & DataLoop with Groovy
Add external jar to SoapUI classpath
Generate random usernames for TestCase Properties
- Using built-in Log variable
log.info(“Any Text message ” + anyVariable)
- Using built-in Context variable
def myVar = context.expand( ‘${#TestCase#SourceTestStep}’) //will expand TestCase property value into the new variable
context.testCase // returns the current testCase system handle
// ex. Fri May 31 17:45:11 PDT 2024:INFO:com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase@b864de4
- Using built-in TestRunner variable
testRunner.testCase.getTestStepByName(“TestStepName”)
testRunner.testCase // return the handle to current testCase
testRunner.testCase.testSuite.project.testSuites[“My_TestSuite”]
- Using built-in MessageExchange variable (for assertions only)
messageExchange.getEndpoint() //endpoint to the selected teststep
messageExchange.getTimestamp() //timestamp
messageExchange.getTimeTaken() //time taken to process the request/response
- Using Project, TestSuite, TestCase, TestStep methods
- Access to lower level object (such as test steps) must be done through the hierarchy
- project > test suite > test case > test step
def project = testRunner.testCase.testSuite.project
def project = context.testCase.testSuite.project
def myTestSuite = project.getTestSuiteAt(IndexNumber)
def myTestSuite = project.getTestSuiteByName(“Name of the TestSuite”)
def myTestCase = myTestSuite.getTestCaseAt(IndexNumber)
def myTestCase = myTestSuite.getTestCaseByName(“Name of the TestCase”)
def myTestStep = myTestCase.getTestStepAt(IndexNumber)
def myTestStep = myTestCase.getTestStepByName(“Name of the TestStep”)
-
RawRequest & RawResponse
messageExchange.getRequestContentAsXml.toString() messageExchange.getResponseContentAsXml.toString() -
groovyUtils & XmlHolder
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder ("Assert_Script#Response")
- Converting String into Integer & Integer into String using groovy
anyStringVar = anyIntegerVar.toString()
anyIntegerVar = anyStringVar.toInteger()
- Reading & Writing custom property with Groovy
getis a normal read onlysetis like an upsert. If the Property did not previously exist, it will create a new one on the fly
def userIdInStep = testRunner.testCase.getTestStepByName( “UserDefinedProperty” )
def userIdStr = userIdInStep.getPropertyValue( “myPropertyName” );
// the `set` method will create and assign value if the Property did not already exist
userIdInStep.setPropertyValue(“newPropertyName”, “StringValueAsInput”)
10.Get or set Properties at any level
- as mentioned above,
setwill also create a property if it did not already exist
// Get a test case property
def testCaseProperty = testRunner.testCase.getPropertyValue("MyProp")
// Get a test suite property
def testSuiteProperty = testRunner.testCase.testSuite.getPropertyValue( "MyProp" )
// Get a project property
def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue( "MyProp" )
// Get a global property
def globalProperty = com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "MyProp" )
// Set a test case property
testRunner.testCase.setPropertyValue( "MyProp", someValue )
// Set a test suite property
testRunner.testCase.testSuite.setPropertyValue( "MyProp", someValue )
// Set a project property
testRunner.testCase.testSuite.project.setPropertyValue( "MyProp", someValue )
// Set a global property
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue( "MyProp", someValue )
- Configure TestStep request Endpoint
testRunner.testCase.getTestStepByName("dd").getHttpRequest().setEndpoint("http://cd-diudara:8280/services/linkedinFollowCompanyPage?wsdl");
def end = testRunner.testCase.getTestStepByName("dd").getHttpRequest().getEndpoint();
log.info end;
- Generate unique Username each time a Request is sent
a. Create a TestStep Property named Username.
b. Set Username Property value to a Property Expansion containing a Groovy random number function. Loginn${=String.valueOf(Math.random()).substring( 0, 5 )}
c. Use the Property Expansion in the Request Payload for the <username> </username> element
The following code demonstrates how you can run a test step from another project of your choice using the built-in scripting objects of SoapUI:
// Replace the project, test suite, case and step names with those you need
// Connect to the test step in another project def prj = testRunner.testCase.testSuite.project.workspace.getProjectByName("ProjectName") tCase = prj.testSuites['TestSuiteName'].testCases['TestCaseName'] tStep = tCase.getTestStepByName("TestStepName")
// Call the test runner and check if it can run the specified step def runner = tStep.run(testRunner, context) log.info ("runner status ....... : " + runner.hasResponse())
SoapUI Set up and Teardown - Docs
Get Assertions from Test run - for example, capture results from all failed test assertions
See SoapUI Docs, Section 9
Load Properties from File to TestCase
nmrao - a SoapUI OSS Project Champion