Test Fragment In JMeter

Overview:

In this tutorial, I would like to demo Test Fragment In JMeter to show how to create reusable and modular test scripts.

Need For Test Fragment:

My application has few business critical processes from which we could come up with different business workflows. When I was trying to come up with performance test script in JMeter, I needed to find some approach for creating a reusable / modular test scripts. So that I could reuse the scripts for different workflows.

For Ex:

Lets consider a sample application which has below functionalities.

  • Login
  • Logout
  • User Registration
  • Search for Product
  • Select Product
  • Order Product with different payment methods
  • Edit Order
  • Cancel Order
  • Search for User Profile

Using above functionalities, I could come up with different workflows as shown here.

  • The user access the application, searches for product, choose a product, registers, orders a product
    • User Registration -> Search for Product -> Select Product -> Order Product -> Logout
  • Existing user logs in, searches for product, choose a product, registers, orders a product
    • Login -> Search for Product -> Select Product -> Order Product -> Logout
  • Admin user logs in, searches for an user and cancels the order
    • Login -> Search for User -> Cancel Order -> Logout

Since I had too many combinations, I wanted to find a proper way to design my JMeter test scripts. In this article, I will explain how I achieved that using below JMeter elements.

  1. Test Fragment
  2. Module Controller
  3. Parameterized Controller
  4. Include Controller

Test Fragment In JMeter:

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:

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.

  1. New User Registration
  2. User Login & Order Creation
  3. User Login & Product View
  4. Existing Order Edit/Cancel
  5. User Search

Some functionalities could be common for these thread groups. For example, User has to login to crate orders / to view the existing products.

mod001

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].

mod002

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.

Parameterized Controller:

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.

mod003

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’.

mod004

mod005

Checkout controller will use test data passed to it while performing the requests.

mod006

Include Controller:

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.

mod007

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

Summary:

We were bale to successfully demonstrate how to create reusable test script using Test Fragment In JMeter. By designing my JMeter test scripts as shown above, I could create different thread groups for different business workflows. Whenever the application changes, I had to update the script only in one place. All the workflows will remain unchanged!

Read more about JMeter.

Happy learning 🙂

Share This:

21 thoughts on “Test Fragment In JMeter

    1. You do not need any code for this as it is a very simple example. Ensure that you have latest JMeter. Whatever you have under the thread group, place them under a Simple Controller & move under a ‘Test Fragment’. Now call it using a Module Controller.

  1. How to execute test plan when i am using Include controller.

    What i did in my test plan. I have created 2 jmx files. A.jmx and B.jmx.

    I have added 2-Include controller. 1 for A.jmx and 2 for B.jmx.

    How could I executed this 2 jmx at time.

    1. Is your question to run both jmx files at the same time?
      In that case, You need to have 2 thread groups 1 for A.jmx & other is for B.jmx.

      JMeter can run the Thread Groups in parallel. Whatever the elements inside a thread group will be executed one by one.

  2. I add Include Controller between two Simple Controllers but I don’t see the pages from other .jmx file which I included in the main .jmx. Please help.

    1. Include Controller includes the those pages from the other.jmx only at run time. Did you run the test? Do you still face the issue?

  3. Is there a way to include a number of fragments in one JMeter script and call each fragment separately from Include Controller, as oppose to creating separate JMeter script for every module?

    Thank you!

    1. Actually you can not place more than 1 fragment in jmx file. (actually you can..but include controller includes only the first one.)
      Also include controller will try to include the whole jmx file.

    1. Test Fragment is a special type element in JMeter. It is simply used to create reusable scripts. It does not do anything else other than holding jmeter test scripts for your functionalities. It can be disabled.

  4. Hi What is the difference Test Fragment and Simple controller as both of them used to hold test scripts.

    1. Controllers are like Directories/Packages in your machine/in a library. Simple controller are like any simple directories to group few samplers you are interested in. Test Fragment on the other hand are like (C,D) Drives/Jar files. Test Fragment might contain one more Simple controllers etc. It all depends on how you would like to keep your tests organized.

  5. How to separate reusable script from report?

    I have 1 reusable component script with label “user login to website”
    and used by 4 thread group.

    in the jtl file report – only have 1 label and make hard to differentiate statistic per thread group.

    any suggestion?

    1. If Login is reusable module and thread groups call the same Login, then why do you need to report it based on the thread group? If that is what you really want, rename the Login controller name to be like ‘Login ${some.id}’. Now this some.id value should be passed via by thread group which calls the Login. This way, you group login based on Thread Group.

  6. Which one is better in term of jmeter performance? using parameterize controller or UDV in each thread group when we need to pass different data?

    1. I do not see any performance degrade by using parameterize controller. If you are very concerned, you should measure it. there is no other way! I feel the parameterize controller provides some flexibility by creating a new variable/copy of the user defined variable to the module controller. So you do not need to worry if the module controller will change the UDV value or not.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.