Suhas Deshpande
Suhas Deshpande

Reputation: 183

Using jmeter variable in if controller

I want to use the jmeter if controller and use a jmeter variable which I get while processing the previous response. I have two services search and register. The logic I want to use is hit the search service, if I get a good response (i.e. the search exists) no need to register. If the search is empty hit register service and check the search service again. So I have a Simple controller. Under simple controller I have search service with BSF assertion. Next thing under simple controller is the if controller (with register service) for which I need a variable (say ${found} ) I will be creating the variable in bsf assertion as

import groovy.json.*
def slurper = new JsonSlurper()
def result = slurper.parseText(prev.getResponseDataAsString())

if (result.id != null ) {
   def found = 0 // can be text logical or any other type ..
}

Question: Can I use the variable ${found} created in the bsf assertion search service as a condition for If controller ? Will it be available beyond the service. Will it be better to have it as user defined variable ?

Upvotes: 1

Views: 4343

Answers (2)

Dmitri T
Dmitri T

Reputation: 168002

Depending on found variable type it can be:

  • vars.put("found", found); - for String
  • vars.put("found", String.valueOf(found)); - for chars, integers, floats, doubles, booleans, etc.
  • vars.putObject("found", found) - for anything which cannot be cast to a String
  • props.put(found, found); - for any Object types. Opposite to JMeter Variables JMeter Properties have "global" scope and Variables visibility is limited to the current thread group only so if you need to pass this value to another Thread Group you need to use properties.

Be careful while setting conditions in the If Controller as in case of string literals you'll need to put both variable and value in quotation marks like:

"${found}"=="someid"

See How to use JMeter's 'IF' Controller and get Pie. guide for more details.

By the way, there is a couple of test elements available via JMeter Plugins which are designed to work with JSON data so you won't have to use BSF scripting:

Upvotes: 1

Zubair M Hamdani
Zubair M Hamdani

Reputation: 1733

yes the variable "found" can be used. But you need to add the following at the end of your code

vars.put("found",found);

Hope this will help.

Upvotes: 0

Related Questions