0
votes

Here is my simple Jmeter test plan.

Jmeter Sample Test Plan

User Parameters look like this:

Jmeter Sample Test Plan

I'm simply calling one endpoint, reading the response body and according to found IDs with the help of Regex Extractor I'm calling another endpoint. ForEach loop helps make sure for all the found IDs same endpoint is called with ID as parameter in the Path.

What I'm trying to achieve with Another HTTP Request inside ForEach loop is to read the response, and if body contains Monday, increment User Parameter Monday by 1, same for Tuesday and for every other User Parameter. Ideally at the end of the test suite, I should get something like this:

  1. Monday - 5
  2. Tuesday - 3
  3. Wednesday - null or zero
  4. Thursday - null or zero
  5. Friday - 1
  6. Saturday - 12
  7. Sunday - 8

According to my BeanShell script I hope I'm following all the right paths:

import org.apache.commons.lang.StringUtils;

    String response = new String(data);
    int Mondays = 0;
    int Tuesdays = 0;
    int Wednesdays = 0;
    int Thursdays = 0;
    int Fridays = 0;
    int Saturdays = 0;
    int Sundays = 0;

    if(response.contains("'DayOfWeek':'Monday'")){
        Mondays++;
        vars.put("Monday", Mondays.toString);
    };
    if(response.contains("'DayOfWeek':'Tuesday'")){
        Tuesdays++;
        vars.put("Tuesday", Tuesdays.toString);
    };
    if(response.contains("'DayOfWeek':'Wednesday'")){
        Wednesdays++;
        vars.put("Wednesday", Wednesdays.toString);
    };
    if(response.contains("'DayOfWeek':'Thursday'")){
        Thursdays++;
        vars.put("Thursday", Thursdays.toString);
    };
    if(response.contains("'DayOfWeek':'Friday'")){
        Fridays++;
        vars.put("Friday", Fridays.toString);
    };
    if(response.contains("'DayOfWeek':'Saturday'")){
        Saturdays++;
        vars.put("Saturday", Saturdays.toString);
    };
    if(response.contains("'DayOfWeek':'Sunday'")){
        Sundays++;
        vars.put("Sunday", Sundays.toString);
    };

My small problem here is that User Parameters variables never get updated and always at the end of the run equals to 0. What am I doing wrong in this situation? Has anyone faced this task before?

1
Still unable to resolve such a simple task...TiredOfProgramming
There were 2 answers to this question, but the second one disappeared somehow....TiredOfProgramming

1 Answers

0
votes
  1. Be aware that starting from JMeter version 3.1 it is recommended to use Groovy for any form of scripting to consider migrating to JSR223 PostProcessor

  2. Looking into repeating 'DayOfWeek':'xxx' pattern it seems you don't need to create 7 branches for each weekday, you can extract the current value using Regular Expressions and set or get and increment the relevant JMeter Variable

Example code would be something like:

def day = (prev.getResponseDataAsString() =~ "'DayOfWeek':'(\\w+)'")[0].get(1)
def value = (vars.get(day) ?: '0') as int
value++
vars.put(day, value as String)

See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests