You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by govardhan nalluri <go...@gmail.com> on 2017/09/27 14:32:35 UTC

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

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

Posted by "glinius@live.com" <gl...@live.com>.
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: user-unsubscribe@jmeter.apache.org
For additional commands, e-mail: user-help@jmeter.apache.org


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

Posted by Stuart Kenworthy <St...@bjss.com>.
How exactly are you trying to read the file?

-----Original Message-----
From: govardhan nalluri [mailto:govardhan.nalluri@gmail.com]
Sent: 27 September 2017 15:33
To: JMeter Users List <us...@jmeter.apache.org>
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.

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

Posted by ZK <st...@gmail.com>.
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);
println(fileContents);

Use a JSR223 Sampler with the language set to groovey

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

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


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

Posted by ZK <st...@gmail.com>.
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); 
println(fileContents); 

Use a JSR223 Sampler with the language set to groovey 

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

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