Tiago Machado
Tiago Machado

Reputation: 394

soapUI groovy script setting test step status to test step with different behavior in script/test suit window

I created a testSuit, and I have some test steps. The objective is to run the test steps one by one, and set the status to OK or to FAILED, for that I get the httpStatus code and set it to FAILED or OK.

This part seems to be good. The problem is, with the code I have, in the groovy script when I click the green button to run the script it gives me the message: "java.lang.NullPointerException: Cannot invoke method setStatus() on null object error at line 17". and the color of the script keeps red.

BUT, if I click on "Test Steps", a small window will appear with the test steps, the groovy script, etc...If I right click the groovy script and choose "Test from here" The script run, the test step I run in the script turn green or red depending if it fails or pass, and the groovy script turns blue.

So... I don't understand whats happening... what's the diference of opening the "Groovy Script" window and run it from there, and opening the "Test Steps" window and running the script from there?

The code:

import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus
def example = testRunner.runTestStepByName("example");
def myStep = context.testCase.getTestStepByName("example")
def exampleResult = testRunner.results.find { it.testStep.is(myStep) }
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def httpResponseHeaders = context.testCase.testSteps["example"].testRequest.response.responseHeaders
def httpStatus = httpResponseHeaders["#status#"]
def httpStatusCode = (httpStatus =~ "[1-5]\\d\\d")[0]

log.info("HTTP status code: " + httpStatusCode)

if (httpStatusCode != "200"){
    testRunner.fail("failed");
    exampleResult.setStatus(TestStepStatus.FAILED);
} else {
    log.info("passed");
    exampleResult.setStatus(TestStepStatus.OK);
}

Note: Im colorblind, so not sure 100% if the green I said above is really green or yellow and the blue could be pink...

Thanks for the help!

Upvotes: 0

Views: 1856

Answers (2)

Chris Adams
Chris Adams

Reputation: 1454

Re So... I don't understand whats happening... what's the diference of opening the "Groovy Script" window and run it from there, and opening the "Test Steps" window and running the script from there? It's not clear to me whether the script you're referring to is a Groovy script assertion on the test step or Groovy Script step within the test case. Either way, the step needs to have been ran before you can obtain details from the response of the step.

I know you've answered your question, but I'll plow on....

When you create a test step and run it, they tend to pass by default. The user then decides how to define what is a Pass or Fail. This can be purely based on a 200 response, like what you're doing, or something more complex by creating assertions against the response to ensure the API is returning expected values.

I'm plowing on as SoapUI provides what you want 'out of the box' without resorting to groovy scripts. If you 'open' the step of interest, you'll see an 'Assertions' section. From here you can add any number of assertions of varying types. Once of them is 'Valid HTTP Status Code', if you select this, you can enter the valid status codes for that step. e.g. 200. When you then run the test case, SoapUI will display the result based on the assertion. If you do do this, try putting 500 as the valid status code and then run. All your tests will then fail.

If this answer is not helpful or applicable to the issue you had, let me know and I'll just delete it.

Upvotes: 0

Tiago Machado
Tiago Machado

Reputation: 394

Found the solution.

There is no need of some things in my code.

The exampleResult, and myStep are not needed.

I can just use the example.setStatus(TestStepStatus.OK). Worked as expected after that.

Upvotes: 1

Related Questions