How to Use Properties in SoapUI Groovy Script – SoapUI Tutorial #7

Properties are the central repository to store our information temporarily. These can contain login information like username and password, session data like session id, page context, header information and so on.

This is the 7th tutorial in our SoapUI free online training series.

Let’s see how to add property test step and then we will discuss assigning values to the property and show them in the log.

How to Add Properties in SoapUI:

Properties in SoapUI

Here are the steps.

  • Right-click on the Test steps node
  • Click Add Step and Properties option from the context menu
  • Enter the property name as desired and click OK
  • In the properties screen, click  properties with groovy Script(1)  icon to add property
  • Enter your desired property name and click OK button. For example, let me enter Pro_Response
  • Type any default value for the property if you wish. For example, I enter “Nothing”
  • Then, add a Groovy Script test step next to the property step. Refer below screenshot.

properties with groovy Script(2)

We can transfer the property data across the test steps during the test execution. For that, SoapUI Pro provides Property Transfer test step. Look at the below screenshot.

properties with groovy Script(3)

In the groovy script, add the following script. This script will assign a string text to the property and then it will show in the log after executing the test case.

String testString = "TestString"
testRunner.testCase.setPropertyValue( "Pro_Response", testString )
def getLocalPropValue = testRunner.testCase.getPropertyValue("Pro_Response")
log.info(getLocalPropValue)
  • Once written the above script in the editor, double-click on the test case name step.
  • Run the test case by clicking on the iconproperties with groovy Script and the see the results in the script log tab.

Accessing property:

There are several ways to access test case, test suite and project properties for setting and getting their data through the script. Here are the samples for retrieving the property data.

def getTestCasePropertyValue =
testRunner.testCase.getPropertyValue( "LocalPropertyName" )
def getTestSuitePropertyValue =
testRunner.testCase.testSuite.getPropertyValue
( " LocalPropertyName " )
def getProjectPropertyValue =
testRunner.testCase.testSuite.project.getPropertyValue
( " LocalPropertyName " )

In order to access a global property, this is the script:

def getGlobalPropertyValue =
com.eviware.soapui.SoapUI.globalProperties.getPropertyValue
( "GlobalPropertyName" )

These script lines are used to set the value to the local and global property.

testRunner.testCase.setPropertyValue( " LocalPropertyName ", someValue )
testRunner.testCase.testSuite.setPropertyValue( " LocalPropertyName ", someValue )
testRunner.testCase.testSuite.project.setPropertyValue( " LocalPropertyName ", someValue )
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue
( " GlobalPropertyName ", someValue )

Here in these scripts, testRunner is the common object which might be test suites, test cases or project. setPropertyValue and getPropertyValue are the methods or functions.

As we mentioned the above script, we can assign data to the properties.

testRunner.testCase.testSteps[“Properties”].setPropertyValue( “Pro_Response”, testString )

After executing the above script, the property will get updated in the property test step. Refer the following screenshot.

properties with groovy Script(4)


Receiving response data:

Now let us discuss how to get the response data through the script. To do this,

  • Execute the service request once and verify the result
  • Go to Groovy script editor and then right click on the editor as shown in the below screenshot

properties with groovy Script(5)

Now SoapUI Pro generates the script as below after specifying the property name.

