Use request value from list of values in JMeter

前端 未结 4 1037
萌比男神i
萌比男神i 2021-02-07 04:21

I\'m sure I\'ve already done this in the past but somehow I cannot figure out how ;-) So, here\'s my problem:

I\'m trying to create a JUnit test plan in which a HTTP re

相关标签:
4条回答
  • 2021-02-07 05:16

    To obtain a random variable value from a list, first declare as User variables the list or available values, with a prefix and a incremental index:

    country_1     Spain 
    country_2     France  
    country_3     Portugal  
    country_4     Italy 
    country_5     England
    

    Then you can obtain a random value from the list concatenating the prefix with a random index in the interval:

    ${__V(country_${__Random(1,6,)})}  --> "Spain", "France", "Portugal", etc...
    

    Take a look to this answer for complete explanation.

    0 讨论(0)
  • 2021-02-07 05:17

    Concerning implementing ${__StringFromArrayAtRandomIndex('3', '2', '54', '42')}.

    Suppose you can easily implement your scenario using e.g. BeanShell Sampler / BeanShell PostProcessor with a bit of code.

    E.g.:

    • Set your source variable (via e.g. User Defined Variables):

      Name        Value          
      categories  3,2,54,42,37
      

      (i.e. use comma as delimiter, no spaces before and after comma).

    • Use add BeanShell Sampler/PostProcessor with the following code:

      import java.util.Random;
      
      String[] categories = (vars.get("categories")).split(",");
      
      int idx = new Random().nextInt(categories.length);
      String category = (categories[idx]);
      
      vars.put("categoryId", category);
      
    • Refer further in the script randomly picked form list value as ${categoryId}.
    0 讨论(0)
  • 2021-02-07 05:18

    There are several ways you can do this.

    • If you're only incrementing, you can use a counter element
    • You can use any number of functions to calculate the value
    • You can read from a CSV file, which is the same effect as "StringFromFile" but easier to customize and manipulate

    Given you have an array of values you could:

    • Try the pre-processor "User Parameters", creating one entry for each user/loop
    • Use a Beanshell script to create the array, select one at random and populate your variable

    You reference them exactly as you have listed: ${varName}

    0 讨论(0)
  • 2021-02-07 05:20

    The Simplest solution without variables can be done using vars.getIteration():

    /test/foo.html?id=${__groovy(vars.getIteration())}
    
    0 讨论(0)
提交回复
热议问题