javascript - 设置 Jmeter 全局 javascript 对象

标签 javascript jmeter

我有这个简单的对象:

var apple = {
    type: "macintosh",
    color: "red",
    getInfo: function(int){
        return "test" + int;
    }
}

在 Jmeter 中,我想将此对象放入一个允许我访问此对象的全局变量中。

我试过:

vars.putObject("test",apple);(在预处理器中,所以在所有断言之前)

var test = vars.getObject("test");(在所有断言中)

但似乎该函数被转换为字符串,因此我不能在我的断言中使用它。

如何让它发挥作用?

最佳答案

如果您正在寻找“全局”解决方案,您需要考虑 JMeter 属性而不是 JMeter 变量,即使用 props 简写而不是 vars。根据 Sharing Variables用户手册章节:

The get() and put() methods only support variables with String values, but there are also getObject() and putObject() methods which can be used for arbitrary objects. JMeter variables are local to a thread, but can be used by all test elements (not just Beanshell). If you need to share variables between threads, then JMeter properties can be used

例如在一个测试元素中:

props.put('test', apple)

在另一个线程组中(也可以在另一个线程组中)

var apple = props.get('test')
log.info(apple.getInfo(1))

JMeter Properties across thread groups.

另请注意 starting from JMeter 3.1 it is recommended to use Groovy language for any form of scripting由于 Groovy 性能比其他脚本选项好得多,请查看 Apache Groovy - Why and How You Should Use It指南了解更多详情。

关于javascript - 设置 Jmeter 全局 javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46807043/

相关文章:

java - 使用正则表达式或 xpath 从包含特定文本的 html 中提取 <td> 值

JMeter:Linux 环境中的分布式(远程)测试

java - 如何使用JPDA调试jmeter-server?

javascript - CSS 和 HTML : How to create an Expanding DIV On Click?

javascript - 如何检查我的对象类型是图像?

javascript - npm 风格的 Emacs JS 模式

javascript - 我应该保持我的 redis 连接持久吗?

java - Jmeter Testplan - 空测试计划错误

jmeter - 如何使用 JMeter 处理严格传输安全 (HSTS)

javascript - Reverse an array in array 反转数组中的所有数组