将 javascript 函数添加到 jMeter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14852573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:44:50  来源:igfitidea点击:

Adding javascript function to jMeter

javascriptjmeter

提问by Ruchit Rami

I am trying to get a javascript function work with jMeter test plan.
It is used to decode a string.

我正在尝试使用 jMeter 测试计划使用 javascript 函数。
它用于解码字符串。

function decode(str) {
    var strtodecrypt = str.split("-");
    var msglength = strtodecrypt.length;
    decrypted_message = "";
    for (var position = 0; position < msglength; position++) {
        ascii_num_byte_to_decrypt = strtodecrypt[position];
        ascii_num_byte_to_decrypt = ascii_num_byte_to_decrypt / 2;
        ascii_num_byte_to_decrypt = ascii_num_byte_to_decrypt - 5;
        decrypted_byte = String.fromCharCode(ascii_num_byte_to_decrypt);
        decrypted_message += decrypted_byte;
    }
    return decrypted_message;
}

I have tried to use BSF Post processor, but don't know what is the exact syntax i need to use. I want to use this function to post a jMeter variable as a parameter in one of the HTTP request.

我曾尝试使用 BSF 后处理器,但不知道我需要使用的确切语法是什么。我想使用此函数在 HTTP 请求之一中将 jMeter 变量作为参数发布。

Edit:I am currently using following script in the BSF post processor. userResponsedoes not show in debub sampler. Do i need to add any reference to use String.fromCharCode(ascii_num_byte_to_decrypt)?

编辑:我目前在 BSF 后处理器中使用以下脚本。userResponse不显示在 debub 采样器中。我需要添加任何引用才能使用String.fromCharCode(ascii_num_byte_to_decrypt)吗?

var str="142";
var strtodecrypt = str.split("-");
var msglength = strtodecrypt.length;
decrypted_message = "";

for (var position = 0; position < msglength; position++) {
    ascii_num_byte_to_decrypt = strtodecrypt[position];
    ascii_num_byte_to_decrypt = ascii_num_byte_to_decrypt / 2;
    ascii_num_byte_to_decrypt = ascii_num_byte_to_decrypt - 5;
    decrypted_byte = String.fromCharCode(ascii_num_byte_to_decrypt);
    decrypted_message += decrypted_byte;
}

vars.put("userResponse",decrypted_message);

回答by Aliaksandr Belik

You can try to use as well JSR223 Samplerwith script language set to javascript (Language: JavaScript).
It will process your script (2nd version), variable set and available in debug Sampler results.

您也可以尝试使用脚本语言设置为 javascript ( ) 的JSR223 SamplerLanguage: JavaScript
它将处理您的脚本(第二版)、变量集并在调试采样器结果中可用。

回答by d.popov

You should WebDriver plugin for that. It can be configured as IE/Firefox/Chrome or even Selenium.

您应该为此使用 WebDriver 插件。它可以配置为 IE/Firefox/Chrome 甚至 Selenium。

The documentation here

这里的文档

This is how you configure IE web driver

这是您配置IE Web 驱动程序的方式

回答by MrBrushy

For anyone reading this years later:

对于多年后阅读的任何人:

Calling javascript function is possible from a BSF PostProcessor.

可以从 BSF PostProcessor 调用 javascript 函数。

You need to define your function higher upin the file than where it is used.

您需要在文件中比使用它的位置更高的位置定义您的函数。

This means this works:

这意味着这有效:

function decode(str) {
    (...... do stuff......)
    return something;
}

var bar = decode("foo");
vars.put("someVariableName", bar);

However this doesn't work:

但是,这不起作用:

var bar = decode("foo"); // <--- Compile error, undefined function 'decode'
vars.put("someVariableName", bar);

function decode(str) {
    (...... do stuff......)
    return something;
}