简体   繁体   English

如何以编程方式将变量添加到 JMeter 脚本

[英]How to add variables to JMeter script programmatically

I am in process of migrating Postman collections to jMeter.我正在将 Postman collections 迁移到 jMeter。 I have no problem importing Postman collection itself, but the variables are the problem.我导入 Postman 集合本身没有问题,但变量是问题所在。 My Postman collections rely on them heavily.我的 Postman collections 严重依赖它们。 I can export the variables from Postman, but couldn't find the way to import them.我可以从 Postman 导出变量,但找不到导入它们的方法。 I also need to apply some logic while importing.我还需要在导入时应用一些逻辑。 I am thinking about writing the Java code to import the Postman variables into jMeter.我正在考虑编写 Java 代码以将 Postman 变量导入 jMeter。 The jMeter class which seems to hold the variables is ValueReplacer and I know how to add variables to ValueReplacer, but couldn't find the way to add the populated ValueReplacer to test plan and save it.似乎包含变量的 jMeter class 是 ValueReplacer,我知道如何将变量添加到 ValueReplacer,但找不到将填充的 ValueReplacer 添加到测试计划并保存它的方法。

My code which loads the test plan:我加载测试计划的代码:

    StandardJMeterEngine standardJMeterEngine = new StandardJMeterEngine();
    JMeterUtils.loadJMeterProperties(jMeterHome + "/bin/jmeter.properties");
    JMeterUtils.setJMeterHome(jMeterHome);
    JMeterUtils.initLocale();
    HashTree testPlanTree = SaveService.loadTree(new File(jMeterScript));
    standardJMeterEngine.configure(testPlanTree);
    ValueReplacer valueReplacer = geterateValueReplacer(new File(postmanExportedVariables));
    // add valueReplacer to testPlanTree  and save

So I am able to load the script and generate the ValueReplacer.所以我能够加载脚本并生成 ValueReplacer。 Is there a way to add valueReplacer object to the script and save it?有没有办法将 valueReplacer object 添加到脚本并保存?

You don't need to explicitly call the ValueReplacer class, if you want to run an existing JMeter test plan from Java code just make sure that JMeter Variables are just declared in their respective places via User Defined Variables or come from the Post-Processors You don't need to explicitly call the ValueReplacer class, if you want to run an existing JMeter test plan from Java code just make sure that JMeter Variables are just declared in their respective places via User Defined Variables or come from the Post-Processors

So if you declare the variable foo with the value of bar in the User Defined Variables:因此,如果您在用户定义的变量中使用bar的值声明变量foo

在此处输入图像描述

You will be able to refer it as ${foo} where required:您可以在需要时将其称为${foo}

在此处输入图像描述

If you run your JMeter test from Java code JMeter will evaluate the variable in the runtime and resolve their values.如果您从 Java 代码运行 JMeter 测试,JMeter 将在运行时评估变量并解析它们的值。

After some investigation it doesn't seem to be possible to import Postman variables directly.经过一番调查,似乎无法直接导入 Postman 变量。 Since none of the Postman to jMeter converters I found can handle everything I need: Postman variables, formdata, oauth2 I ended up writing my own converter由于我发现没有一个 Postman 到 jMeter 转换器可以处理我需要的一切:Postman 变量、formdata、oauth2 我最终编写了自己的转换器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM