How to build a jmeter plugin utilising groovy Imagine you want to test a website but you have tons of different types of tests. You want to break test suites down in groups but still reuse the configuration as much as possible.
So what i wanted to do is use User Defined Variables but be able to reuse them across test suites. Some settings would be be globally reused like UserAgentMozilla. You can reuse same user agent string in all tests. Another example could be an user/passowrd pair for a particular type of account. If you want to run your test suites on development, staging and production environments it is nice to have a way of overriding selected variables.
To do that i wanted to have a list of config files that will be loaded one after another and all variables will be overriding previous values (if they were defined already).
Sample test suite
Here is how you can use the Include TestPlans' Vars plugin:

Each file will be loaded, User defined variables in the root node will be parsed and added to the scope. Then next file will be loaded etc.
Configuration files
Jmeter user defined variables are very nice way of configuring test plans as you can easily edit them as XML source as well as click around in jmeter itself. They are more flexible than csv files especially if you want to be able to add any content as values.
Here are examples of these two config files:
global.jmx

api.jmx

Now test plan including both of them will have all the variables and name will have value "peter".
Creating a plugin
To create a jmeter plugin all you need is a clean project with a class implementing one of the jmeter interfaces, you can also use some of the abstract classes.
You drop jmeter jars and its dependencies into the lib folder of your project and add a simple ant script as below:
<project name="jmeter-ejsmont-plugins" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="lib.dir" value="lib"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="includeAntRuntime" value="false"/>
<path id="my.classpath">
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>
<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc"
classpathref="my.classpath"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<groovyc srcdir="${src.dir}" destdir="${classes.dir}">
<classpath>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
<javac source="1.6" target="1.6" debug="on" />
</groovyc>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar"
basedir="${classes.dir}" >
<fileset id="resources" dir="${src.dir}">
<include name="**/*.properties"/>
</fileset>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>
Then you drop in the groovy-all jar into the lib folder and you are all set. Here is example of groovy class for parsing of the jmx config elements:
package org.ejsmont.java.jmeter.plugins.config;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
class XmlConfigVarsLoader {
private static final Logger log = LoggingManager.getLoggerForClass()
private transient String filename;
private static final long serialVersionUID = 2L;
public void addXmlVars(String fileNames, JMeterVariables threadVars) {
log.debug( "XmlConfigVarsLoader FILES '" + fileNames +"'" )
fileNames.eachLine{ addXmlVarsFromFileName(it, threadVars) }
}
protected void addXmlVarsFromFileName(String fileName, JMeterVariables threadVars) {
try{
def xmlString = new File(fileName).getText()
def jmeterTestPlan = new XmlSlurper().parseText(xmlString)
log.debug( "XmlConfigVarsLoader PARSING '" + fileName +"'")
jmeterTestPlan.hashTree.hashTree.Arguments.collectionProp.children().each {
def name = it.children().findAll{ it.@name == 'Argument.name' }
def value = it.children().findAll{ it.@name == 'Argument.value' }
log.debug('Adding variable: ' + name + ' = ' + value)
threadVars.put((String)name, (String)value)
}
}catch(Exception e){
log.error( "FILE DOES NOT EXIST '" + fileName +"'")
}
}
}
If you want to see more you can get the sorce of the sample plugin for a user config variables inclusion.
Once you are done with the plugin you just drop the jar into jmeter/lib/ext folder, you will also need to drop groovy-all jar into jmeter/lib folder. Here is the jmeter plugin to include multiple jmx test plans and extract user defined variables
I hope someone will find it useful :)
Main Blog Categories
About the author

Hi, my name is Artur Ejsmont,
welcome to my blog.
I am a passionate software engineer living in Sydney and working for Yahoo! Drop me a line or leave a comment.
Follow @artur_ejsmont
Comments
Post new comment