如何在 JMeter 脚本中以编程方式添加变量。

huangapple 未分类评论56阅读模式
英文:

How to add variables to JMeter script programmatically

问题

我正在将Postman集合迁移到jMeter。导入Postman集合本身没有问题,但变量是个问题。我的Postman集合严重依赖于它们。我可以从Postman导出这些变量,但找不到导入它们的方法。我在导入时还需要应用一些逻辑。我正考虑编写Java代码将Postman变量导入jMeter中。似乎保存这些变量的jMeter类是ValueReplacer,我知道如何向ValueReplacer添加变量,但找不到将填充的ValueReplacer添加到测试计划并保存的方法。

我的加载测试计划的代码:

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));
// 将valueReplacer添加到testPlanTree并保存

那么,我能否将valueReplacer对象添加到脚本中并保存呢?

英文:

I am in process of migrating Postman collections to jMeter. I have no problem importing Postman collection itself, but the variables are the problem. My Postman collections rely on them heavily. I can export the variables from Postman, but couldn't find the way to import them. I also need to apply some logic while importing. I am thinking about writing the Java code to import the Postman variables into 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.

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. Is there a way to add valueReplacer object to the script and save it?

答案1

得分: 0

你无需显式调用ValueReplacer类,如果你想要从Java代码运行现有的JMeter测试计划,只需确保JMeter变量仅在其各自位置通过用户定义的变量声明,或来自后处理器

因此,如果你在用户定义的变量中声明变量foo,其值为bar

如何在 JMeter 脚本中以编程方式添加变量。

你将能够在需要的地方引用它${foo}

如何在 JMeter 脚本中以编程方式添加变量。

如果你从Java代码运行JMeter测试,JMeter将在运行时评估变量并解析其值。

英文:

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:

如何在 JMeter 脚本中以编程方式添加变量。

You will be able to refer it as ${foo} where required:

如何在 JMeter 脚本中以编程方式添加变量。

If you run your JMeter test from Java code JMeter will evaluate the variable in the runtime and resolve their values.

答案2

得分: 0

经过一些调查,似乎不可能直接导入 Postman 变量。由于我找到的所有从 Postman 转换为 jMeter 的工具都无法处理我所需的所有内容:Postman 变量、formdata、oauth2,所以最终我写了自己的转换器。

英文:

After some investigation it doesn't seem to be possible to import Postman variables directly. 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

huangapple
  • 本文由 发表于 2020年4月9日 03:24:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/61108447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定