def response = context.expand( ‘${ServiceRequest#Response}’ )

As we know, “def” is a groovy script keyword that represents defining properties/objects. By default, SoapUI Pro has the property name as “response” in the Get Property popup. If we want we can change this name. Remaining portions of the script are auto-generated.

Let us merge the above script in our earlier discussed script. Here’s what you would see:

def response = context.expand( '${ServiceRequest#Response}' )
testRunner.testCase.setPropertyValue( "Pro_Response", response )
def getLocalPropValue = testRunner.testCase.getPropertyValue("Pro_Response")
log.info(getLocalPropValue)

If we execute the above script separately, it will log the entire response data in the log section. Even when execute this along with the test case, it will show the same output in the script log.

Creating properties from the navigator pane:

There is another way to create properties locally through the property panel which will appear when we click on the nodes under the project tree. Let’s see how:

  • Add currency converter service request and a groovy script test step under the test suite ConversionTestSuite.
  • Click on the TestSuite name under the project (i.e. ConversionTestSuite)
  • At the bottom of the Navigation panel, we can see a Property panel. It contains TestSuite Properties and Custom Properties tabs.
  • Go to Custom Properties tab by clicking on it
  • Then click on the plus ( + ) icon to add the property as shown below:

properties with groovy Script(6)

  • Enter property name and provide default input value as shown in the above screenshot.
  • Now execute the currency converter service request once. Only then we can get the property information when right-clicking on the editor.
  • Enter the following script in the editor

 def getPropValue = context.testCase.NetSuite.getPropertyValue(“FromCurrencyValue”)

  • Click on the Run icon

This script gets the property value and assigns to the variable “getProValue”. To print the value of the property, we can write the following script :

Log.info (getPropValue);

Global Properties:

Now let us discuss global properties. These properties are defined in one place and we can access them across the project components like test suite, test case, test steps etc.

Here are the scripts for writing data to the global properties.

com.eviware.soapui.SoapUI.globalProperties.setPropertyValue
( "prjFromCurrency", "USD" )
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue
( "prjToCurrency", "INR" )

Once we execute the above test step script, the mentioned properties will be created and the respective values will be assigned to those properties. Let us see how we can verify it.

  • Click on the File menu
  • Then, choose Preferences option
  • In the left side, click on the Global Properties tab.
  • Verify the properties in the property sheet on the right side. Refer the screenshot below:

properties with groovy Script(7)

Conclusion:

Properties are helpful for transferring the data between the test steps such as test suites, test steps and test cases. Property can be defined through a groovy script. We can also assign and retrieve data of the properties through the script. And, just like other test steps we can rename or delete or disable the property test step by right click and then choose the respective options from the context menu.

In the next tutorial, we will learn more features about properties like passing properties in the input request and retrieve from global properties, property transfer test step and so on.

Please stay with us and let us know in case of any questions or comments.




Recommended reading

16 comments ↓

#1 Sushma s

Hi
Thanks for sharing.
Can you explain how to randomize datasource?

#2 Karunagara Pandi

@Sushma: Thanks for reading..

Randomizing the data source will be discussed in the data driven testing article.

#3 Gaurav Khurana

this was a great tutorial and an important step to pass on data between various componeneys in a test case
Thanks for sharing it

1) Some problem which can be corrected is :- Space should not be there below
testRunner.testCase.testSuite.getPropertyValue( ” LocalPropertyName ” )

2) Can’t we see properties in a table like we see custom properties,, I was not able to see properties defined inside groovy scipt in the form of table in navigation bar on left side

Keep up the good work.

#4 bob lichtenfels

thank you for the write up.

#5 hemantvarhekar

Hi,

I tried this sample where Upon execution of below groovi scripts:

String testString = “TestString”
testRunner.testCase.setPropertyValue( “Pro_Response”, testString )
def getLocalPropValue = testRunner.testCase.getPropertyValue(“Pro_Response”)
log.info(getLocalPropValue)

I was expecting the value of Pro_Response should be set to TestString but this is not happening.

can you suggest

#6 Ameet

in “receiving response data” section on right click am not getting get data option. am using soapui5.2.1

plz revert on the above

#7 July

Hi all.
The “Error establishing a database connection” database error message on the white screen is displayed when opening this page
http://www.softwaretestinghelp.com/soapui-tutorial-8-working-with-soapui-properties/

#8 Vijay

Hi
Thanks for reporting it. It may be a temporary refresh. Can you please try and let us know if the issue still persist? You can try other pages as well.

#9 July

Hi
Thank you Vijay
The “8 soap tutorial” page is successfully opened.

#10 john.tran

Cool one! Thanks you very much!

#11 Dipesh Udhlani

runner.testSuite.testCaseList.each(){
for (testStep in it.testStepList)
testStep.setPropertyValue(“Username”, “dpshud1”)
}

#12 Dipesh Udhlani

How to get METHOD from a testStep/testCase/testSuite, basically from any one.

I am interested to access only GET APIs/testSteps among all type of APIs.
Thanks!

#13 Komal Diwan

Hello
This tutorial is very helpful…:) Thanku so much
But i am getting an error : unexpected char: ‘#’ @ line 1

Can you please resolve this?

#14 Dipesh Udhlani

Got solution for my query as below
String sMethod = (testStep.getTestRequest().getMethod())

#15 Elle

When first adding the property transfer, there is no screen shot of how this is set up. I have attempted to insert the correct source & target details but when I execute that, the Groovy Script is overwritten by the property value. Can anyone help fix this?

#16 Yaswanth

What is the difference between context and testRunner. where we use these. In which case we use context and testRunner. I ran the below script. The output is same .

def getPropValue = testRunner.testCase.testSuite.getPropertyValue(“suite”)
log.info “using test tunner “+getPropValue

def getPropValue1=context.testCase.testSuite.getPropertyValue(“suite”)
log.info “using context”+getPropValue1

Leave a Comment