最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览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);
        }

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");
}
发布评论

评论列表(0)

  1. 暂无评论