2013-05-22

How to read file in jmeter?

As we know previously that can read file in jmete by different config elements. In this article we are going to see functions that are used in jmeter used for the same purpose. That means, we can use the function directly reading file without any component.

We can read file in 4 ways. They are

1. String from File :
It is used for reading a string form a text file. This function is same as CSV Data Set config element. It does not supports multiple files.
-Each time function call, it reads the next line
-All thread share the same function, so during execution, different thread will get different line sequentially.
-At the EOF, it will start from begin of the file(a maximum loop can be set)
- If there is an multiple reference in single thread, it will open the file separately(even we use different file)
-It is best practice to use different variable names for each function call.
-Reference variable is resolved(initiated) each time function call
- It returns ERR if there is an error opening file. 
-This is very useful when test required a lot of variable.

 ${__StringFromFile(D:\Shantonu.txt,myVeriable,,)}

-It has 4 parameter, File name(mandatory, full path), Variable name , Start Sequence Number & End sequence number as integer. Sequence number is used for defining reading file pattern.


Note for using Sequence number: 
-If sequence numbers are used, the path name is used as the format string(not file path) for java.text.DecimalFormat 
-# or 0[any digit] is used as for representing as Number
-"." is a formatting character so it is enclosed in single quotes like '.'
-If the path name does not contain any special formatting characters, the current sequence number will be appended to the name, otherwise the number will be inserted according to the formatting instructions. Example :  if we use
${__StringFromFile(Shantonu#'.'txt,myVeriable,1,2)}
It will read Shantonu1.txt and Shantonu2.txt. Like as start with number 1 to 2.

-If the start sequence number is omitted, and the end sequence number is specified, the sequence number is interpreted as a loop count, and the file will be used at most "end" times. In this case the file name is not formatted . Example :  if we use
${__StringFromFile(Shantonu.txt,myVeriable,,2)}
It will read Shantonu.txt twice. That means end sequence 2 is the loop count of reading file.

2. File to String :
It is used to read entire file at a time. It returns ERR if there is an error opening file. 

${__FileToString(D:\Shantonu.txt,ASCII,${userName})}

-It has 3 parameter, file name(mandatory), Encoding (to read file) and variable name(where to put value.
-File name is the full path to file.
-Encode depend on what type of file we use(like ASCII). If blank, it use platform default(os)
-We can use new variable by assigning a new variable(like myVer) or we can use our stored variable and use this(like ${userName}). In case of stored variable, the value will be assigned by this function.
-The file name, encoding and reference name parameters are resolved on each time function called


3. CSV file Reading :
This function is same as CSV Data Set config element. But, it supports multiple file reading(using . *alias)
${__CSVRead(D:\Shantonu.csv,0)}
-It returns a string from a CSV file. From starting, it read full file in to internal array and if it found blank, it assumes as end of file.[So, we can comment at the end] 
-This function has 2 parameter, File path and, column number.
-File parameter (path) is case secretive. Ex- if shantonu.csv and Shantonu.csv are given, it will operate separately creating separate internal array.

-Each thread has a internal pointer to its current row in the file array. When a thread first refers to the file it will be allocated the next free row in the array, so each thread will access a different row from all other threads. [Unless there are more threads than there are rows in the array.]. That means , if we run 5 threads, and if we have 5 lines in CSV file, each thread will get one after another sequentially. Not that, every one will get all 5 lines.

-The function splits the line at every comma by default. If we want to enter columns containing commas, then you will need to change the delimiter to a character by setting the property: csvread.delimiter
-The second parameter takes either column number as integer or "next". Next will go to next line of file.
-We can use this function more than one time in same variable like
${__CSVRead(D:\Shantonu.csv,0,D:\Shantonu.csv,next)}
-As this file is stored in memory, so better not to use this to save memory.

4. Xpath matching with XML file :

It reads XML file and matches XPath each time the function called. After reaching to end of the file, it will start again.

${__XPath(D:\ShantonuTest.XML,.//*[@id='birthday_reminders_link']/div/div/div/span/strong)}

-It has two parameter,  first one is local path of xml file with name and 2nd one is the xpath to match. Both are mandatory fields.
-It returns the next match Xpath(if it found matching)
-It returns Empty string if no matches with warning message to jmeter log 

-It takes a lot of memory as all Node List stays in memory

Thanks...:)

6 comments:

  1. There is one, more flexible, way to read file(s) using JMeter: Beanshell Scripting. It gives all the power of JMeter API, associated Apache libraries and Java SDK so there are no limitations at all.

    ReplyDelete
    Replies
    1. writing file with Beanshell is not direct writing, it is discussed in beanshell section

      Delete
  2. Please someone provide me solution for the problem
    I have a web application,i wrote web driver script for jmeter firefox driver
    now as like in java read the text file and want to store the string in variable and want to use that variable as sendkeys for username and password.please someone give example too

    ReplyDelete
    Replies
    1. I can help you in weekend.Please mail me detail with your code, lets debug before solve.

      Delete
  3. scenario : im sending a POST request in jmeter it has json payload . ihave saved this json payload in a txt fil now i ahve fuction

    ${__eval(${__FileToString(${input_json})})}; in body of http request running my test i recive MAlformed json request error for POST .
    input json is column in csv file and i have give the path of my json .txt file . ?

    ReplyDelete