skip to Main Content

I am pretty new to testing with JMeter. I have a JSON block that I would like to populate with values from three arrays that I created from a previous GET request. The structure is similar to this:

    "books": [
        {
            "publisher": ${publisherName}, //variable but it's the same for all items in this request
            "ISBN": ${isbn_1},  //the next three are from from 3 different arrays 
            "Price": ${price_1},
            "Publisher": ${publisher_1},
            "Static Value that doesn't change": 100
       
        },

        {
            "publisher": ${publisherName}, //variable but it's the same for all items in this request
            "ISBN": ${isbn_2},  //the next three are from from 3 different arrays 
            "Price": ${price_2},
            "Publisher": ${publisher_2},
            "Static Value that will be the same for all items": 100
       
        },
        
    ],

Is there a way to loop through adding the blocks dynamically so I can send all of these items in a single request?

2

Answers


  1. I assume you have 3 different arrays with same size for ISBN, Price, Publisher. You write code to JSR223 Pre-Processor

    import groovy.json.JsonOutput
    
    def books = []
    
    def isbn_list = ['isbn1', 'isbn2', 'isbn3']
    def price_list = [100, 200, 300]
    def publisher_list = ['A', 'B', 'C']
    
    def publisher = "fix"
    
    for (i = 0; i < isbn_list.size(); i++) {
        def book = [:]
        book['publisher'] = publisher
        book['ISBN'] = isbn_list[i]
        book['Price'] = price_list[i]
        book['Publisher'] = publisher_list[i]
        books.add(book)
    }
    
    vars.put('books', JsonOutput.prettyPrint(JsonOutput.toJson(books)))
    

    enter image description here

    Login or Signup to reply.
  2. You can do it using any suitable JSR223 Test Element and Groovy language

    Without seeing how your "arrays" look like it’s hard to come up with a comprehensive solution, the JSON structure can be created like:

    vars.put('publisherName', 'some publisher')
    vars.put('isbn_1', 'foo')
    vars.put('isbn_2', 'bar')
    vars.put('price_1', '123')
    vars.put('price_2', '456')
    vars.put('publisher_1', 'baz')
    vars.put('publisher_2', 'qux')
    
    
    def arraySize = vars.entrySet().findAll { entry -> entry.getKey().startsWith('isbn_') }.size()
    
    def books = []
    
    1.upto(arraySize, {index ->
        books.add([publisher:vars.get('publisherName'),
                   ISBN: vars.get('isbn_' + index),
                   Price: vars.get('price_' + index),
                   Publisher:vars.get('publisher_' + index),
                   "Static Value that doesn't change": 100
        ])
    })
    
    log.info(new groovy.json.JsonBuilder([books:books]).toPrettyString())
    

    enter image description here

    The generated value can be stored into a JMeter Variable like:

    vars.put('payload', new groovy.json.JsonBuilder([books:books]).toPrettyString())
    

    and used as ${payload} later on where required

    More information:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search