Forum Discussion

Sandyapitester's avatar
Sandyapitester
Frequent Contributor
7 years ago

soapui/ Soapui pro - Need to execute the teststep based on API response code

Hi All,

 

Need to execute the API test cases based on API response code.

 

Ex: while executing the teststep need to validate the response code, based on the api response code need to execute the another testcases

 

It's possible to execute? if yes share your valuable input

6 Replies

  • sanj's avatar
    sanj
    Super Contributor

    I will see if I can a find a groovy example

    This may be doable using groovy not sure yet

    • Sandyapitester's avatar
      Sandyapitester
      Frequent Contributor

      Hi San,

       

      If u share the script it would be more helpful for me

  • nmrao's avatar
    nmrao
    Champion Level 3
    The best practise is to have test cases independent of each other.
    • Sandyapitester's avatar
      Sandyapitester
      Frequent Contributor

      Hi,

       

      All test cases are depended based on the teststep status code need to run another test cases if any test cases are failed there is no point to add the another testcases.

       

      that's the reason am looking for this solution.

      • Bill_In_Irvine's avatar
        Bill_In_Irvine
        Contributor

        I think using Groovy is the likely way to the solution.

         

        I don't have a need to call a request based on the response of a previous request however I can only give some
        guide on how I go about it and you can finish the details (because working with Groovy is fun!).

         

        The basics of running test cases via Groovy  are covered here:

        https://support.smartbear.com/readyapi/docs/testing/scripts/samples/cross-project.html

         

        I suppose you can fullfill your goal in a Setup script at the Project level, test suite level or test case level.
        I'd go about it by first setting up my test steps, and then get to work with Groovy.

        You can call a test step based on the information in the above URL. Once you executed that step, you can
        get the response. Here is how I get the response, but note I did this in a Groovy Test Step within a test case:

         

        import groovy.json.JsonSlurper
        
        def previousTestStep = testRunner.getTestCase().getTestStepByName("request1")
        String propertySpec = '${' + previousTestStep.name + '#Response}'
        def response = context.expand(propertySpec)
        def jsonSlurper = new JsonSlurper().parseText(response)
        
        
        // Now suppose your server's response has a name field called status   which has a value "Success" or "Fail"
        def status =  jsonSlurper.status
        
        
        if( jsonSlurper.status == "Success")
        {
            // call request2
        }
        else
        {
            // call request3
        }

        You can stop the IDE from executing the steps again with testRunner.cancel()

         

        testRunner.cancel("done")

        'again, I have not called other requests based on responses as so far I had no need but I hope this helps to give you a guide.