Forum Discussion

bdre's avatar
bdre
Occasional Contributor
6 years ago

recursion in Event afterRequest?

I've got a SoapUI project set up:

 

testcase1:

  request 1

 

testcase2:

  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) {
    return
}

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

def testSuite = context.getTestCase().getTestSuite()
def refreshTestCase = testSuite.getTestCaseByName("testcase2")
refreshTestCase.run(null, 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
    Lucian
    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
      bdre
      Occasional Contributor

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

       

      • Lucian's avatar
        Lucian
        Community Hero

        Hi,

         

        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")) {
        	log.info "yes"
        	def testSuite = context.getTestCase().getTestSuite()
        	def refreshTestCase = testSuite.getTestCaseByName("TestCase_2")
        	refreshTestCase.run(null, false)	
        }

        Let me know if this worked!

  • nmrao's avatar
    nmrao
    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
        groovyguy
        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.