0

If the soap request has a failure means Status != "HTTP/1.1 200 OK" , testCase should stop and no further steps should run

There is a way to do this in groovy, but i do not want an extra test step to be added in testcase

            def headers = testRunner.testCase.getTestStepByName("RequestName").httpRequest.response.responseHeaders['#status#']
            if (!headers.contains("HTTP/1.1 200 OK"))
            {
                testRunner.fail("" + headers + "Flow failed")
                testRunner.fail("No futher testSteps will be run inside the current case")
            }

Please note i cannot change below settings due to some other groovy code restriction

enter image description here

The reason for not changing above option i have a testcase where there are 10 steps. 1 is request and other 9 steps validate various things. So if i check the "Abort on error" option and step 3 fails. then none of the steps from 4 to 10 runs. So please provide a solution considering not using "abort" option

So could you please provide a solution for script assertion without ticking this option."Abort on error"

Since testRunner.fail is not available inside script assertion and also normal assertion (assert 0==1) does not stop testcase unless we tick the above setting. i am stuck with this limitations

Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38
  • 1
    By default, if assertion fails, the execution is stopped at the test step and does not execute further steps. Is it not the one you are expecting or happening? – Rao Mar 30 '18 at 05:36
  • For Groovy steps(step nbr 3-10) if assertion fails,it should not stop and it will not since i have uncheck that option. But if request(Step 1) fails, it should be detected by Script assertion and it should stop there itself.. So if we can override that property AbortTest somehow in script assertion and change it based on request that would work. or if we can bring something like Testrunner.fail in script assertion that will also work – Gaurav Khurana Mar 30 '18 at 06:32
  • Is [this](https://stackoverflow.com/questions/39856956/how-to-run-specified-step-in-soapui-according-testcase-result) post helpful? – Rao Mar 30 '18 at 08:28
  • Thanks but this cannot be used as in that post we are using Groovy Script teststep. I would like to achieve this with Script assertion. If a request fails no further testSteps should run and all this has to be done in script Assertion as we dont have any other option. – Gaurav Khurana Mar 30 '18 at 08:42
  • When you say `9 steps to validate various things` - are those actually assertions? – Rao Mar 30 '18 at 10:31
  • They are groovy scripts with various detailed validations on the repsonse... if there are any failures we capture them in list if list size > 0 then we fail that step by assertion – Gaurav Khurana Apr 01 '18 at 10:45

2 Answers2

2

You have access to testRunner through the context variable available in a script assertion, so why not something like:

def httpResponseHeader = messageExchange.responseHeaders
def headers = httpResponseHeader["#status#"]
log.info("Status: " + headers)

if (!headers.contains("HTTP/1.1 200 OK")) { 
    context.testRunner.fail("" + headers + "Flow failed")
    context.testRunner.fail("No futher testSteps will be run inside the current case")
}
craigcaulfield
  • 3,381
  • 10
  • 32
  • 40
0

Thanks @craigcaulifield, your answer has helped me a lot.

Good to know that testRunner is available even in script assertion in a tricky way

Now using script assertion we can stop a testCase if request fails.

However when we run request alone not as part of testcase an error comes

cannot invoke method fail() on null object

This error comes because

context.testRunner.fail()

testRunner is only available during test Case execution and not individual testStep execution

So to overcome here is the code which can take care of both situation

def responseHeaders=messageExchange.responseHeaders
def status=responseHeaders["#status#"]

// Checking if status is successfully fetched i.e. Response is not empty
if(status!=null)
{
    if(!status.contains("HTTP/1.1 200 OK"))
   {
  // context.testRunner is only available when the request is run as a part of testCase and not individually 
    if(context.testRunner!=null)
    {
          // marking the case fail so further steps are not run
        context.testRunner.fail()
     }
    assert false, "Request did not returned successful status. Expected =  [HTTP/1.1 200 OK] but Actual =  " + status
   }
}
else
{
    if(context.testRunner!=null)
    {
    context.testRunner.fail()
    }
    assert false, "Request did not returned any response or empty response"

}
Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38