Extending JMeter – Creating Custom Functions – String Joiner

JMeter is one of the best open source tools in the Test Automation Community. It comes with all the possible extensions to come up with our test scripts quickly. To make our life even more easier, It also lets us to come up with our own plugins by implementing few interfaces.

In this article, Lets see how we can create a custom function and make it appear in the below JMeter’s Function Helper dialog.

cf001

 

Goal:

My goal is to create a simple Join function which will join 2 given strings with a given delimiter & store it in an user defined variable.

The usage would be  ${__join(string1, string2, delimiter, resultVariable)}

Reference:

We will refer to the JMeter’s AbstractFunction which should be extended for creating our own function &  __strLen function to get an idea how our own function can be implemented.

 

Setting Up IDE:

Lets first set up our IDE with all the dependencies.

  • Create a simple Maven project

cf002

  • Add below dependency for creating a custom function – add other dependencies as you need.

 

Creating Custom Function:

  • I create a StrJoin.java by extending ‘AbstractFunction
  • I let the IDE add the ‘unimplemented methods’. It will look like this.

cf003

  • getReferenceKey method returns the name of the function.  Source is here.
  • So I create a String constant with a function name and make this method return the variable. This is what is going to appear in the Function Helper dialog.

  • getArgumentDesc method returns a list of Strings to describe each parameter of our custom function.  Source is here.

cf004

  • So I create a LinkedList to add the description for each parameter for our custom function as shown below.

  • The parameter argument of the setParameters method will hold all the parameters we set in JMeter for our custom function. Lets store them in an array for our use.

  • The logic for joining 2 Strings should be in the ‘execute‘ method.

The StrJoin class would look like this.

 

Export:

  • Export this class as a Jar file or mvn clean package command will create the jar file.
  • Copy the Jar file and place it under JMETER_HOME / lib / ext folder. Restart JMeter.
  • Check the Function Helper. Our custom function should appear there. (If it does not appear, check the JMeter log file for any exception)

cf005

 

Testing Custom Function:

  • I create a simple test in JMeter to use our custom function as shown here.

cf006

  • Execute and verify the Debug Sampler’s Response Data.

cf007

  • JMeter joins the 2 strings as we had expected and store the result in the user-defined variable.

 

Summary:

I hope this article would help you to come up with your own function implementation. We will also see more how we can extend other JMeter’s test elements in our future articles.

 

Happy Testing & Subscribe 🙂

 

 

Share This:

Categories: Articles, Extend JMeter, JMeter