0

I have a JMeter script that contains many API Requests. What I want to do is execute some/all the APIs depending upon whether the user has provided true/false in properties file. If it is true for the API it should be executed otherwise not.

Properties file looks like this:

#############################API Executions
#Make sure to provide true if you want to execute the API otherwise false.
API1=true
API2=false
API3=true

JMeter Test Plan looks like this:

- Test Plan
    * Thread Group
        - If Controller1
            * API1
        - If Controller2
            * API2
        - If Controller3
            * API3

Current approach that I am following is checking for every API via an If Controller

${__groovy("${__P(API1)}" == "true")}

But this approach does not look good to me, as I need to include an If Controller with every API Request.

Is there any better approach for this problem?

SAIR
  • 479
  • 3
  • 9

1 Answers1

0

We don't know how the different API requests look like hence it's hard to suggest a better approach, you might be interested in the following alternatives:

  1. Instead of multiple If clauses you could use Switch via JMeter's Switch Controller

  2. You could convert the properties from their current state into JMeter Variables looking like:

    API_1=endpoint of API 1
    API_2=endpoint of API 3
    etc.
    

    and use ForEach Controller instead, example conversion code would be:

    def index = 1
    props.entrySet().each { property ->
        if (property.getKey().toString().startsWith('API')) {
            if (property.getValue() == 'true') {
                vars.put('API_' + index, 'endpoint for API')
                index++
            }
        }
    }
    

Also just in case avoid using JMeter Functions or Variables in Groovy scripts as it might conflict with Groovy's string interpolation and in case of JSR223 Test Elements the value will be cached and only first occurrence will be used for subsequent iterations so it might be a better idea to consider changing your function to

 ${__groovy(props.get("API_1") == "true")}

More information on JMeter API shorthands available for the JSR223 Test Elements: Top 8 JMeter Java Classes You Should Be Using with Groovy

Dmitri T
  • 159,985
  • 5
  • 83
  • 133