Reusability of components is an important consideration in the software development world. As part of performance testing, we frequently need to use similar scripts multiple times in various workflows which adds extra effort, time and expense. A simple solution is the creation of reusable scripts or fragments -build once, use repeatedly. The reusable code blocks help build automation scripts in a short span of time reducing development time, improving efficiency and cycle time.
In this blog, we will walk through how to create reusable scripts in Apache JMeter. JMeter provides a feature called Test Fragment, where we can write a script to be used multiple times in different flows.
Let’s consider an e-commerce application. An example scenario is the creation of API/Performance test scripts for various flows where the user needs to purchase a product from different sections and then log out from the process – in short, login to the application, perform specific actions and log out.
Here we can have two reusable blocks:
How to Create Reusable Test Fragments in JMeter
To add a Test Fragment, right-click on “Test Plan” and follow the steps below:
Test Plan -> Add -> Test Fragment -> Test Fragment – as shown in the following screenshot:
Steps to Create Reusable Test Fragment
- Step 1 – Adding “Simple Controller” to the Test Fragment
- Step 2 – Adding “HTTP Request” to the Simple Controller
- Step 3 – Adding “HTTP Header Manager” to the HTTP Request
Step1: How to add HTTP request to the Test Fragment:
To add the HTTP request, we need a “Simple Controller” which can be added by right-clicking on Test Fragment – Test Fragment -> Add -> Logic Controller -> Simple Controller
STEP2: Adding “HTTP Request” to the Simple Controller:
After adding the Simple Controller, the next step is to add HTTP request by right-clicking on Simple Controller – Simple Controller-> Add-> Sampler->HTTP Request
STEP3: Adding “HTTP Request” to the Simple Controller:
Once you have added the API details and other information in the HTTP request, the next step is to add the HTTP Header Manager to that HTTP Request. To add HTTP Header Manager, right click on HTTP Request – HTTP Request -> Add -> Config Element- > HTTP Header Manager
After following the above steps, your test fragment is ready and now it can be reused based on the requirements.
How to reuse Test Fragments in different performance flows
To reuse the developed test fragment, it needs to be added to the Thread group. For each flow, there should be a separate thread group.
To add the Thread group, right-click on Test Plan – Test Plan-> Threads (Users) -> Thread Group
After adding the thread group, the Module Controller needs to be added to the Thread Group to call the pre-created Test Fragments. To add the Module Controller, right-click on thread group – Thread Group -> Add -> Logic Controller -> Module Controller
After adding the module controller for a flow, we need to select the test fragment which we want to run by selecting the test fragment on the “Module to Run” UI as shown in the following screenshot.
The screenshot below shows how the test plan would look like after adding multiple flows and using the test fragment for the common step(s).
The test result for all the positive and negative scenarios that use the test fragments can be seen as below:-
An actual use case
Now, let’s review how the above process was utilized as part of a Salesforce project for customer account creation involving 20+ objects. The customer creation process involves objects like contact info, bill to, ship to, bank account and more. Reusable fragments were created in JMeter to run automated tests using these objects. Through test automation, the information related to each object is verified when the scripts are executed every time, without manual intervention writing and executing them. These reusable fragments can also be utilized across multiple projects involving similar test cases and objects.
Reusable fragments in JMeter can help improve the efficiency of testing teams in scenarios where similar processes or steps are to be tested. The reusability factor garners benefits of faster development, test automation, and reduced time consumption. If you haven’t employed reusable fragments/blocks yet, Contact us to get started.