0

Here i am facing One problem that. i want to read the csv file data and one by one put it into a variable and that variable i want to assign a next Http Request let say ids.csv file which consisting of values like this

23333
23334
23335
22336
23336

I am using Jsr223 PreProcessor code:

def csvfile = new File('D:/datas/id/ids.csv')
def lines =csvfile.readLines()
lines.each { String line ->
  vars.put('Id_value', line.toString())
}

If it is wrong, how to do this with simple code?

2
  • You are override the value each iteration, how will you get all values in 1 variable? Commented Dec 28, 2017 at 13:39
  • here Id_values i want to send it to next http request so on that time it will become empty it will take next iteration Commented Dec 28, 2017 at 13:43

2 Answers 2

2

You can emulate JMeter CSV data set which add variables with different suffixes, example in tutorial:

String Filename = vars.get("GETfile");
String fileContents = new File(Filename).getText('UTF-8');
def lines = 0  ;
    fileContents.eachLine { line ->
        lines++;
        vars.put("CSVLine_" + lines, line);
    }
vars.put("GETfileLength",lines.toString()) ; 

and then loop the variables using ForEach Controller:

Sign up to request clarification or add additional context in comments.

Comments

2

You need to add a some form of counter to your JMeter Variables reference names, your current code will create only one Id_value variable with the value of 23336, you need to amend it like:

def csvfile = new File('D:/datas/id/ids.csv')
def lines =csvfile.readLines()
lines.eachWithIndex {line, idx ->
    vars.put('Id_value_' + idx, line)
}

And you will get:

Id_value_0=23333
Id_value_1=23334
Id_value_2=23335
Id_value_3=22336
Id_value_4=23336

More information:

1 Comment

i just paste the above code and the variable is not passing inside the request it is not taking the values inside the Id_value_ but it is only taking ${Id_value_}. -Http Request --jsr223 preprocessor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.