Randomize data to send as a request body in Jmeter depending on another set of random data












0















I'm new at scripting in Jmeter and I've tried some functions. What I'm trying to do here is:



I have a POST request with a customer id as a path variable but I'm randomizing it:



http://endpoint/customers/${__RandomFromMultipleVars(customerId1|customerId2|customerId3, customer_id)}/verify


The line above works, and it is getting a random value among the three customer IDs, the request can result to this:



http://endpoint/customers/6fb5ef21-6b3e-4564-952f-e83df63a9907/verify


customerId1, customerId2, and customerId3 came from the config element: User Define Variables



Now, in the response body, I have this groovy script instead of an actual JSON request body:



${__groovy(
if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {

} else if ("${customer_id}" == "8f0b21e5-ddf6-42d4-b07e-6da09db6a611") {

} else if ("${customer_id}" == "40aa4f1b-424b-40d2-8213-36a39f5c98b2") {

)}


My question is I don't know if it is possible, but for every if statement, I want to randomize again the request body depending on the customer Id that was set.



I've tried doing:



if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {
${__RandomFromMultipleVars(resp1|resp2|resp3)}
}


But it resulted to an error. I've tried just returning a simple string:



if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {
"TEST"
}


And it works, the string "TEST" was used as the request body. I'm really not sure on how to move forward with this. I appreciate any comments or suggestions. Thanks!










share|improve this question



























    0















    I'm new at scripting in Jmeter and I've tried some functions. What I'm trying to do here is:



    I have a POST request with a customer id as a path variable but I'm randomizing it:



    http://endpoint/customers/${__RandomFromMultipleVars(customerId1|customerId2|customerId3, customer_id)}/verify


    The line above works, and it is getting a random value among the three customer IDs, the request can result to this:



    http://endpoint/customers/6fb5ef21-6b3e-4564-952f-e83df63a9907/verify


    customerId1, customerId2, and customerId3 came from the config element: User Define Variables



    Now, in the response body, I have this groovy script instead of an actual JSON request body:



    ${__groovy(
    if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {

    } else if ("${customer_id}" == "8f0b21e5-ddf6-42d4-b07e-6da09db6a611") {

    } else if ("${customer_id}" == "40aa4f1b-424b-40d2-8213-36a39f5c98b2") {

    )}


    My question is I don't know if it is possible, but for every if statement, I want to randomize again the request body depending on the customer Id that was set.



    I've tried doing:



    if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {
    ${__RandomFromMultipleVars(resp1|resp2|resp3)}
    }


    But it resulted to an error. I've tried just returning a simple string:



    if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {
    "TEST"
    }


    And it works, the string "TEST" was used as the request body. I'm really not sure on how to move forward with this. I appreciate any comments or suggestions. Thanks!










    share|improve this question

























      0












      0








      0








      I'm new at scripting in Jmeter and I've tried some functions. What I'm trying to do here is:



      I have a POST request with a customer id as a path variable but I'm randomizing it:



      http://endpoint/customers/${__RandomFromMultipleVars(customerId1|customerId2|customerId3, customer_id)}/verify


      The line above works, and it is getting a random value among the three customer IDs, the request can result to this:



      http://endpoint/customers/6fb5ef21-6b3e-4564-952f-e83df63a9907/verify


      customerId1, customerId2, and customerId3 came from the config element: User Define Variables



      Now, in the response body, I have this groovy script instead of an actual JSON request body:



      ${__groovy(
      if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {

      } else if ("${customer_id}" == "8f0b21e5-ddf6-42d4-b07e-6da09db6a611") {

      } else if ("${customer_id}" == "40aa4f1b-424b-40d2-8213-36a39f5c98b2") {

      )}


      My question is I don't know if it is possible, but for every if statement, I want to randomize again the request body depending on the customer Id that was set.



      I've tried doing:



      if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {
      ${__RandomFromMultipleVars(resp1|resp2|resp3)}
      }


      But it resulted to an error. I've tried just returning a simple string:



      if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {
      "TEST"
      }


      And it works, the string "TEST" was used as the request body. I'm really not sure on how to move forward with this. I appreciate any comments or suggestions. Thanks!










      share|improve this question














      I'm new at scripting in Jmeter and I've tried some functions. What I'm trying to do here is:



      I have a POST request with a customer id as a path variable but I'm randomizing it:



      http://endpoint/customers/${__RandomFromMultipleVars(customerId1|customerId2|customerId3, customer_id)}/verify


      The line above works, and it is getting a random value among the three customer IDs, the request can result to this:



      http://endpoint/customers/6fb5ef21-6b3e-4564-952f-e83df63a9907/verify


      customerId1, customerId2, and customerId3 came from the config element: User Define Variables



      Now, in the response body, I have this groovy script instead of an actual JSON request body:



      ${__groovy(
      if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {

      } else if ("${customer_id}" == "8f0b21e5-ddf6-42d4-b07e-6da09db6a611") {

      } else if ("${customer_id}" == "40aa4f1b-424b-40d2-8213-36a39f5c98b2") {

      )}


      My question is I don't know if it is possible, but for every if statement, I want to randomize again the request body depending on the customer Id that was set.



      I've tried doing:



      if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {
      ${__RandomFromMultipleVars(resp1|resp2|resp3)}
      }


      But it resulted to an error. I've tried just returning a simple string:



      if ("${customer_id}" == "6fb5ef21-6b3e-4564-952f-e83df63a9907") {
      "TEST"
      }


      And it works, the string "TEST" was used as the request body. I'm really not sure on how to move forward with this. I appreciate any comments or suggestions. Thanks!







      groovy jmeter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 10:04









      cascas

      73




      73
























          1 Answer
          1






          active

          oldest

          votes


















          1














          According to JSR223 Sampler documentation:




          JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.



          props.get("START.HMS");

          props.put("PROP1","1234");



          So remember that you should not inline JMeter Functions and/or Variables into Groovy scripts. Instead of accessing variables like ${customer_id} you should be using vars shorthand instead, it stands for JMeterVariables class instance.



          If you want to proceed with __groovy() function the syntax you're looking for should be something like:



          ${__groovy(if (vars.get('customer_id').equals('6fb5ef21-6b3e-4564-952f-e83df63a9907')) { return vars.get('resp' + org.apache.commons.lang3.RandomUtils.nextInt(1, 4))},)}


          Demo:





          • Variables defined at Test Plan level



            enter image description here




          • Groovy script in action:



            enter image description here




          More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It






          share|improve this answer
























          • Thank you so much! The script works on my side! Noted, on the first part, I will try to learn this more. Just a follow up question: This code here: ` org.apache.commons.lang3.RandomUtils.nextInt(1, 4)` What is the '' for?

            – cas
            Nov 22 '18 at 10:41













          • It is needed to escape the comma which is used as parameters delimiter in JMeter Functions

            – Dmitri T
            Nov 22 '18 at 11:03











          • Noted, and thank you! Before posting this question I've tried using 'vars' but I'm not sure on how to use it properly and I keep getting errors. Thank you also for showing how to use vars :)

            – cas
            Nov 22 '18 at 11:10











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428406%2frandomize-data-to-send-as-a-request-body-in-jmeter-depending-on-another-set-of-r%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          According to JSR223 Sampler documentation:




          JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.



          props.get("START.HMS");

          props.put("PROP1","1234");



          So remember that you should not inline JMeter Functions and/or Variables into Groovy scripts. Instead of accessing variables like ${customer_id} you should be using vars shorthand instead, it stands for JMeterVariables class instance.



          If you want to proceed with __groovy() function the syntax you're looking for should be something like:



          ${__groovy(if (vars.get('customer_id').equals('6fb5ef21-6b3e-4564-952f-e83df63a9907')) { return vars.get('resp' + org.apache.commons.lang3.RandomUtils.nextInt(1, 4))},)}


          Demo:





          • Variables defined at Test Plan level



            enter image description here




          • Groovy script in action:



            enter image description here




          More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It






          share|improve this answer
























          • Thank you so much! The script works on my side! Noted, on the first part, I will try to learn this more. Just a follow up question: This code here: ` org.apache.commons.lang3.RandomUtils.nextInt(1, 4)` What is the '' for?

            – cas
            Nov 22 '18 at 10:41













          • It is needed to escape the comma which is used as parameters delimiter in JMeter Functions

            – Dmitri T
            Nov 22 '18 at 11:03











          • Noted, and thank you! Before posting this question I've tried using 'vars' but I'm not sure on how to use it properly and I keep getting errors. Thank you also for showing how to use vars :)

            – cas
            Nov 22 '18 at 11:10
















          1














          According to JSR223 Sampler documentation:




          JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.



          props.get("START.HMS");

          props.put("PROP1","1234");



          So remember that you should not inline JMeter Functions and/or Variables into Groovy scripts. Instead of accessing variables like ${customer_id} you should be using vars shorthand instead, it stands for JMeterVariables class instance.



          If you want to proceed with __groovy() function the syntax you're looking for should be something like:



          ${__groovy(if (vars.get('customer_id').equals('6fb5ef21-6b3e-4564-952f-e83df63a9907')) { return vars.get('resp' + org.apache.commons.lang3.RandomUtils.nextInt(1, 4))},)}


          Demo:





          • Variables defined at Test Plan level



            enter image description here




          • Groovy script in action:



            enter image description here




          More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It






          share|improve this answer
























          • Thank you so much! The script works on my side! Noted, on the first part, I will try to learn this more. Just a follow up question: This code here: ` org.apache.commons.lang3.RandomUtils.nextInt(1, 4)` What is the '' for?

            – cas
            Nov 22 '18 at 10:41













          • It is needed to escape the comma which is used as parameters delimiter in JMeter Functions

            – Dmitri T
            Nov 22 '18 at 11:03











          • Noted, and thank you! Before posting this question I've tried using 'vars' but I'm not sure on how to use it properly and I keep getting errors. Thank you also for showing how to use vars :)

            – cas
            Nov 22 '18 at 11:10














          1












          1








          1







          According to JSR223 Sampler documentation:




          JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.



          props.get("START.HMS");

          props.put("PROP1","1234");



          So remember that you should not inline JMeter Functions and/or Variables into Groovy scripts. Instead of accessing variables like ${customer_id} you should be using vars shorthand instead, it stands for JMeterVariables class instance.



          If you want to proceed with __groovy() function the syntax you're looking for should be something like:



          ${__groovy(if (vars.get('customer_id').equals('6fb5ef21-6b3e-4564-952f-e83df63a9907')) { return vars.get('resp' + org.apache.commons.lang3.RandomUtils.nextInt(1, 4))},)}


          Demo:





          • Variables defined at Test Plan level



            enter image description here




          • Groovy script in action:



            enter image description here




          More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It






          share|improve this answer













          According to JSR223 Sampler documentation:




          JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.



          props.get("START.HMS");

          props.put("PROP1","1234");



          So remember that you should not inline JMeter Functions and/or Variables into Groovy scripts. Instead of accessing variables like ${customer_id} you should be using vars shorthand instead, it stands for JMeterVariables class instance.



          If you want to proceed with __groovy() function the syntax you're looking for should be something like:



          ${__groovy(if (vars.get('customer_id').equals('6fb5ef21-6b3e-4564-952f-e83df63a9907')) { return vars.get('resp' + org.apache.commons.lang3.RandomUtils.nextInt(1, 4))},)}


          Demo:





          • Variables defined at Test Plan level



            enter image description here




          • Groovy script in action:



            enter image description here




          More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 10:27









          Dmitri TDmitri T

          72.8k33763




          72.8k33763













          • Thank you so much! The script works on my side! Noted, on the first part, I will try to learn this more. Just a follow up question: This code here: ` org.apache.commons.lang3.RandomUtils.nextInt(1, 4)` What is the '' for?

            – cas
            Nov 22 '18 at 10:41













          • It is needed to escape the comma which is used as parameters delimiter in JMeter Functions

            – Dmitri T
            Nov 22 '18 at 11:03











          • Noted, and thank you! Before posting this question I've tried using 'vars' but I'm not sure on how to use it properly and I keep getting errors. Thank you also for showing how to use vars :)

            – cas
            Nov 22 '18 at 11:10



















          • Thank you so much! The script works on my side! Noted, on the first part, I will try to learn this more. Just a follow up question: This code here: ` org.apache.commons.lang3.RandomUtils.nextInt(1, 4)` What is the '' for?

            – cas
            Nov 22 '18 at 10:41













          • It is needed to escape the comma which is used as parameters delimiter in JMeter Functions

            – Dmitri T
            Nov 22 '18 at 11:03











          • Noted, and thank you! Before posting this question I've tried using 'vars' but I'm not sure on how to use it properly and I keep getting errors. Thank you also for showing how to use vars :)

            – cas
            Nov 22 '18 at 11:10

















          Thank you so much! The script works on my side! Noted, on the first part, I will try to learn this more. Just a follow up question: This code here: ` org.apache.commons.lang3.RandomUtils.nextInt(1, 4)` What is the '' for?

          – cas
          Nov 22 '18 at 10:41







          Thank you so much! The script works on my side! Noted, on the first part, I will try to learn this more. Just a follow up question: This code here: ` org.apache.commons.lang3.RandomUtils.nextInt(1, 4)` What is the '' for?

          – cas
          Nov 22 '18 at 10:41















          It is needed to escape the comma which is used as parameters delimiter in JMeter Functions

          – Dmitri T
          Nov 22 '18 at 11:03





          It is needed to escape the comma which is used as parameters delimiter in JMeter Functions

          – Dmitri T
          Nov 22 '18 at 11:03













          Noted, and thank you! Before posting this question I've tried using 'vars' but I'm not sure on how to use it properly and I keep getting errors. Thank you also for showing how to use vars :)

          – cas
          Nov 22 '18 at 11:10





          Noted, and thank you! Before posting this question I've tried using 'vars' but I'm not sure on how to use it properly and I keep getting errors. Thank you also for showing how to use vars :)

          – cas
          Nov 22 '18 at 11:10




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428406%2frandomize-data-to-send-as-a-request-body-in-jmeter-depending-on-another-set-of-r%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]