Traversing a set of parameters under jmeter- controller
Overview
In the process of interface automation, we often encounter groups of parameters that need to be traversed. In jmeter, the foreach controller can implement traversal parameters, but there can only be one input parameter. Once encountered with an array, the foreach controller said there was nothing I could do.
In order to solve this problem, I will teach you a way to implement array traversal today.
Involved element
Json expression + Loop Controller + counter + matchNr function + beanshell script
Case analysis
First of all, let's look at an example. In the following figure, we need to write the parameters in the three sets of return values to the local file. For example, province: population
First of all, we need to extract these three sets of parameters through JSON Extractor
Looking at debug, we can see that these three sets of data all appear in pairs. {name_1,population_1}; {name_2,population_2}; {name_3,population_3}. The total is calculated by the matchNr function
Add a loop counter with the number of cycles as matchNr. It means to count several sets of data, and the script traverses it several times.
Add a counter under the loop controller to specify the number of rows to read. The maximum value is also the value calculated by matchNr
Add a beanshell script under the controller to write the read parameter traversal to the local file. Through the function nesting of the counter, traverse each set of data and write to the local file in a loop.
FileWriter fstream = new FileWriter ("D:\\ parameter table .txt", true); BufferedWriter out = new BufferedWriter (fstream); out.write (vars.get ("name_$ {num}") + "population" + ":" + vars.get ("population_$ {num}") + "ten thousand"); out.write (System.getProperty ("line.separator")); out.close (); fstream.close ()
Observe the response result
Written local file