在录制测试时,当我选中一个复选框时,它会返回'On'作为值。我已经在JMeter中制定了测试计划,现在我需要在每个请求中将值随机设置为On或OFF。
我可以使用Beanshell脚本+分割或随机功能来实现它吗?
您可以使用__RandomFromMultipleVars函数在值之间随机
RandomFromMultipleVars函数根据Source Variables提供的变量值返回随机值
如果在变量内置ON和OFF,例如在on
和off
JMeter变量里面,你可以使用:
${__RandomFromMultipleVars(on|off, rnd)}
它会将随机值保存在rnd
变量(第二个参数)中,以后可以用作${rnd}
获得相同结果的另一个选择是添加以下JMeter变量:
a_matchNr = 2
a_1 = on
a_2 = off
然后使用JMeter变量前缀a
的函数:
${__RandomFromMultipleVars(a, rnd)}
Starting from JMeter version 3.1 users are encouraged to switch to Groovy from Beanshell主要是因为Groovy has much better performance comparing to other scripting options。
所以你可以使用__groovy() function这样做:
${__groovy(['On'\,'OFF'][new Random().nextInt(2)],)}
演示:
在运行多次之后最后得到它,我们可以把它写成 - $ {__ RandomFromMultipleVars {__ split($ {String1},value,|)},rememberMe)}。现在即使字符串在拆分后生成100个值,这些值也会传递给__RandomFromMultipleVars,它只接受变量值。