2

this is my objective:

keep the Test Plan more flexible and usable both on win and mac (since some people use mac and other use win).

I created this simple script in groovy:

import org.apache.jmeter.services.FileServer;
import groovy.json.JsonSlurper;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

    String winPath;
    String macPath;
    String winSlash;
    String macSlash;
    String userPath;
    String userSlash;

    if (System.properties['os.name'].toLowerCase().contains('windows')) {
        winPath="C:\\QA\\";
        winSlash="\\";
        vars.put("userPath",winPath.toString());
    }
    if (System.properties['os.name'].toLowerCase().contains('mac')) {
        macPath="/Users/macUser/QA/";
        macSlash="/";
        vars.put("userPath",macPath.toString());
    }

and add it into a "JSR223 Sampler" object under my Thread Group object

Then I've added a "User Defined Variables" object with the following var:

    Name        value
    projectDir  myProjectDir
    rootPath    ${__groovy(props.getProperty("userPath"))}${projectDir}

Then I tried to used the rootPath variable for setting the path of my csv files, so I've added ${projectDir}/AUTH.csv to FileName in "CSV Data Set Config" object, but I got this message:

2018-11-23 16:36:40,634 DEBUG o.a.j.t.TestBeanHelper: Ignoring property 'property' in org.apache.jmeter.config.CSVDataSet
2018-11-23 16:36:40,634 DEBUG o.a.j.t.TestBeanHelper: Setting filename=myProjectPath/AUTH.csv

2018-11-23 16:36:40,634 DEBUG o.a.j.t.TestCompiler: Subtracting node, stack size = 2
2018-11-23 16:36:40,634 DEBUG o.a.j.t.TestCompiler: Subtracting node, stack size = 1
2018-11-23 16:36:40,634 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2018-11-23 16:36:40,634 INFO o.a.j.s.FileServer: Stored: myProjectPath/AUTH.csv
2018-11-23 16:36:40,635 ERROR o.a.j.t.JMeterThread: Test failed!
java.lang.IllegalArgumentException: Could not read file header line for file myProjectPath/AUTH.csv

as you can see it trying to read myProjectPath/AUTH.csv and then off course it get an exception..

why it doesn't "read" the variable rootPath ?

any suggestions?

ClaudioM
  • 1,418
  • 2
  • 16
  • 42

2 Answers2

3
  1. According to the User Defined Variables documentation:

    Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.

  2. Additionally be aware of JMeter Test Elements Execution Order

    0. Configuration elements
    1. Pre-Processors
    2. Timers
    3. Sampler
    4. Post-Processors (unless SampleResult is null)
    5. Assertions (unless SampleResult is null)
    6. Listeners (unless SampleResult is null)
    

Assuming above points your Groovy code is being executed after User Defined Variables therefore you cannot access the value. So the only way to define dynamic value depending on the operating system in the User Defined Variables is using __groovy() function directly in the Value section like:

${__groovy(if(System.getProperty('os.name').toLowerCase().contains('windows')){return 'C:\\\QA\\\' } else { return '/Users/macUser/QA/' },)}

enter image description here

Make sure to escape commas and backslashes with another backslash as in JMeter Functions comma acts as parameters separator and backslash is an escape character. Check out Apache JMeter Functions - An Introduction guide to learn more about JMeter Functions contept.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • thank you for your suggestion @Dmitri T but I found another solution using the global variables and Bean Shell Assertion – ClaudioM Nov 27 '18 at 11:19
0

The issue is that you try to add it to the properties and try to read it from the variables.

Also, don't bother the \ or / in Java. Java handles both on every platform. (Difference between File.separator and slash in paths)

For me this works fine:

def path;

if (System.properties['os.name'].toLowerCase().contains('windows')) {
    path="C:\\QA\\";
} else if (System.properties['os.name'].toLowerCase().contains('mac')) {
    path="/Users/macUser/QA/";
}
vars.put("userPath",path);
vars.put("rootPath", path+vars.get("projectDir"));

And to use it: log.info(vars.get("rootPath"))

Sven
  • 2,343
  • 1
  • 18
  • 29
  • ok @Sven thanks for your suggestion.. but how should I use the variable rootPath in "User Defined Variables" ? I tried ˋ${__groovy(vars.get("rootPath"))}ˋ and ˋvars.get("rootPath")ˋ but doesn't works – ClaudioM Nov 26 '18 at 09:18
  • Normally you should just use ${rootPath}. – Sven Nov 26 '18 at 10:01
  • ok I found the solution .. the only way to read (or better extract) the variable from groovy and read it in User Defined Variable is using the BeanShell Assertion. There you can set your global properties and then you can read it from UDV. – ClaudioM Nov 26 '18 at 13:54