Book Image

JMeter Cookbook

By : Bayo Erinle
Book Image

JMeter Cookbook

By: Bayo Erinle

Overview of this book

Table of Contents (16 chapters)
JMeter Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Feeding data into a script


More often than not, we will need to provide varying input datasets to our scripts to simulate realistic user interaction with an application. Most applications, for instance, need to be authenticated against to view certain restrictive areas of the application. The way to accomplish this is by supplying an input datafile to the test script. The file is normally in the form of comma-separated values (CSV).

How to do it…

This recipe shows you how to feed data into your script to handle such scenarios. Perform the following steps:

  1. Download the book code samples from http://www.packtpub.com/jmeter-cookbook/book.

    Note

    Alternatively, you can clone the sample from the GitHub repository at http://github.com/jmeter-cookbook/bundled-code.

  2. Extract the contents of the ZIP file.

  3. Launch JMeter.

  4. Open the ch1_feed_me_data.jmx script from the scripts/ch1 directory.

  5. Add CSV Data Set Config to the test plan by navigating to Test Plan | Add | Config Element | CSV Data Set Config. Let's configure it:

    1. In the Filename box, enter input.txt.

    2. Leave the rest of the entries blank.

  6. Save and run the test. The execution of the script is shown in the following screenshot:

How it works…

Assuming you haven't changed the default sharing mode of the added CSV dataset configuration, with each iteration run of our test plan, a line of the input.txt file is consumed. The consumed values are then turned into JMeter variables named after the first line of our input datafile, which are user and pass in our case. The values are then used further down the execution chain to fill in the username and password to access our application. In this particular test plan, as we have more threads than the amount of input data, the input data is recycled once the end of the file is reached.

There's more…

In our example, we have just provided the name of the file to read the input feed from. If you examine the CSV dataset configuration element closely, you will notice that it has other properties that can be filled in. These include the following:

  • Variable Names: Provide the comma-separated names of the variables. As we left this blank, the first line of our input script will be treated as the variable name.

  • Delimeter: Change to this option if you want to use a different delimiter other than a comma. For instance, you can use tab spaces (\t) or the colon symbol (:) instead of commas.

  • Allow Quoted Data: Set to True, if you would like to allow quoted data. For example, 'hello' as opposed to hello.

  • Recycle on EOF: In cases where you have more threads than the input data, this attribute specifies whether to allow the input data to be recycled. In such a case, the input feed will start from the beginning of the file once the data feed has run out.

  • Stop thread on EOF: This option determines whether to kill the thread has been reached.

  • Sharing mode: The default option is All meaning all threads; even those in a different thread group get to share the same data. The other options are Current thread group and Current thread, which specify that only the threads within a particular thread group and only a particular thread can retrieve data from the file, respectively.