How to read full data from a txt file by using Beanshell script

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to read full data from a txt file by using Beanshell script

govardhan nalluri
HI Jmeter Supporter,

I have txt file and able to read only single(First) line but I need to read
at time all data and need to save as variable and send as request.

Thanks,

Govardhan N
Reply | Threaded
Open this post in threaded view
|

RE: How to read full data from a txt file by using Beanshell script

Stuart Kenworthy
How exactly are you trying to read the file?

-----Original Message-----
From: govardhan nalluri [mailto:[hidden email]]
Sent: 27 September 2017 15:33
To: JMeter Users List <[hidden email]>
Subject: How to read full data from a txt file by using Beanshell script

HI Jmeter Supporter,

I have txt file and able to read only single(First) line but I need to read at time all data and need to save as variable and send as request.

Thanks,

Govardhan N

The information included in this email and any files transmitted with it may contain information that is confidential and it must not be used by, or its contents or attachments copied or disclosed to, persons other than the intended addressee. If you have received this email in error, please notify BJSS. In the absence of written agreement to the contrary BJSS' relevant standard terms of contract for any work to be undertaken will apply. Please carry out virus or such other checks as you consider appropriate in respect of this email. BJSS does not accept responsibility for any adverse effect upon your system or data in relation to this email or any files transmitted with it. BJSS Limited, a company registered in England and Wales (Company Number 2777575), VAT Registration Number 613295452, Registered Office Address, First Floor, Coronet House, Queen Street, Leeds, LS1 2TW.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
ZK
Reply | Threaded
Open this post in threaded view
|

Re: How to read full data from a txt file by using Beanshell script

ZK
This post was updated on .
In reply to this post by govardhan nalluri
Hi,

this would do it:

import org.apache.jmeter.util.JMeterUtils;
String fileContents = new File('./nameOfFileToRead.txt').getText('UTF-8');
vars.put("your_fileContentsVariableName",fileContents);

Use a JSR223 Sampler with the language set to groovy

This script assumes the 'nameOfFileToRead.txt' is in your jmeter bin folder



Or use:
https://jmeter.apache.org/usermanual/functions.html#__FileToString



ZK



--
Sent from: http://www.jmeter-archive.org/JMeter-User-f512775.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
For additional commands, e-mail: user-help@jmeter.apache.org

Reply | Threaded
Open this post in threaded view
|

Re: How to read full data from a txt file by using Beanshell script

glinius@live.com
In reply to this post by govardhan nalluri
It is recommended to avoid scripting where possible, you should be able to
read the file into a JMeter Variable or directly into the request body via
__FileToString() function
<https://jmeter.apache.org/usermanual/functions.html#__FileToString>   like:

Read file contents:


> ${__FileToString(/path/to/your/file.txt,,)}

Read file contents and store it into *myVar* variable:


> ${__FileToString(/path/to/your/file.txt,,myVar)}

If you still want to go for scripting be aware that since JMeter 3.1  users
are encouraged to use JSR223 Test Elements and Groovy language
<https://www.blazemeter.com/blog/groovy-new-black>  .  

Here is the code which will store file's contents into *myVar* JMeter
Variable


> import org.apache.commons.io.FileUtils
> import java.nio.charset.StandardCharsets
>
> vars.put('myVar', FileUtils.readFileToString(new
> File('/path/to/your/file.txt'),StandardCharsets.UTF_8));

It will work for both Beanshell and JSR223 Elements.



--
Sent from: http://www.jmeter-archive.org/JMeter-User-f512775.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]