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"
		},
		"cartItems":
		[
			{"cartItemId":"","quantity":"6"}
		]
	} '''
	
	def jsonSlurperRequest = new JsonSlurper().parseText(request)
	jsonSlurperRequest.cartItems[0].cartItemId = idDevice
	def conv = JsonOutput.toJson(jsonSlurperRequest)
	def restRequest = testRunner.testCase.getTestStepByName('/cart/{id}/modify');
	restRequest.setPropertyValue('Request',conv);
} 

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)
log.info(jsonObject)

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

Function get time

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

Replace function and conversion to BigDecimal

"price":priceDevice[i].replace(",",".").toBigDecimal()

Conversion to Integer

"quantity":quantityDevice[i].toInteger()

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 ) {
	log.info "Different values -> test OK."
	assert true
} else {
	log.info "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')
log.info( "date read: ${lastUpdateBefore_value}" )
def lastUpdateBefore_valueTimestamp = lastUpdateBefore_value.time.toString()
log.info( "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()
log.info(values_number)

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
	log.info(valueTypeArray[i])
	if(valueTypeArray[i]!="pinscher" && valueTypeArray[i]!="testing"){
		assert false
	}else{
		assert true
	}	
}

 

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