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 request is modified each iteration by altering a specific parameter. So, for example in five iterations I want the following HTTP requests to be made:

http://localhost:8080/test/foo.html?id=1
http://localhost:8080/test/foo.html?id=2
http://localhost:8080/test/foo.html?id=3
http://localhost:8080/test/foo.html?id=4
...

I want to configure the identifier values globally for the test plan and use them within the HTTP request samplerer like this:

Path: /test/foo.html?id=${categoryId}

The question now: How do I configure the identifiers values globally (I do not want to use StringFromFile) and how do I reference them in the sampler?

Christian Seifert
  • 2,562
  • 5
  • 23
  • 42

4 Answers4

20

There are several ways you can do this.

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}

BlackGaff
  • 7,284
  • 1
  • 35
  • 45
  • 2
    I knew about the counter but the identifiers I have aren't consecutive (i.e. 3323, 5512, 2345, ...). I also don't want to read from an external file but simple use something like `${__StringFromArrayAtRandomindex('3', '2', '54', '42')}` – Christian Seifert Jun 15 '11 at 18:04
  • Given you have an array of specific values, but don't want an external file, I would recommend "User Parameters" or a beanshell script (updated answer) – BlackGaff Jun 15 '11 at 19:44
  • @BlackGaffANy help on http://stackoverflow.com/questions/19351587/namestringfromfile-valueerr ? – Ava Oct 13 '13 at 23:58
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}.
Aliaksandr Belik
  • 11,827
  • 6
  • 58
  • 86
  • 1
    Just a typo: vars.put("categoryId", category); Otherwise, jMeter (at least, version 2.9 on Java 6) complains. – edrabc Oct 18 '13 at 09:02
13

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.

Community
  • 1
  • 1
AlexGuti
  • 2,553
  • 1
  • 22
  • 26
0

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

/test/foo.html?id=${__groovy(vars.getIteration())}
user7294900
  • 47,183
  • 17
  • 74
  • 157