How to open and update json file with jmeter and then run the batch script

678 views Asked by At

I am using jmeter 5.3 and I got my jmeter test, that uses data from pre-created JSON file. I would like to let jmeter to create this file before every test run. This includes:

  1. open existing JSON file
  2. update certain values in this file
  3. save and close the file
  4. run the batch file
  5. run the test

that first 4 steps should be executed just once, even I run test for more users. ( I suppose I could use extra thread for that)

I tried to use this sort of code, but it doesn't work.

import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper("cfg.json")

def taps_count = jsonSlurper.context.parameters.find { "globals" }
taps.count.value."random_taps" = 100

def period = jsonSlurper.context.parameters.find { "time_window" }
period.value."from" = "2020.12.14 08:40:00"
period.value."to" = "2020.12.14 08:45:00"



"script.bat".execute()
2

There are 2 answers

4
Dmitri T On BEST ANSWER

You need to change this line:

def jsonSlurper = new JsonSlurper("cfg.json")

to this one:

def jsonSlurper = new JsonSlurper().parse(new File("cfg.json"))

This is the very first and the most obvious error, if you experience further problems you will need to provide the contents of your cfg.json file

More information:

1
Ori Marko On

Use OS Process Sampler to execute bat file

OS Process Sampler is a sampler that can be used to execute commands on the local machine. It should allow execution of any command that can be run from the command line.