1
votes

Is it possible to / how do you run a groovy script from a SoapUI assertion without copy/pasting the script into all of your test steps where you need the same script executed? Is it possible to write a script outside of the assertion and run the script like you are calling a method? So that you can reuse the assertion script in multiple test steps.

So far, I've tried to call a groovy test step from within the assertion, but the run() method requires a testRunner variable which is unavailable from within the assertion. I've also tried to write a groovy script as a subsequent test step (not an assertion) that calls another groovy test step script, but I was unable to transfer the response from one test step to the next (Honestly, I'd rather not create test steps that are really just assertions).

Note: this is not a duplicate of How to create variables in soapui test case that can be accessed across all test steps - groovy test step & script assertion test step? because that question pertains to storing properties, not reusing scripts.

1
Are you looking for the "Script Library"? soapui.org/scripting-properties/… - SiKing
@SiKing 's comment should, IMHO, be the answer. I've wrote several assertion classes, but I wrote them in Java and stored them in the scripts folders of the SoapUI install. You still need a groovy step in your test to call the external script, but if your checks change, you only need to update the external script in one place. - Chris Adams
@ChrisAdams the solution needs to be portable, i.e., the project xml will be handed off to multiple people. Separate files loaded into soapui as libraries seems like it would add a layer of complexity to the portability. I'd really like the solution to be visible (and editable) on the project layer and in the SoapUI screen itself. - ryvantage

1 Answers

1
votes

I was able to finally figure out my 2nd approach: add another groovy script as a subsequent test step that has assertions and passes the response. The script is:

context.response = context.expand('${MyTestStep#Response}') // store response to context variable
Object result = testRunner.testCase.testSuite.testCases['Validate Response'].testSteps['Validate Response'].run(testRunner, context)

if(result.getError() != null) {
    log.error("error", result.getError())
    assert false
}
assert true

MyTestStep is the test step before the groovy script. Validate Response is the name of the test case of the groovy script which is also called Validate Response and is executed via the run method.