Adding a ‘Sentinel’ script to SoapUI that will clean up the test environment after the test suite run

After following just two steps described below you’ll be able to run any test case after the whole Test Suite finished its run.
This might be useful when you have to clean up the mess your test suite made to the test environment 🙂

OK, enough gibberish, let’s make it happen. What we actually have to do is :

  1. Create a test case(s) that will do the clean up after the test suite run. (*)
  2. Add a TearDown Script to your Test Suite

* – disable them, to exclude them from the main test suite run.

Here’s the screenshot with my configuration:

sentinel script

sentinel script

And here’s the code that we need to add to a TearDown Script in your Test Suite:

// this is needed to run a test case
def properties = new com.eviware.soapui.support.types.StringToObjectMap()

// "sometimes" I have to work with unstable builds, 
// so to make sure that I can actually access anything I've added this 
// test case that checks the API availability (it makes a simple request towards the API and fails if there's no proper response)
def checkAPIAvailability = testSuite.getTestCaseByName( "checkAPIAvailability" )
// a test case that makes all the requests and runs all the scripts needed to clean up the environment after the test suite run
def runCleanUpStep = testSuite.getTestCaseByName( "cleanUpEnvAfterTestrun_-_RegularUser" )
// counter that gives a bit more control on for how long you want to check for the API availability
int counter = 1;

// Do the first check
boolean status = ( checkAPIAvailability.run(properties, false).status.toString().equals("FINISHED") ) ? true : false;
// EDIT: Had to change the expression from checking if it failed to the one that checks if it actually passed 
// boolean status = ( checkAPIAvailability.run(properties, false).status.toString().equals("FAILED") ) ? false : true;


// in case the API is not available then loop the whole check thing till it is 🙂
// or stop after 20 loops 
// (consider adding a delay step to the availability check test case which will decrease the number of unnecessary calls
while ( !status && counter <= 20){
	counter++;
	// EDIT: same as above, had to change the expression to handle some strange cases 🙂
	status = ( checkAPIAvailability.run(properties, false).status.toString().equals("FINISHED") ) ? true : false;
}

// I don't know why I left that additional "if" but it is still there just in case ....
if (status){
	runCleanUpStep.run(properties, false)	
}

SoapUI – tips & tricks – part1

I’ve decided to group and share all the helper scripts/methods etc. that I’m using in SoapUI.
This time there are only 3 short hints but I hope they can be useful 🙂


Verify Content-Type of response message using script assertion:

String actual = new String(messageExchange.responseHeaders["Content-Type"].getAt(0).toString())
String expected = "application/json";
assert actual.equals( expected ), "Actual Content-Type: '" + actual + "' does not match expected: '" + expected + "'";



To access body of the response message as XML in script assertion (this method will also transform JSON into XML):

def response = messageExchange.getResponse().getContentAsXml();
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
def holder = groovyUtils.getXmlHolder( response );
holder.namespaces["ns"] = holder.getNodeValue("//namespace::*[2]"); // set the namespace
</br>
def valuesYouWantToGetUsingXPath = holder.getNodeValues( "//ns:list/ns:item/ns:description" ); // get all descriptions
def valueYouWantToGetUsingXPath = holder.getNodeValue( "//ns:list/ns:item[id=1]/ns:description" ); // get specific description



To quickly access the response from different test step as XML and using XPath:

${previousTestStepName#ResponseAsXML#//*:this/*:is/*:your/*:xpath/*:to/*:the/*:value}