Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding javascript function to jMeter

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

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.

Edit: I am currently using following script in the BSF post processor. userResponse does not show in debub sampler. Do i need to add any reference to use 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);
like image 982
Ruchit Rami Avatar asked Jul 10 '26 09:07

Ruchit Rami


1 Answers

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

like image 112
Aliaksandr Belik Avatar answered Jul 11 '26 21:07

Aliaksandr Belik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!