In this article, Lets see how we can modularize the JMeter load test scripts for easy maintenance / reusability.
To achieve that we need to know about the following JMeter elements.
Test Fragment element is a special controller which can be added directly under JMeter test plan like Thread Group. But It does nothing except holding other elements inside!! It gets executed only when it is referenced by a Module/Include controller from other Thread Groups. It acts like a library of reusable of scripts.
Module Controller in JMeter is useful in referencing any of the logic controller in the JMeter Test Plan.
For Example, I have 5 thread groups as given below in my test.
Some functionalities could be common for these thread groups. For example, User has to login to crate orders / to view the existing products.
In the above example, you can see, Both thread groups have to have the login functionality. Whenever the login functionality changes, I need to ensure that I update the script in both thread groups.
So, instead of having duplicate simple controllers for login in both thread groups, I can add a Test Fragment & move the ‘User Login’ Simple Controller under the Test Fragment. So that it can be referenced by a Module Controller. [It does not have to be a Simple Controller always. It can be any controller].
Now, If the script for login changes, I have to just update the ‘User Login’ under the Test Fragment. Both thread groups will work just fine.
One ‘User Login’ is being accessed by more than 1 Thread Group in the above example. Sometimes, these Thread Groups might want to use these Simple/Transaction controllers under the Test Fragment like a function so that they can pass different data & they expect the controllers to perform the actions based on the data passed to it.
For example, My requirement is to use VISA credit card while ordering a new product & to use MasterCard while editing/upgrading the existing order. [Sorry if it is a stupid example. :)]
I can use a Parameterized Controller for this purpose.
I add the Parameterized Controller first. Then I add the Module Controller under the Parameterized Controller. Now the Module Controller calls the ‘Checkout’ & passes the test data to be used in the ‘Checkout’.
Checkout controller will use test data passed to it while performing the requests.
As a Module controller is used to call a logic controller in the Test Plan, Include Controller is used to reference an existing .jmx file itself.
For Example, The application is very complex & 2 engineers are involved in script creation.
Developer A is creating a test script for Login & Searching for the Products functionality of the application.
Developer B is creating a test script for Checkout.
Both engineers come up with a different .jmx files for the different modules of the application.
Now we create the final JMeter test plan which will reference these external ‘.jmx’ files as given below.
I can have different jmx files for Login, Order Product, Product Search, User Search, View Product, Edit Order, Cancel Order
Now I can create any business flow I want by referencing the external jmx files. [Add the User Defined Variables, Cookie Manager etc in the final JMeter Test. Not in the included files. ]
Login -> Order Product -> View Product
Login -> Order Product -> Edit Order
Login -> Product Search -> Order Product
Login -> Order Product -> Product Search -> Cancel Order