I came across a situation where I had to use groovy scripts in to test some REST endpoints that return JSON responses.
So following is a groovy code snippet which does the following:
1. Import the json parsing library
2. Fetch the JSON response from a test step
3. Parse the JSON response
4. Access properties in the JSON response
So following is a groovy code snippet which does the following:
1. Import the json parsing library
2. Fetch the JSON response from a test step
3. Parse the JSON response
4. Access properties in the JSON response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import library to parse JSON | |
import groovy.json.JsonSlurper; | |
//get the JSON response from the test step that makes the call to some test step to get a JSON repsonse | |
def response = testRunner.testCase.getTestStepByName("TestStepName").getPropertyValue("response"); | |
//parse the json response | |
def responseJSON = new JsonSlurper().parseText(response); | |
//access the propertyname | |
def property = responseJSON.propertyname; |
No comments:
Post a Comment