Jmeter and JavaScript Assertions

Apache JMeter can be used to help you regression test your application by letting you create test scripts with assertions to validate that your application is returning the results you expect. These assertions allow you to assert facts about responses received from the server being tested. For instance, you can assert that the response to a query will contain some particular pattern that can be either be:

a “string” for “Equals” or “Substring” clauses
a “Perl5-style” Regular Expression for “Contains” or “Matches” clauses

Sometimes JSON Path Assertions are insufficient for confirming a response. This is where JSR233 (Scripting for the Java Platform) Assertions come in! JSR233 Assertions come in many favours of Scripting language, including JavaScript / EcmaScript.

One necessary step is using JS233 Assertions is setting variables for use in the assertions which is done separately in a JSON Path Extractor to extract values from JSON responses using JSONPath syntax.

Once a variable is set it can be used in javascript with a var.get statement (assuming a json path extractor has created a subtotal destination variable with the name “subtotal :

1
var subtotal = vars.get("subtotal");

The javascript variable can now be used as normal.

Two other useful Jmeter functions available to your script are part of the AssertionResult class :

AssertionResult.setFailureMessage(String); Set the failure message giving more details about a failure or error.

AssertionResult.setFailure(boolean); Set the flag indicating whether or not a failure occurred.

A Best Practice would be to include the description (and feature or bug name) in the comments to understand why the test is needed.

To installing JMeter with all the needed plugins on Mac OSX :

1
$brew install jmeter --with-plugins

,
and if you need to install java (so sad) on OSX Yosemite :

1
2
3
 $brew tap caskroom/cask
 $brew install brew-cask
 $brew cask install java

after which you may need to uninstall (via brew) and re-install jmeter.

Soon, I hope and Api Garage will replace my need for Jmeter. Soon!

Leave a Reply