Forum Discussion

harry21's avatar
harry21
Occasional Contributor
8 years ago

Groovy - Fail and Abort the test execution for 500 ISE and continue to run on 4x error codes

I am new to SOAPUI & Groovy.

 

I am trying to run from the TestSuite level.. I need to set a Script Assertion on each Test step to make sure the execution should be aborted if the service returns 500 error code. This groovy should continue to run in case we get 400, 404 etc and should not bother about these 4x error codes. i.e. only if we get 500 ISE on any step within the test case, then execution should not proceed.

 

I tried this below Groovy.

But it still proceeding the next steps and also I'm getting the message "No such property: testRunner for class"

 

 

import java.io.*;
import java.util.*;
import com.eviware.soapui.model.testsuite.*
import com.eviware.soapui.impl.wsdl.teststeps.assertions.*
import com.eviware.soapui.SoapUI
def alert = com.eviware.soapui.support.UISupport;
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def httpResponseHeaders = context.testCase.testSteps["Request"].testRequest.response.responseHeaders
def httpStatus = httpResponseHeaders["#status#"]
def httpStatusCode = (httpStatus =~ "[1-5]\\d\\d")[0]
if (httpStatusCode == "200")
{
testRunner.testCase.cancel()
}
else 
log.info("HTTP status code: " + httpStatusCode)

I even tried the below piece of code.

testRunner.testCase.fail()
testRunner.testCase.cancel()
testRunner.testCase.testSuite.fail()

 

Please help out with this

5 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Are you adding script assertion for each test step?

    As the error says, 'testRunner' is not available in Script Assertion.

    So, try Change from:
    testRunner.testCase.cancel()

    To
    context.testCase.cancel()
    • harry21's avatar
      harry21
      Occasional Contributor

      Yeah.. i tried that too .. I'm getting this message..

       

      No signature of method: com.eviware.soapui.impl.wsdl.WsdlTestCasePro.cancel() is applicable for argument types: () values: [] Possible solutions: any(), each(groovy.lang.Closure), any(groovy.lang.Closure), clone(java.lang.String, java.lang.String), collect(), collect(groovy.lang.Closure)

      • Radford's avatar
        Radford
        Super Contributor

        The WsdlTestCasePro class does not have the cancel and fail methods, these are on the WsdlTestCaseRunner (and the test suite and project runners).

         

        I'm not sure myself how (or if you can) access the runners from the script assertion. As an alternative, you could add a Groovy test step after your request step to interrogate the assertions and make the decision at that step whether to continue or not?

         

        You may also have to look at the test case options, In particular the two options:

         

        • Abort on Error
        • Fail Test Case on Error

        and possibly adjust these to suit your needs.

         

         

         

  • harry21's avatar
    harry21
    Occasional Contributor

    nmrao: the assertion script does not have access to the testRunner variable.. I read this statement somewhere in SmartBear blogs.. I still looking for a code piece to achieve my problem statement

  • zen_paul's avatar
    zen_paul
    Occasional Contributor

    Whilst this is not an answer to the question, it is an alternative approach to using script assertions.

    You might be able to create the Invalid Http Code assertion programatically with groovy

     

    This is how we are creating some assertions, adding them to the test step before the test step executes and letting SoapUI do the clever assertion stuff.

     

     

    def tStep = testRunner.getTestCase().getTestStepByName(stepName)
    
    def assertion
    // add assertions to the test step
    assertion = tStep.addAssertion("Contains")
    assertion.setName("hasCustomerAccountNumber")
    assertion.setToken("customerAccountNumber")

     

    You would have to get hold of the setters for creating the Invalid Http Code assertion, but I found this easier than writing scripts assertions or another test step groovy script to manipulate the response.