Generate Random Identifier using Groovy Script
In this part of “Responses and parsing in Ready API,” we will see Groovy Script to generate values with the Universally Unique Identifier (UUID).
The below given an example explains how to generate a UUID:-
// Generating random variable value
def var = UUID.randomUUID()
// Logging the variable
log.info var
To generate the UUID in the string, we will use the below code:-
// Generate a random item identifier
log.info "Item: ${java.util.UUID.randomUUID()}"
Using JDBC Driver to connect Database
In this part of "Responses and parsing in Ready API," we will see how to register the JDBC driver and connect to the Database using Groovy Script.
The below given example explains how to register the driver and connect to the Database using Groovy Script:-
// Import the Groovy class required to work with SQL databases
import groovy.sql.Sql
// Setting up the database connection properties
def url = 'jdbc:mysql://mydatabase.com/DatabaseVariable' /* IMPORTANT: must start with jdbc:mysql:// */
def user = 'DatabaseUser'
def password = 'DatabasePassword'
def driver = 'com.mysql.jdbc.Driver'
// Register the MySQL JDBC driver – required for Groovy to send requests to the database
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver( driver )
// Connecting to the SQL instance
def sql = Sql.newInstance(url, user, password, driver)
// Use the SQL instance
// ...
// Close the SQL instance
sql.close()
Log Results using Ready API
In this part of “Responses and parsing in Ready API,” we will see how to log your test result in Ready API and extract details about test steps that satisfy specific requirements, etc.
The below given an example explains how to save the results of every test step:-
// Replace the path according to need
// Make sure the specified folder exists
filePath = 'c:/work/test-results/'
// Create output files
fos = new FileOutputStream( filePath + testStepResult.testStep.label + '.txt', true )
// Fill output files
pw = new PrintWriter( fos )
testStepResult.writeTo( pw )
// Close the output
pw.close()
fos.close()
The below given an example explains how to save the results of every test step in failed test cases:-
for ( testCaseResult in runner.results )
{
// Get all test cases’ names from the suite
testCaseName = testCaseResult.getTestCase().name
log.info testCaseName
// Check whether the case has failed
if ( testCaseResult.getStatus().toString() == 'FAIL' )
{
// Log failed cases and test steps’ resulting messages
log.info "$testCaseName has failed"
for ( testStepResult in testCaseResult.getResults() )
{
testStepResult.messages.each() { msg -> log.info msg }
}
}
}
Modifying Responses using Groovy Script
In this part of “Responses and parsing in Ready API,” we will use groovy scripts to perform various operations on responses, such as changing their content or logging the outcomes.
The below given an example explains how to modify a specific value across all response messages with the RequestFilter.afterRequest event handler:-
if( request.response == null )
return
// Get the response content.
def content = context.httpResponse.responseContent
// Modify the content – replacing all 555 with 444.
content = content.replaceAll( "555", "444" )
// Override the response.
context.httpResponse.responseContent = content
Parsing JSON and XML using Groovy Scripts
In this part of “Responses and parsing in Ready API,” we will use Groovy Scripts to parse JSON and XML in your requests and response.
Let's use an example where we have XML content in the request and JSON content in the response and want to verify that some values are equal.
{
"info" : {
"buildingStyle" : "MBH.1990",
"owner" : "MBH.1990",
"checkIn" : true,
"checkOut" : true
}
}
Groovy Scripts to check whether specific values are equal or not is:-
//Import packages that contain libraries needed to parse JSON and XML
import groovy.json.*
import groovy.util.*
//Parse JSON content in the request
def request = context.expand( '${REST Request#Request}' )
def parsedJson = new JsonSlurper().parseText(request)
//Parse XML content in the response
def response = context.expand( '${REST Request#Response}' )
def parsedXml = new XmlSlurper().parseText(response)
//Assert values in the request against values in the response
assert parsedJson.info.buildingStyle.toString() == parsedXml.BuildingStyle.text()
assert parsedJson.info.owner.toString() == parsedXml.Owner.text()
assert parsedJson.info.checkIn.toString() == parsedXml.CheckIn.text()
assert parsedJson.info.checkOut.toString() == parsedXml.CheckOut.text()
Frequently Asked Questions
What types of reports is ReadyAPI able to provide?
ReadyAPI provides a range of pre-built reports and reporting templates in order to assist you in delivering the information that is most important to you. ReadyAPI offers a variety of export formats in addition to visual representations so that you can save or transfer your continuous integration findings into another system for analysis in the future.
What is the use of Ready API?
Agile and DevOps software teams can improve API quality by using ReadyAPI, which gives teams the ability to create, manage, and carry out automated functional, security, and performance tests through a single, centralized interface.
What makes a ready API different from a REST API?
REST-assured does not require licensing fees, whereas ReadyAPI! offers much more native performance. Any REST API can be tested if that's all you're doing.
Conclusion
Congratulations on finishing the blog! We have studied Responses and parsing in Ready API. We further looked at the header, generating identifiers, JDBC connection, Log results, Modifying Response, and parsing JSON and XML.
We sincerely hope that this blog has improved your understanding of Responses and parsing in Ready API, and if you want to learn more, then you can check articles on:-
Please refer to our guided pathways on Code studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.
Please do upvote our blogs if you find them helpful and informative!
Happy learning!