Forum Discussion

bdre's avatar
Occasional Contributor
7 years ago

recursion in Event afterRequest?

I've got a SoapUI project set up:



  request 1



  request 2


afterRequest script:

  check http status, if 401 then run

  testcase 2


When I run testcase1, the request in it sometimes returns 401, which means afterRequest script will run testcase2.  But, when this happens I can see testcase2 get executed (and received on the server), but execution never returns to the afterRequest event script.  The ReadyAPI UI shows that the tests are still running and they never complete.


Here's what the event script looks like:


def status = context.httpResponse.getStatusCode()
if (status != 401) {

def path = request.getPath()
if (path != <request 1>) {

def testSuite = context.getTestCase().getTestSuite()
def refreshTestCase = testSuite.getTestCaseByName("testcase2"), false)

// never get here and UI spins forever

Are there limitations on what can be done in an event script like this?  It seems like SoapUI is recursing, but I added checks to keep the script from executing testcase2 a second time, so not sure if that's what's going on.


6 Replies

  • Lucian's avatar
    Community Hero

    Hey there,


    What are you trying to achieve? 


    Is the testcase2 disabled by default or why doesn't it run anyways after testcase1?

    • bdre's avatar
      Occasional Contributor

      Yes, testcase2 is disabled.  I'm trying to run testcase2 only when the testcase1 request returns 401.


      • Lucian's avatar
        Community Hero



        This is how I done it and it seemed to work... The following is my project setup:



        You can see that I have a groovy script at the end of TestCase_1. This will check whether the response has a 401 status code and if so it will trigger the execution of TestCase_2:


        // Get status code header
        def statusCodeHeader = context.testCase.testSteps["Request_1"].testRequest.response.responseHeaders["#status#"]
        // Extract only the status code
        def statusCode = (statusCodeHeader =~ "[1-5]\\d\\d")[0]
        // If the status code is 401 then execute TestCase_2
        if (statusCode.equals("401")) { "yes"
        	def testSuite = context.getTestCase().getTestSuite()
        	def refreshTestCase = testSuite.getTestCaseByName("TestCase_2"), false)	

        Let me know if this worked!

  • nmrao's avatar
    Champion Level 3
    The best practise is that each test case should be independent.
    You may need to change design of your test in such a way that both requests in the same test if that is what you needed.
      • groovyguy's avatar
        Champion Level 1

        It looks like exactly what you stated, you've kind of entered into an infinite loop of recursion. You could try setting a context variable the first time the script runs, and have the script check that variable before running again.