Jmeter – If controller example

If controller  example to control which request to send based on response of the other sampler.

We’re using Dummy Sampler to generate requests.

We also need to extract response’s code. We can use Regular Expression Extractor to get it:

  • Field to check: Response Code
  • Reference Name: RESPONSE_CODE
  • Regular Expression: (\d+)
  • Template $1$

First If Controller Condition

  • ${RESPONSE_CODE}!="200"

Second If Controller Condition

  •  ${RESPONSE_CODE}=="200"

Setup Dummy Sampler to return code 200.

Dummy Sampler - 200

Result:

if controller - yes condition

And now edit it to return something other than 200

Dummy Sampler - 404

Result:

If Controller result

 

Jmeter – If controller example

Jmeter’s Variables From CSV File – relative path

Jmeters Variables From CSV File config element set its base directory as a user’s home directory. If you ‘Browse’ your file it will put something like:

/Users/your_user/./your_file.txt

So in such case we need to pass base directory to be able to work with relative paths for our files.

1. Define base directory in User Defined Variables:

baseDir =>  ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir())}

2. Use it in your element:

${baseDir}/../data/your_data.csv
Jmeter’s Variables From CSV File – relative path

Use Jmeter’s base directory

You can access these variables using Beanshell as follows:

  1. JMeter’s base dir
    import org.apache.jmeter.services.FileServer;
    
    String baseDir = FileServer.getFileServer().getBaseDir();
    vars.put("baseDir", baseDir);
    
  2. JMeter’s script location (GUI mode)
    import org.apache.jmeter.gui.GuiPackage;
    
    String testPlanFile = GuiPackage.getInstance().getTestPlanFile();
    vars.put("testPlanFile", testPlanFile);
    
  3. JMeter’s script name (non-GUI mode)
    import org.apache.jmeter.services.FileServer;
    
    String scriptName = FileServer.getFileServer().getScriptName();
    vars.put("scriptName", scriptName); 
    

If you prefer you can get the same using __Beanshell() function as

${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir())}

and it will return current base directory where it is called.

source: http://stackoverflow.com/a/28110434/1037754

Use Jmeter’s base directory