The first part is here: SoapUI and Groovy Scripting (first part)

Modify an item for obtain for example an Error from Response

import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def response = testRunner.testCase.getTestStepByName("/cart/{id}").getPropertyValue("response")
jsonSlurperResponse = new JsonSlurper().parseText(response)

if(jsonSlurperResponse.cartItems[1] != null) {
	def idDevice = jsonSlurperResponse.cartItems[1].id
	def request = '''
	   	"context":    {
	     	"cartid": null,
	      	"flowid": null,
	      	"id": "defaultSessionContextId",
	      	"userid": "pinscherTesting"
	} '''
	def jsonSlurperRequest = new JsonSlurper().parseText(request)
	jsonSlurperRequest.cartItems[0].cartItemId = idDevice
	def conv = JsonOutput.toJson(jsonSlurperRequest)
	def restRequest = testRunner.testCase.getTestStepByName('/cart/{id}/modify');

Insert a new item at the end of a json Response

def slurperNewDevice = new JsonSlurper().parseText(newDevice)
def slurperNewDeviceToKeep = new JsonSlurper().parseText(newDeviceToKeep)
def slurperResponse = new JsonSlurper().parseText(takeResponse)

slurperResponse << slurperNewDevice //add new device to list of device
slurperResponse << slurperNewDeviceToKeep
def map = ["devices":slurperResponse]
def deviceList  = JsonOutput.toJson(map)

def restRequest = testRunner.testCase.getTestStepByName("Create New Device")
restRequest.setPropertyValue('Request', deviceList)

Takes code from Response and set it like parameter in next Request

def String pinTmp = testRunner.testCase.getTestStepByName("/login").httpRequest.response.responseHeaders["pinscher-variable"]
def String[] pinSplit = pinTmp.split('\\[')
def String[] pinSplit2 = pinSplit[1].split('\\]')
def String pin = pinSplit2[0]

testRunner.testCase.testSteps["/verify"].setPropertyValue("pinscher-variable", pin)

Replace an element and send it

def String pinErrato = pin.replaceAll("J9fQ","X8hW")
testRunner.testCase.testSteps["/verify-wrong"].setPropertyValue("pinscher-variable", pinErrato)

Takes value of variables from Data Source (and DataSource Loop after Request)

import groovy.json.*
import java.lang.Object

def cvv = context.expand( '${DataSource#cvv}' )
def emailOwner = context.expand( '${DataSource#emailOwner}' )
def expirationDate = context.expand( '${DataSource#expirationDate}' )
def firstName = context.expand( '${DataSource#firstName}' )
def lastName = context.expand( '${DataSource#lastName}' )
def pan = context.expand( '${DataSource#pan}' )

slurperNewCreditCard.creditCardData.cvv = cvv
slurperNewCreditCard.creditCardData.emailOwner = emailOwner
slurperNewCreditCard.creditCardData.expirationDate = expirationDate.toLong()
slurperNewCreditCard.creditCardData.firstName = firstName
slurperNewCreditCard.creditCardData.lastName = lastName
slurperNewCreditCard.creditCardData.pan = pan

def jsonObject = JsonOutput.toJson(slurperNewCreditCard)

def restRequest = testRunner.testCase.getTestStepByName("POST")
restRequest.setPropertyValue("Request", jsonObject)

Function get time

def timestamp = new java.util.Date().getTime()

Replace function and conversion to BigDecimal


Conversion to Integer


Create a map – A map is a collection of (key, value) pairs

def map = ["id":null, "products":slurperPinscherProducts]
def listPinscherProducts  = JsonOutput.toJson(map)

Forced Assert True and False

if ( buyerName_beforePatch != buyerName_afterPatch ) { "Different values -> test OK."
	assert true
} else { "Same values -> test KO."
	assert false

Conversions date to timestamp

def date = new Date()
lastUpdateBefore_value = date.parse('dd/MM/yyyy mm:ss', '04/10/2016 02:00') "date read: ${lastUpdateBefore_value}" )
def lastUpdateBefore_valueTimestamp = lastUpdateBefore_value.time.toString() "timestamp: ${lastUpdateBefore_valueTimestamp}" )

Checks values in Response

import groovy.json.JsonSlurper

def values = context.expand( '${Request the list of items of the catalogue#Response#$[\'values\']}' )
def slurperProducts = new JsonSlurper().parseText(products)
def Integer i
def String[] productTypeArray = ["",""]

Integer values_number = slurperValues.size()

for(i=0; i<values_number; i++){
	def valueType = context.expand( '${Request the list of items of the catalogue#Response#$[\'values\']['+i+'][\'valueType\']}' )
	valueTypeArray[i] = valueType[i])
	if(valueTypeArray[i]!="pinscher" && valueTypeArray[i]!="testing"){
		assert false
		assert true


share me!:))Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone
Print this page