0

I'm using Jmeter for testing purposes and I have to edit a paticular variable which is extracted through a regular expression and I'm trying to edit the variable using a javaScript which is in the Beanshell. First of all I would like to know whether I can iunclude javascripts directly in the Beanshell and second How can I invoke a JavaScript Function. For Example the following code.

var passwd = "abcd@123";
        var newpasswd = "";
        var ranVal= "ABCDEF";
 function SaveClick(){
      print("BBBBB");   

                document.write("<h3>Final Encripted Password : </h3>", newpasswd);
            //document.write(newpasswd);
        }
3
  • Beanshell allows you to execute arbitrary Java code, not JavaScript. Commented Aug 12, 2013 at 4:55
  • Thanks for the Reply. But if I need to use a JavaScript how can I do that? Commented Aug 12, 2013 at 5:00
  • I really doubt, if the javascript in post processor will in anyway, be able to operate on the DOM of the received response. I feel/assume, you are expecting something of that sort, from your sample code. Commented Aug 12, 2013 at 6:58

2 Answers 2

2

You cannot mix Beanshell and javascript.

But to use full javascript, use Jsr223 elements and select javascript.

Note that you will need to inline your function code as it is not possible to call a function outside of element.

Anyway, Javascript does not give you access to DOM, so what you are trying to do with document.write will not work.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use BSF post processor, which has various scripting language, javascript being one of them.

http://jmeter.apache.org/usermanual/component_reference.html#BSF_PostProcessor Following is sample javascript from one of my test plan. Note the use of eval, putObject, put, log etc.

log.info("processing image index response");
if ("" != prev.getResponseDataAsString()) {
    eval( 'var indexJSON = ' + prev.getResponseDataAsString() );
    vars.putObject("indexJSON", indexJSON);

    vars.put("currentThumb", "0");
    vars.put("currentSlide", "0");

    var next_slide_timestamp=indexJSON[0].timestamp;
    vars.put("next_slide_timestamp", "0");

    var maxSlides=indexJSON.length;
    vars.put("maxSlides", maxSlides);
} else {
    vars.put("currentThumb", "0");
    vars.put("currentSlide", "0");
    vars.put("next_slide_timestamp", "0");
    vars.put("maxSlides", "0");
    log.info("index time : empty response , setting defaults to zero");
}

3 Comments

Jsr223 is more up to date way.
Hmm... The document do not talk about the differences. What are the differences. Also, in the Language option and there are multiple similar options. I am to date wondering, whats the differences.
Hi thanks for the Reply I was able to achieve this with the JSR223 Sampler and you don't have to make your code inline. The thing is How can I update a User defined variable or call the JSR223 code so that return value will be assigned to a variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.