Jmeter error when trying to create file using beanshell - jmeter

This is my beanshell code to create a file and append one line to it:
FileName = vars.get("fileName");
f = new FileOutputStream(FileName,true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print("Test Set: " + FileName);
f.close();
I get fileName from a regex extractor in a previous sampler. I have checked debug postprocessor and confirmed this is set correctly. However I get this error in sampler result:
Response code: 500
Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``FileName = vars.get("fileName"); f = new FileOutputStream(FileNam . . . '' : Object constructor

The problem is: if FileName is null, the constructor for FileOutputStream will throw an exception, and BeanShell is not great in showing underlying exceptions. So what you need is to handle the case where file name is null:
String fileName = vars.get("fileName");
if( fileName == null )
{
fileName = "mydefaultname"; // assign some default name
}
f = new FileOutputStream(fileName, true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print("Test Set: " + fileName);
f.close();
If you don't want to have some default name, you can also quit the script at that point:
if( fileName == null )
{
return;
}

Related

How to replace the value inside a file that was located in local using javascript?

I have a scenario where after reading the file, it needs the value inside that file to be replaced.
We have this script from our JMeter where we based our script from. (Please refer to the code below)
def file = new File('C:/Peak2020/China/${__time(YMMdd)}-085644-336_000101-plant 8956.xml')
def newConfig = file.text.replace('596791365558876095', '000101')
file.text = newConfig
def newConfig2 = file.text.replace('C6D-CN-NBB2829A', 'C7D-CN-NBB$4568792B')
file.text = newConfig2
def sku = file.text.replace('323094-01', '45903-01')
file.text = sku
I tried doing it in the Neoload, using the replace() but it's not working. It does copy the file from sourcefolder to destinationfolder but the value was not changed. (Please refer to the code below)
var file = new java.io.BufferedReader(new java.io.FileReader("C:\\Peak2020\\China\\testSource1.xml"));
var line = file.readLine();
var id = line.replace(new RegExp("596791365558876095", "12345678"), "");
var destFile = line;
var writer = new java.io.FileWriter("C:\\Peak2020\\Teemp\\TestDestination3.xml",true);
writer.write(destFile);
writer.close();
Does anyone knows what right javascript code to use? Thank you.
String is immutable in Javascript and Java you are replacing it in
var id = line.replace(new RegExp("596791365558876095", "12345678"), "");
but then you use the var line again for the new variable.
var destFile = line;
it should be
var destFile = id;
because replace will return a new string with the replaced value.

How can I overwrite data in csv file using jmeter

I am using Jmeter for API automation, I am writing result pass or fail in CSV file.
I am unable to overwrite old result data, Every time I run the test cases it's appending with old result data.
I use Beanshell Post processor for writing in CSV file.
import java.io.file;
import org.apache.jmeter.services.FileServer;
ActualResponseCode = prev.getResponseCode();
if (vars.get("ExpectedResponse").equals(vars.get("ActualResponse")))
{
if(vars.get("ExpectedResponseCode").equals(prev.getResponseCode()))
{
prev.setSuccessful(true);
Result = "Pass";
ErrorMessage = "No Error";
}
else
{
Result = "Fail";
ErrorMessage = "ResponseCode not matching";
}
}
else
{
prev.setSuccessful(false);
Result = "Fail";
ErrorMessage = "ResponseData is not matching";
}
f = new FileOutputStream("C://Users//a622821//Desktop//apache-jmeter-3.2//API_AUTOMATION//TestResult_Post.csv", true);
p = new PrintStream(f);
p.println(vars.get("TestCase") + "," + vars.get("API_Endpoint") + "," + vars.get("ExpectedResponseCode") + "," + ActualResponseCode + "," + Result + "," + ErrorMessage);
p.close();
f.close();
Basically when you write to file you can overrid file by using boolean false as second parameter to FileWriter constructor
File file = ....
new FileWriter(file, false);
If you have several calls for beanshell I suggest create Beanshell sampler which will be called in start of the test and will override the file (create empty file):
import java.io.file;
f = new FileOutputStream("C://Users//a622821//Desktop//apache-jmeter-3.2//API_AUTOMATION//TestResult_Post.csv", false);
p = new PrintStream(f);
p.close();
f.close();
If you must use same beanshell you need a variable flag, so add variable firstTime with value true
In Beanshell use it to set the flag by firstTime variable:
import java.io.file;
import org.apache.jmeter.services.FileServer;
ActualResponseCode = prev.getResponseCode();
if (vars.get("ExpectedResponse").equals(vars.get("ActualResponse")))
{
if(vars.get("ExpectedResponseCode").equals(prev.getResponseCode()))
{
prev.setSuccessful(true);
Result = "Pass";
ErrorMessage = "No Error";
}
else
{
Result = "Fail";
ErrorMessage = "ResponseCode not matching";
}
}
else
{
prev.setSuccessful(false);
Result = "Fail";
ErrorMessage = "ResponseData is not matching";
}
firstTime = vars.get("firstTime");
flag = true;
if ("true".equals(firstTime)) {
flag = false;
vars.put("firstTime", "false");
}
f = new FileOutputStream("C://Users//a622821//Desktop//apache-jmeter-3.2//API_AUTOMATION//TestResult_Post.csv", flag);
p = new PrintStream(f);
p.println(vars.get("TestCase") + "," + vars.get("API_Endpoint") + "," + vars.get("ExpectedResponseCode") + "," + ActualResponseCode + "," + Result + "," + ErrorMessage);
p.close();
f.close();
Test Plan variable:
Don't use scripting to write anything to files, JMeter is able to store literally anything into its .jtl results file so I would recommend rather configuring it to store what you need there rather than trying to create an extra results file in such a weird manner as:
Beanshell is not the best scripting option, if you need to go for scripting consider using JSR223 Test Elements and Groovy language instead
If you are writing something into a file make sure you put aforementioned JSR223 Elements under the Critical Section Controller
So I would suggest switching to either Response Assertion or JSR223 Assertion instead of the PostProcessor. If required you can tell JMeter to store assertion failure message by adding the next line to user.properties file (located in the "bin" folder of JMeter installation, however I think it defaults to true in any case)
jmeter.save.saveservice.assertion_results_failure_message=true
Check out Scripting JMeter Assertions in Groovy - A Tutorial for more details.

Getting below error while running ruby script

I am running a test suite in Soap UI where I am trying to call one ruby script from groovy script. The step is getting executed successfully but still the script is not able to move on to the next step as it gives this error after running.
Have searched in google about this error, but found no proper resolution. Moreover the error itself is not very explanatory.
Will appreciate any kind of help.
Below is the groovy script which is calling "ap-v4-batch_DEV_QA.rb" ruby script.
This ruby script opens a browser and performs the task successfully and closes the browser. We expect the step to be marked as Passed so that it can move on to the next step, but it gives the error mentioned at the bottom.
Groovy Script:
String script = "webdriver/v4/ap-v4-batch_DEV_QA.rb";
String argv0 = com.eviware.soapui.SoapUI.globalProperties.getPropertyValue("GLOB_DefaultIP");
String argv1 = "com.wupay.batch.process.tasks.PaymentFileParsingTask_RunOnce";
String argv2 = "";
String argv3 = "";
String argv4 = "";
/* Nothing needs to be modified below */
String commandLine = "ruby " + com.eviware.soapui.SoapUI.globalProperties.getPropertyValue("GLOB_ScriptLocation") + "/" + script + " " + argv0 + " " + argv1 + " " + argv2 + " " + argv3 + " " + argv4;
log.info("Running command line: " + commandLine);
java.lang.Runtime runtime = java.lang.Runtime.getRuntime();
java.lang.Process p = runtime.exec(commandLine);
def propertyStep = testRunner.testCase.getTestStepByName("Properties");
java.io.BufferedReader stdInput =
new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
java.io.BufferedReader stdError =
new java.io.BufferedReader(new java.io.InputStreamReader(p.getErrorStream()));
String s = null;
String e = null;
StringBuffer eb = new StringBuffer();
while ((e = stdError.readLine()) != null) {
eb.append(e);
log.error("Ruby: " + e);
}
while ((s = stdInput.readLine()) != null) {
log.info("Ruby: " + s);
if(s.startsWith("#prop")) {
String[] propSplit = s.split(":", 3);
testRunner.testCase.setPropertyValue(propSplit[1], propSplit[2]);
}
}
p.waitFor();
log.info("Ruby: exit value " + p.exitValue());
if(eb.length() > 0) {
throw new Exception(eb.toString());
}
Error:
java.lang.Exception: C:/Ruby23/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:133:in require':require "watir-webdriver"is deprecated. Please, userequire "watir". java.lang.Exception: C:/Ruby23/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:133:inrequire': require "watir-webdriver" is deprecated. Please, use require "watir". error at line: 57
I have finally resolved the issue.
The issue was that ruby script was not accepting require "watir-webdriver".
I installed watir and replaced require "watir-webdriver" with require "watir".
now I am not getting the above mentioned error.
Thanks anyways!
Regards,
Faraz

JMeter asserting a response has been successfully downloaded

I am using JMeter to test some of the functionality on my site. Through using the Save Responses to a file element, I have been able to successfully issue a request to download a pdf through JMeter. However, I am curious if there is an assertion to check that a file has actually downloaded (and if possible, is in the format I specified!). I know I can simply look at the file, but I'm hoping to make this more automated. I have checked "Save Successful Responses Only," but I want to ensure a response has actually been saved.
I think you need to use a Beanshell Assertion for this.
Example code to check file presence, size and content type is below:
File file = new File("/path/to/downloaded/file");
//check file existence
if (!file.exists())
{
Failure = true;
FailureMessage = "File " + file.getName() + " does not exist";
}
//check file size
long expectedSize = SampleResult.getBodySize();
long actualSize = file.length();
if (expectedSize != actualSize)
{
Failure = true;
FailureMessage = "Actual file size differs from expected. Expected: " + expectedSize + " and got: " + actualSize ;
}
//check content type
String expectedType = SampleResult.getContentType();
String actualType = file.toURI().toURL().openConnection().getContentType();
if (!expectedType.equals(actualType))
{
Failure = true;
FailureMessage = "Response types are different. Expected: " + expectedType + " and got: " + actualType;
}
See How to Use JMeter Assertions in 3 Easy Steps guide for more information on JMeter Assertions superpower.

InputStream read is blocked while reading BLOB from Oracle column through JDBC

While trying to read from an InputStream obtained from a BLOB like below code, the InputStream goes to indefinite wait state.
I set this data from a web application running on Jboss app server and read/write works absolutely fine, problem is while running a standalone java code using plain JDBC.
Environment is JDK6, Oracle 10g.
ResultSet rs = this.stmt.executeQuery();
log.println("ResultSetType: " + (rs != null ? rs.getClass() : null));
while (rs != null && rs.next()) {
. . . // read other columns
Blob savedBlob = rs.getBlob("PERSISTENCE_BLOB");
long len = savedBlob.length();
log.println("Going to read bytes..." + len);
InputStream is = savedBlob.getBinaryStream();
log.println("IS Received...");
log.println("Available : " + is.available());
ObjectInputStream oip = new ObjectInputStream(is);
Object obj = oip.readObject();
oip.close();
is.close();
savedBlob.free();
. . .
Output is as Below...
ResultSetType: class oracle.jdbc.driver.OracleResultSetImpl
RowID: XXXXXXXXXXXXXXX // Row is selected and printed properly
Going to read bytes...6022
IS Received...
Available : 0
But if i try to read as below in chucks.. it works fine, which i don't want as i am reading a serialized object and want to have ObjectInputStream opend from InputStream.
. . .
ResultSet rs = this.stmt.executeQuery();
log.println("ResultSetType: " + (rs != null ? rs.getClass() : null));
while (rs != null && rs.next()) {
. . .
Blob savedBlob = rs.getBlob("PERSISTENCE_BLOB");
long len = savedBlob.length();
int start = 1;
int totalBytesRead = 0;
int buffSize = 2048;
byte[] byteBuff = null;
log.println("Going to read bytes..." + len);
do {
byteBuff = new byte[buffSize];
byteBuff = savedBlob.getBytes(start, buffSize);
totalBytesRead += buffSize;
log.println(start + "," + buffSize + " #BLOB bytes: " + new String(byteBuff));
start += buffSize;
. . .
} while (. . . );
log.println("Total Bytes: " + totalBytesRead);
Output:
ResultSetType: class oracle.jdbc.driver.OracleResultSetImpl
Going to read bytes...6022
1,2048 #BLOB bytes: //......bytes data..........
.....
Total Bytes: 6022
InputStream.available() doesn't indicate how much you can read, it indicates how much it can return to you (eg from a buffer), without going into a - potentially - blocking read operation.
The Javadoc also indicates:
Note that while some implementations of InputStream will return the
total number of bytes in the stream, many will not. It is never
correct to use the return value of this method to allocate a buffer
intended to hold all data in this stream.
and
The available method for class InputStream always returns 0.
So instead of using available() as any sort of indication, just read it (which clearly works as indicated by your other code).

Resources