1
votes

My requirement is to iterate over a CSV Data Set Config in Apache JMeter with a varying starting index. Let us assume I have started a test plan in JMeter today and my CSV file has 8 variables. The first time my sampler will run from 1st row to 8th row. The next time I will start running my test plan I want sampler to pick values from 2nd index to 8th index. In this manner, I want to iterate over CSV file using CSV Data set config.

I am able to initialize a counter for every test run in Apache JMeter using setUp ThreadGroup and tearDown Thread group. I am able to extract the same using _P(count) in JMeter.

In setUp Thread group I have included JSR 223 Sampler and written a script like

def file = new File('number')
if (!file.exists() || !file.canRead()) {
    number = '1'
}
else {
    number = file.text
}
props.put('number', number as String)

In tearDown Thread Group the JSR223 Sampler has a script like

def number = props.get('number') as int
number++
new File('number').text = number

I want to loop over my CSV data set config file with the counter through properties file( which is getting incremented by 1 for every test run)

1

1 Answers

0
votes

Please check the below plan:-

enter image description here

Input CSV example:-

enter image description here

If Controller has the below code:-

${__groovy(vars.get('Used').take(1)!='Y')}

In JSR223 post processor, I have the below code:-

def inputFile = new File("C:\\Path\\toFile\\Excel\\OutputCSV.csv")
def lines = inputFile.readLines()
boolean isWrite = false;

lines.each { String line ->
    if(line.contains('Used'))
    {
        inputFile.write(line + '\n')
    }
    else
    {
        if(line.startsWith('Y'))
        {
            inputFile.append(line + '\n')
        }
        else if (!isWrite)
        {
            inputFile.append('Y' + line + '\n')
            isWrite = true;
        }
        else
        {
            inputFile.append(line + '\n')
        }

    }
} 

First Run output:- enter image description here enter image description here

Second Run output:- enter image description here enter image description here

As you can see, in first run sample 1 execute 4 time and in 2nd it is executed 3 times. This is not the nicest or best code, just first try. Please check if helps.