vlatko606
vlatko606

Reputation: 1149

Jmeter - How to loop data based on the 'jar' file

I want to create scenario where i want use data from jar file into Jmeter Loop logic.

My jar looks like:

public String Australia()
{
    String a = "{"
            + "\"location\": {" 
            + "\"lat\": -33.8669710,"
            + "\"lng\": 151.1958750"
            + "},"
            + "\"accuracy\": 50,"
            + "\"name\": \"Google Shoes!\","
            + "\"phone_number\": \"(02) 9374 4000\","
            + "\"address\": \"48 Pirrama Road, Pyrmont, NSW 2009, Australia\","
            + "\"types\": [\"shoe_store\"],"
            + "\"website\": \"http://www.google.com.au/\","
            + "\"language\": \"en-AU\""
            +
            "}";

    return a;
}

public String canada()
{
    String c = "{"
            + "\"location\": {" 
            + "\"lat\": -33.8669710,"
            + "\"lng\": 151.1958750"
            + "},"
            + "\"accuracy\": 50,"
            + "\"name\": \"Google Shoes!\","
            + "\"phone_number\": \"(02) 9374 4000\","
            + "\"address\": \"48 Pirrama Road, Pyrmont, NSW 2009, Canada\","
            + "\"types\": [\"shoe_store\"],"
            + "\"website\": \"http://www.google.com.ca/\","
            + "\"language\": \"en-CA\""
            +
            "}";

    return c;
}

1) with above data i want to 'feed' Jmeter call as described on the bellow picture

enter image description here

2) every time i add new country at the jar file, loop be increased accordingly.

Some thought how this could be done, what should i use as variable and how i can tell the loop to increase?

Upvotes: 1

Views: 215

Answers (1)

Dmitri T
Dmitri T

Reputation: 168092

  1. Add JSR223 PreProcessor as a child of the 002_2_send payment request
  2. Put the following code into "Script" area:

    def testData = new com.example.TestData()
    def methods = testData.class.getDeclaredMethods()
    def payload = org.apache.commons.lang.reflect.MethodUtils.invokeExactMethod(testData, methods[vars.get('__jm__Loop Controller__idx') as int].getName())
    sampler.addNonEncodedArgument('',payload,'')
    sampler.setPostBodyRaw(true)
    
  3. Define "Loop Count" in the Loop Controller using __groovy() function like:

    ${__groovy(com.example.TestData.getDeclaredMethods().size(),)}
    

    enter image description here

  4. Change com.example to your own package name and TestData to your class name

  5. Once you drop the new version of .jar to JMeter Classpath you will need to restart JMeter to pick up the changes

That's it, each iteration of the Loop Controller the JSR223 PreProcessor will execute the next function in your class and update the request body with the returned data:

enter image description here

References:

Upvotes: 2

Related Questions