How to call a javaScript function in Beanshell in Jmeter - Stack Overflow

I'm using Jmeter for testing purposes and I have to edit a paticular variable which is extracted t

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

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);
        }
Share Improve this question asked Aug 12, 2013 at 4:48 ycrycr 14.6k3 gold badges28 silver badges52 bronze badges 3
  • Beanshell allows you to execute arbitrary Java code, not JavaScript. – leonm Commented Aug 12, 2013 at 4:55
  • Thanks for the Reply. But if I need to use a JavaScript how can I do that? – ycr 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. – Manish Sapariya Commented Aug 12, 2013 at 6:58
Add a ment  | 

2 Answers 2

Reset to default 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.

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

http://jmeter.apache/usermanual/ponent_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");
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745309392a4621910.html

相关推荐

  • How to call a javaScript function in Beanshell in Jmeter - Stack Overflow

    I'm using Jmeter for testing purposes and I have to edit a paticular variable which is extracted t

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信