cancel
Showing results for 
Search instead for 
Did you mean: 

How to Get and Set Properties during script assertions?

Regular Visitor

How to Get and Set Properties during script assertions?

Hello all,

 

I am looking for some help in order to get and set property values during a script assertion. I need to use them as part of a comparison during later steps to validate data is not changing throughout the test. The reason I am being forced to do this is that my JSON response seems to require the following import in order to parse the response:

 

import groovy.json.JsonSlurper

 

As I am sure many of you know, this is not supported during Groovy script steps so I am kind of stuck with where to go next. I will include an exampe of the JSON response below along with what I have been trying to use so far. The assertion passes, but data is not being saved as part of the ".setPropertyValue" line of code. You will also notice that I have two different possible ways of saving the data currently included. Neither of which seem to be working as expected.

 

Thanks,

Dave

 

 

JSON Response

 

{
"items": [ {
"price": 40,
"total": 40,
"consumerTotal": 40,
"discounts": [],
"productId": 1250,
"addons": {"phonenumber": "6149995555"}
}],
"discountSum": 0,
"grandTotal": 40,
"consumerDiscountSum": 0,
"consumerGrandTotal": 40,
"orderType": "airtime"
}

 

 

Assertion Code

 

import groovy.json.JsonSlurper

 

def ResponseMessage = messageExchange.response.responseContent
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)

 

def price = jsonSlurper.items[0].price
def total = jsonSlurper.items[0].total
def consumerTotal = jsonSlurper.items[0].consumerTotal
//def discount = jsonSlurper.items[0].discounts[0].amount
def discountSum = jsonSlurper.discountSum
def grandTotal = jsonSlurper.grandTotal
def consumerDiscountSum = jsonSlurper.consumerDiscountSum
def consumerGrandTotal = jsonSlurper.consumerGrandTotal

 

messageExchange.modelItem.testStep.testCase.getTestStepByName("Estimate").setPropertyValue("EstimateItem1Price", "price")
def EstimateItem1Price = price


log.info (EstimateItem1Price)
log.info (price)


assert grandTotal == price
assert consumerGrandTotal == price

 

assert discountSum == 0
assert grandTotal == total
assert consumerDiscountSum == 0

 

assert price - discountSum == total
assert grandTotal == price - discountSum
assert consumerGrandTotal == price - consumerDiscountSum
assert consumerGrandTotal == grandTotal

 

 

assert context.response, 'Response is empty or null'

def json = new groovy.json.JsonSlurper().parseText(context.response)

log.info (json.items.price)
context.testCase.setPropertyValue('EstimateItem1Price', json.items.price.toString())

1 REPLY 1
Highlighted
Super Contributor

Re: How to Get and Set Properties during script assertions?

Since 'price' is variable declared here. You cannot pass it as String. Please change the code to :-

messageExchange.modelItem.testStep.testCase.getTestStepByName("Estimate").setPropertyValue("EstimateItem1Price", price)

If it doesn't work. Please try this way also:-

testRunner.testCase.getTestStepByName("Estimate").setPropertyValue("EstimateItem1Price", price)

And also, please change price variable to price1 or anything else. You already have price as jsonObject in your Json.