I'm using SoapUI open source for REST testing, so I need to do things the hard way via Groovy (no JSON property transfer). I have a test step that places a call to /workspaces, creating a new workspace object and returns an id for said object in the response under data[0].id. I'm using JsonSlurper to parse the response and can easily run asserts on the parsed response. Where I'm having trouble, is figuring out how to set a test case property so that I can pass the id into the endpoint of the next response in the test case, a PUT to /workspaces/{workspace.id}. Here's what I have so far in a Groovy script test step between the two:
import groovy.json.JsonSlurper def responseJson = testRunner.testCase.getTestStepByName("Create Workspace").getPropertyValue("response") def slurper = new JsonSlurper().parseText(responseJson) def testCaseProperty = testRunner.testCase.getPropertyValue("workspace_id") testRunner.testCase.setPropertyValue("workspace_id", slurper.data[0].id)
This returns the following error message:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer error at line: 7
A little help please?
Solved! Go to Solution.
Please use below script assertion to the REST Request Test step( that means remove the additional groovy script test step)
/** * Below is the script assertion for the REST Request step * which extracts the data id and set it test case property **/ assert context.response, 'Response is empty or null' def json = new groovy.json.JsonSlurper().parseText(context.response) //Change the property name as needed log.info json.data.id context.testCase.setPropertyValue('ID', json.data.id.toString())
I changed the last line to:
testRunner.testCase.setPropertyValue("workspace_id", slurper.data[0].id as String)
But am still receiving the same error.
{ "status": 200, "code": 2000, "totalCount": 1, "limit": 20, "offset": 0, "data": { "id": 7002, "name": "Test Workspace", "isDisabled": false, "tags": null, "description": "Test Workspace Description", "parsedDescription": { "text": "Test Workspace Description" }, "created": "2019-01-31T18:48:31+0000", "modified": "2019-01-31T18:48:31+0000", "settings": { "logoUrl": null, "setupGuide": {
"text": { "intro": null, "settings": null, "forms": null, "members": null, "reports": null }
} }, "metadata": { "lastActivity": null, "storageUsed": "0.0000" } } }
Please use below script assertion to the REST Request Test step( that means remove the additional groovy script test step)
/** * Below is the script assertion for the REST Request step * which extracts the data id and set it test case property **/ assert context.response, 'Response is empty or null' def json = new groovy.json.JsonSlurper().parseText(context.response) //Change the property name as needed log.info json.data.id context.testCase.setPropertyValue('ID', json.data.id.toString())
Thank you! This did the trick!
Subject | Author | Latest Post |
---|---|---|