In the previous blog post here and here, we saw how to create a functional test case and add assertions to validate the result. ReadyAPI provides many inbuilt assertion methods which will help to easily validate the output response without any coding. However, in real-world usage for test automation, it may not be enough. Consider the scenario where we need to validate the response content has proper values from an expected list. In this example, let us make an assertion to validate, that the status field in the response for making the order is either placed or notplaced.
1 2 3 4 5 6 7
Script assertion window provides access to some default objects like log, context and message exchange. They contain information about the request and response made. Details of available methods can be found in Javadoc defined at here or in here
In the first line, we are reading the response of the current step by using the getResponse() method of messageExchange object. Once we have a string, which is in JSON format, we can use JsonSlurper to parse it into an object. JSON slurper parses text or reader content into a data structure of lists and maps Once we have an object, we can easily assert whether the value belongs to the expected list
We can directly run the scripts from this editor and see output logs
We can expand above assertion to do additional validations. If there is a need to check response from current test step against previous steps, we can make use of context object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
In the above code, we are trying to compare the output of the current response with the response from the previous step. This can be achieved through Context objects. From the context object, get details of the test case object. Once we have access to the test case object, then it is easier to expand to the step list We can easily identify previous steps with a specific name and then extract its response. Parse it again to an object and then make an assertion