Santiago Peraza is a Performance Engineer. He is a ISTQB certified software tester. Santiago's main areas of expertise are JMeter scripting and executing cloud and on-premise performance tests.

Learn JMeter in 5 Hours

Start Learning
Slack

Run massively scalable performance tests on web, mobile, and APIs

Aug 28 2017

Selection Statements in JMeter Made Easy

When running a load test, we sometimes need to alter the script flow. To achieve that goal, Apache JMeter™ offers control flow statements. This blog posts series will explain how to use these JMeter elements to control your load test flow. We will start with Selection Statements, which are a kind of Control Flow Statement.

 

What are Control Flow Statements?

 

Control Flow Statements are statements that determine which step is going to be executed next, how many times, etc. In other words, they allow us to alter the linear statement’s flow execution of our script. Control Flow Statements are widely used in almost every programing language.

 

Control Flow Statements can be classified in several ways. For example, according to whether they evaluate a condition, they repeat the previous action, or whether they jump to another part of the script.

 

What are Selection Statements?

 

In this part, we are going to focus our attention on Selection statements. As their name suggests, this category of statements allow us to change the flow of the execution by specifying, or selecting, which statement will be executed next.

 

In this blog post we are going to review two types of Selection statements in JMeter: the If Controller and Switch Controller.

 

If Controller

 

The If Controller evaluates a condition. If the result of evaluating the condition is true, then the following test elements are executed. If not, they will be skipped.

 

To learn the basics of the If Controller, click here. To learn more advanced If Controller best practices, check out this blog post: “6 Tips for JMeter If Controller Usage”.

 

Let’s look at the If Controller configuration:

 

jmeter selection statements if controller

  • Condition: Here is where you enter the condition for the If Controller. By default, the condition is interpreted as Javascript code that returns "true" or "false", but you can override that by selecting the following option - “interpret condition as variable expression?”
  • Interpret Condition as Variable Expression?: which will evaluate the expression to be "true" only. Note: Checking this option and using a __jexl3 or __groovy function in the Condition is advised for a better performance. New to Groovy? Check out this post.
  • Evaluate for all children?: This checkbox allows you to specify whether the condition should be evaluated for all children or only evaluated for the first child element only.

 

Let’s see how it works.

 

We will create a simple script to navigate to blazedemo.com (a flight reservation site used for testing purposes), search for flights between San Diego and New York, and if there are flights from United Airlines under $500.00, then we are going to buy one ticket.

 

As you can see in the image below, in Step 02 – Search flights, we have included a Regular Expression Extractor that matches the price of United Airlines flights, and stores the value in the variable price.

 

Then, on Step 03 – Select flight, before selecting the flight we have added the If Controller with the condition ${price} < 500, which will evaluate whether the variable extracted on Step 02 is less than 500. If so, the script execution will proceed and the flight will be selected and the ticket will be bought. Conversely, if the price is not less than 500, then the script will finish the iteration.

 

how to use selection statements with jmeter if controller

 

As we noted before, for a better performance it is advised to use __jexl3 or __groovy functions to evaluate a condition. The image below illustrates that:

 

jmeter if controller how to use

 

Please note that if you are evaluating a string variable then it needs to be quoted.

 

Switch Controller

 

The Switch Controller is also a popular programming statement. However, you should note that the behavior of JMeter’s Switch Controller is a bit different than the one used in many programming languages. In fact, it is simpler, since it does not evaluate the condition but simply points to the element that should be executed next.

 

jmeter selection statements switch controller

 

With that being said, there are two ways to configure the Switch Controller:

 

1. Point to the position of one of the children elements: The Switch Controller looks for the subordinate element at the position specified in the Switch Value and executes it (counting starts from 0).

 

 

2. Look for a child element with the same name: If the Switch Value is non-numeric, then the Switch Controller looks for the element with the same name (case sensitive).

 

Let’s see how it works.

 

We will use the flight reservation script that we used for the If Controller, but this time, we will have three different steps for Step 02 - Search flights:

  • Step 02 - Search flights from San Diego to New York
  • Step 02 - Search flights from Boston to New York
  • Step 02 - Search flights from Philadelphia to New York

 

And we will use the Switch Controller to specify which of them will be run.

 

jmeter open-source selection statements test scenario script switch controller

 

Point to the position of one of the children elements

 

In the screenshot below, we have set the Switch Value to 2, so JMeter will look for the subordinate element at the position specified in the Switch Value (starting from 0) and execute it.

 

how to use the switch controller

 

As you can see, the script executed Step 02 - Search flights from Philadelphia to New York. Remember, counting starts from 0. Step 0 was Search flights from San Diego to New York. Step 1 was Search flights from Boston to New York.

 

Look for a child element with the same name

 

It is also possible to specify the element to run by entering its name in the Switch Value (case sensitive).

 

switch controller, jmeter, selection statements

 

You can also determine weights with Groovy functions. Learn how from here.

 

What if...

  • The numeric switch value is out of range? It runs the zeroth element.
  • We leave the switch value empty? It runs the zeroth element.
  • None of the names matches with the switch value? Then the element named “default” is run.
  • None of the names matches with the switch value and there is no “default” element? No element is selected, hence, the controller does not run any subordinate element.

 

That's all for now! In the next post we will go over other useful Control Flow Statements that will allow you to create more flexible and realistic load test scripts.

 

You can learn more JMeter for free through our JMeter academy.

 

Click here to subscribe to our newsletter.

 

To check out BlazeMeter, which enhances JMeter, request a demo or just put your URL or JMX file in the box below, and your test will start in minutes.

Interested in writing for our Blog?Send us a pitch!

Your email is required to complete the test. If you proceed, your test will be aborted.