Feb. 13th, 2014

How to Save ‘Loads’ of Time Using JMeter's Template Feature

During your recent forays into JMeter (from JMeter V2.10+), you may have noticed a new, colorful icon. Well, it’s definitely time to check out JMeter’s new Template feature. It's totally fabulous dahhling.
 
JMeter's Feature Template Icon
 

What are JMeter Templates?

JMeter Templates are skeleton scripts that you can reuse to get started with a project. You can opt to use existing templates or create a new one yourself. You can also create “snippets” of code that you can insert into your script with templates. 
 
Click on the icon, or choose File -> Templates.....
Select one from the dropdown and get going.
 
JMeter-create template
 
Each existing template has an exhaustive description and useful links to understand each and every element in the created test plan. 
 
Select the one you want, click Create and the framework will be automatically be created! Just edit some variables and run your own script.   
 
creat trees-JMeter template feature
 
Almost all existing templates can create a new test plan. But the BeanShell Sampler can be added to an existing script. Select element in the tree that you want to append, click the Templates icon, select it, and click the Merge button. 
 
beanshell-JMeter
 
 
 
The BeanShell Sampler will be added to the selected location. It already contains some of the script examples. 
 
JMeter's Beanshell Sampler
 
This Template has enough data to run the script without any additional modifications.
How awesome is that? In minutes, you have your own test script (ps-You can also use BlazeMeter's Chrome Extension to record JMeter scripts right from your browser). 
 
Let’s run it.
 
BlazeMeter's Load Testing Dashbosrd-Scripts run with the JMeter Template Script
 
Creating your JMeter Template
 
If you are anything like me, you probably create new scripts using the same base framework every time. But with JMeter's new Template feature you can create varying templates which you can utilize as often as you need.
 
Create your own combo (I created a simple CSV Data Set Config example) and save it: 'Save Test Plan As (Ctrl + Shift + S)'
 
Create CSV Example using JMeter's Template feature
 
Save the new .jmx file to apache-jmeter-2.10/bin/templates/
*All templates perform in the apache-jmeter-2.10/bin/templates/templates.xml file.
 
Each template described by 4 tags:
- <template> - root tag that describe, will this template be create new test plan (isTestPlan="true"), or can be merged to existed (isTestPlan="false"). 
- <name> - template name
- <fileName> - path to .jmx file
- <description> - template description, presented in HTML text. 
 
For example, let’s add this script to template.xml:
 
Sample script from JMeter's Template feature
 
Now, restart JMeter, click the Templates icon and… voila:
 
Create a JMeter Template
 
Try it out. Let us know how it works for you.
 

Interested in writing for our Blog? Send us a pitch!