I have the following test steps:
GetDetails outputs a JSON object as follows:
{
"databaseId": 123,
"databaseName": "Test",
"address": "ON",
"details": {
"detail_id": 999,
"userId": 2,
"date": null,
"state": "active"
},
"itemName": "Bob details",
}
transferObject transfers this details
object to ChangeDetails test step.
But now I want to modify the object (change the state
property to non-active
) before feeding it to ChangeDetails test case.
How can I do that? any suggestions?
I am not really sure how to achieve this using property transfer step as it appears to be data manipulation.
If it would be achieved, I would do this in the below fashion(Using script assertion) .
Have only two steps
Add a Script Assertion
with the below code for Get Details step:
import groovy.json.*
//Read the response of GetDetails and filter details
def details = new JsonSlurper().parseText(context.response).details
//assert there is details available and not empty
assert details, "Details is empty or null in the response"
//Creating object to build the next step request
def json = new JsonBuilder()
//Building details object for Change
json.details {
//looping thru each data
details.each { key, value ->
//Change state to inactive
if ('state' == key) value = 'non-active'
//add the properties inside details
"$key"("$value")
}
}
//Create a pretty print sting and this is going to be the next test step's request
def prettyJson = JsonOutput.prettyPrint(json.toString())
//Assign this data to a test step custom property, say REQUEST
context.testCase.setPropertyValue('REQUEST', prettyJson)
In the Change Details step, open request editor => have ${#TestCase#REQUEST}
Now run your test see if it is working as you needed.
Note: It is mentioned in the comment inactive
, but it is mentioned in the question to be non-active
- so kept the same in the reply. I think it is not really big deal in this case, I believe.