问题描述
我需要在Jmeter中设置和获取变量以实现API自动化.
I need to set and get variables in Jmeter for API automation.
我正在使用groovy脚本.
I am using the groovy script for same.
我使用以下代码实现了相同的目的:
I have achieved same using code as below:
import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("PC_CREATED_PROMO_CODE", "shubhamvalue");
log.info("will it work? ="+JMeterUtils.getProperty("PC_CREATED_PROMO_CODE"))
现在的问题是我无法在任何容器中看到该值,我可以在其中设置令牌,baseURL,标头之类的硬代码值.它应该与我们在SOAP-UI或邮递员测试中所做的相似.
Now the problem is I am not able to see the value in any contanier where I can set my hardcode values like token, baseURL, Headers. it should be similar we do in SOAP-UI or postman tests.
请让我知道是否可以在Jmeter的文件/节/容器中看到这些setProperty值.
Please let me know if I can see these setProperty values in file/section/container in Jmeter.
或者建议我其他更可行的解决方法.
Or suggest me any other workaround which is more feasible for same.
任何解决方法都将对您有所帮助,并提前得到赞赏.
Any workaround will be helpful and appreciated in advance.
推荐答案
如果您需要获取并设置变量,我建议您使用vars
速记
If you need to get and set variables I would recommend using vars
shorthand
根据文档
因此,我建议将变量设置为:vars.put('foo', 'bar')
并以${foo}
的形式访问它们,根据我的期望,您将为每个线程(虚拟用户)获得不同的PC_CREATED_PROMO_CODE
So I would suggest setting variables as: vars.put('foo', 'bar')
and accessing them as ${foo}
where required as my expectation is that you will be getting different PC_CREATED_PROMO_CODE
for each thread (virtual user)
另外请注意,还建议您尽可能避免编写脚本,因此请考虑使用 JSON提取器.
Also be aware that it is also recommended to avoid scripting where possible so consider going for JSON Extractor instead.
这篇关于如何在Jmeter中设置和获取环境变量以测试API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!