JMeter – How To Create Reusable & Modular Test Scripts

Overview:

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 create 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:

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:

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!

 

Happy Testing & Subscribe 🙂

 

 

Share This:

Categories: Articles, Best Practices, Extend JMeter, Framework, JMeter

12 comments

    • vIns

      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.

  • DRC

    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.

    • vIns

      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.

  • bala

    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.

    • vIns

      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?

  • Lena

    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!

    • vIns

      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.

    • vIns

      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.

Leave a Reply

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