How to Create Re-Usable Fragments in JMeterReusability 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.

Example -

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:

  1. Login
  2. Logout

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

  1. Step 1 - Adding “Simple Controller” to the Test Fragment
  2. Step 2 - Adding “HTTP Request” to the Simple Controller
  3. 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.

Conclusion

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.


Written by Prem Gupta

Senior Software Test Engineer

Prem has over 5 years of experience in IT industry, working on various software projects in domains like Ecommerce, Travelling, Real Estate, Medical and Income Tax. He is a seasoned developer having worked on functional and test automation tools such as Selenium, Appium, Protractor, Sikuli, TOSCA, etc.

Prem has played an integral role in developing Automation Hybrid frameworks using TestNG, POM, Maven and Cucumber. He constantly works on learning and refining his skills on cutting edge technologies for QA and testing.

If you would like to connect with Prem Prem.Gupta@emtecinc.com

Leave a comment

Continuous Testing

 

Popular Posts

Get IT Insights in Your Inbox