Interview Questions

How do I use external data files to define variables in my Test scripts?

Apache JMeter FAQ

(Continued from previous question...)

How do I use external data files to define variables in my Test scripts?

The CSV Data Set Config element is the best way to do this, as it can create multiple variables from a single data file.

Answer:Another way to do this is to create a User Parameters Pre-Processor in which you list all the values that you want to read from files. You can then use the variable names later in the script.

For example:
1. Start JMeter

2. Add a Thread Group to the Test Plan
1. set the appropriate number of threads and iterations

Add Pre Processors > User Parameters to Thread Group

4. Click Add Variable
1. Set Update once per iteration
2. Set the Name to the name of the variable (e.g. ACCOUNTID)
3. Set the value (under User_1) to ${_StringFromFile(accounts.dat)}

5. Add Sampler > HTTP Request to Thread Group:

6. Click the Add button to add a parameter to the request:
1. Name: account_id
2. Value: ${ACCOUNTID}

7. Add Listener > View Results Tree to Thread Group

8. Save

9. create the file accounts.dat containing one line per account id. If it is not in the bin directory, then modify the StringFromFile parameter accordingly, e.g. ${_StringFromFile(../testdata/accounts.dat)} or ${_StringFromFile(/home/user/testdata/accounts.dat)} or ${_StringFromFile(C:/work/data/accounts.dat)}

10. Run > Start

Each iteration, the ACCOUNTID variable will be set to the next line in the file, and the HTTP Request will use its value to set the account_id parameter.

When the end of the file is reached, StringFromFile starts reading again at the beginning.

N.B. If using such a script in client-server mode, make sure that any data files are copied to the appropriate place on the server host, as the files will be opened by the server process, not the client.

(Continued on next question...)

Other Interview Questions