Forum Discussion

britanneywiley's avatar
britanneywiley
New Contributor
3 months ago
Solved

Validate SAP API Responses Using ReadyAPI Assertions

Hi everyone, I’ve been working on API testing for an SAP Cloud integration and recently started using ReadyAPI for response validation. Most of the calls return as expected, but I’m facing issues wh...
  • scot1967's avatar
    3 months ago

    This is some VERY basic groovy I used with a TestComplete project.  I don't do much with APIs.  I hope it helps.  

    log.info ("**** Process Response ****")
    
    import groovy.json.JsonOutput
    import groovy.json.JsonSlurper
    
    // Specify the REST request step
    def requestStep = testRunner.testCase.testSteps["Rest Request"]
    
    // Retrieve the response
    def response = requestStep.getPropertyValue("Response")
    
    if (response) 
    {
        try 
        {
            // Parse the response as JSON
            def jsonResponse = new JsonSlurper().parseText(response)
            def jsonResponseString = JsonOutput.toJson(jsonResponse)
    
            // Log the formatted JSON response for TestComplete
            log.info("JSON Response: " + jsonResponseString)
        } 
        catch (Exception e) 
        {
            log.error("Failed to parse response as JSON: ${e.message}")
            log.info("Raw Response: " + response)
            // Stop the Test Case execution on error
            testRunner.fail("Failed to parse the response as JSON.")
        }
    } 
    else 
    {
        log.warn("The response is empty.")
    }