How to randomly inject or not something to the request in JMeter

Nothing really special today, but maybe someone will find it handy 🙂

Sometimes when I want to test if my script is handling various server responses properly, I’m preparing a request that will randomly malform itself or not.
This can be accomplished by randomly injecting something into the URL or msg body.

A simple way to do that is to use a JS or BeanShell script, like this one:

${__javaScript(if(!! Math.round(Math.random() * 1)) {"injectedText";} else {"";} )}

An example HTTP request that will randomly malform itself 🙂

Randomly Inject Something To The URL

Randomly Inject Something To The URL

Introducing Soapy – a helper lib for SoapUI

Hi All,

This project is really an immature one, with a mixture of Java and Groovy, lacking loads of the documentation, so use it at your own risk 🙂
After those few words of encouragement I must say that actually without this lib my friendship with SoapUI would end months ago.
I don’t have much time to describe what kind of stuff you can find in there, so please explore the src code. There’s one example at the end of this post that I find pretty useful.
The best thing is that you can use it with the free version of SoapUI.

As mentioned before, many classes are still missing a decent documentation, but I’m still hoping that one day everything will better and sun will shine for all of us!
Before asking any questions, please read the README.md file and/or read the code itself 🙂

To compile the project you’ll need:

  • Git
  • Java
  • & Maven

Once the build process is done, simply put the soapy-1.0.jar from ./target directory to your SoapUI/bin/ext folder, then restart the SoapUI.
If everything went fine, then you should see a similar entry in soapUI log:

Fri Oct 05 16:41:03 BST 2012:INFO:Adding [/home/jk7/git/soapui-runner/soapui-4.5.1/bin/ext/soapy-1.0.jar] to extensions classpath

To make any use of it, import any Soapy class into your script step or script assertion:

import com.yelllabs.soapy.”name.of.the.class”

Ahh… I almost forgot, here’s the link to my Soapy project on GitHub 🙂

Cheers,
J

How to parse and handle a JSON response:

import com.yelllabs.soapy.helpers.Response;
import com.yelllabs.soapy.json.JSONHandler;

Response r = new Response( context, testRunner, log, "nameOfTheStepWithJSONResponse" );
JSONHandler jh = new JSONHandler( log );
Object json = jh.parse( r.getRawResponse() );
// validate the JSON 
jh.isValid(json); 
jh.isObject(json);

// then to access any element, simply:
json.elementName.arrayName[positionInArray].etc

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)	
}