简体   繁体   中英

How to call a javaScript function in Beanshell in Jmeter

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);
        }

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.

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM