jmeter-用户定义的变量和常规

时间:2018-11-23 15:47:08

标签: groovy jmeter

这是我的目标:

在Win和Mac上保持测试计划更加灵活和可用(因为某些人使用Mac,而其他人则使用Win)。

我用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());
    }

并将其添加到“线程组”对象下的“ JSR223采样器”对象中

然后,我添加了一个带有以下var的“用户定义的变量”对象:

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

然后,我尝试使用rootPath变量设置csv文件的路径,因此在“ CSV数据集配置”对象中将${projectDir}/AUTH.csv添加到FileName,但是却收到此消息:

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

如您所见,它试图读取myProjectPath/AUTH.csv,然后偏离路线,它会出现异常。

为什么不“读取”变量rootPath?

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

  1. 根据用户定义的变量documentation

      

    请注意,测试计划中的所有UDV元素-无论它们在何处-都会在开始时进行处理

  2. 另外请注意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)
    

假设以上几点,您的Groovy代码将在用户定义的变量之后之后执行,因此您无法访问该值。因此,根据用户定义变量中的操作系统定义动态值的唯一方法是直接在Value部分中使用__groovy()函数,例如:

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

enter image description here

确保像在JMeter Functions中一样用另一个反斜杠转义逗号和反斜杠,逗号用作参数分隔符,反斜杠是转义字符。查阅Apache JMeter Functions - An Introduction指南,以了解更多有关JMeter Functions概念的信息。

答案 1 :(得分:0)

问题是您尝试将其添加到properties并尝试从variables中读取它。

此外,不要打扰Java中的\或/。 Java在每个平台上都可以处理。 (Difference between File.separator and slash in paths

对我来说,这很好:

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"));

并使用它:log.info(vars.get("rootPath"))

相关问题