Transferring my dynamic value from response data to excel file in Jmeter - jmeter

I want to actually transfer my dynamic value from the response data to Excel file in Jmeter... can anyone plz let me know the clear process for it ?
I used beanshell post processor but dint got the expected output...

Take a look at Apache POI - Java API To Access Microsoft Excel Format Files, this way you will be able to create, read and update Excel files from Beanshell code. The easiest way to add binary documents formats support to JMeter is using Apache Tika, given you have tika-app.jar in JMeter Classpath you will be able to view Excel files contents using View Results Tree listener and use Apache POI API to manipulate Excel files.
Minimal working code for creating an Excel file and adding to it JMeter variable value looks like:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(vars.get("your_variable"));
FileOutputStream fileOut = new FileOutputStream("FileCreatedByJMeter.xlsx");
wb.write(fileOut);
fileOut.close();
References:
Busy Developers' Guide to HSSF and XSSF Features
How to Extract Data From Files With JMeter

Related

Save extracted variable value in a pdf file using JSR223 PostProcessor

How to save the extracted variable value(text/content) in a pdf file using JSR223 PostProcessor in JMeter??
let say variable name ${data} .
Please suggest some solutions.
You will need a library like PDFBox for this
Add it and all its dependencies to JMeter Classpath
Restart JMeter to pick the .jars up
The simplest code to write a JMeter Variable into a PDF file would be something like:
def document = new org.apache.pdfbox.pdmodel.PDDocument()
def page = new org.apache.pdfbox.pdmodel.PDPage()
document.addPage(page)
def font = org.apache.pdfbox.pdmodel.font.PDType1Font.HELVETICA_BOLD
// Start a new content stream which will "hold" the to be created content
def contentStream = new org.apache.pdfbox.pdmodel.PDPageContentStream(document, page)
// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText()
contentStream.setFont(font, 12)
contentStream.moveTextPositionByAmount(100, 700)
contentStream.drawString(vars.get('data'))
contentStream.endText()
// Make sure that the content stream is closed:
contentStream.close()
// Save the results and ensure that the document is properly closed:
document.save('path-to-your-file.pdf')
document.close()
More information:
Apache Groovy: What Is Groovy Used For?
Apache PDFBox Cookbook - Document Creation

Jmeter Mongo Db insert script is not adding iterations from csv data set

CSV set as below
enter image description here
Jmeter insert mongo DB script is as below
import com.mongodb.*
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;
try {
MongoCollection<Document> collection = vars.getObject("collection");
Document document = new Document("_id", "${_id}")
.append("has_mortgages",false)
.append("data", new Document ("etag":"${_etag}")
.append("links", new Document ("charges","/company/${_id}/charges"))
);
collection.insertOne(document);
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}
Thread group - no.of threads is 1 and loop count is 3
While running the script the first iteration is picking the value of _id as PT000001 and inserting the record. While
second iteration is picking up the _id correctly from the csv which is PT000002 but the
collection.insertOne(document); is still adding the _id PT000001
Can some one please tell me what is wrong with my script??
Take a look at JSR223 Sampler documentation:
The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:
Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
Or Use Script Text and check Cache compiled script if available property.
When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.
so you need to change "${_id}" to vars.get('_id') and "${_etag}" to vars.get('_etag') and your script will start working as expected.
vars stands for JMeterVariables class instance, see the JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy for more information on this and other JMeter API shorthands available for the JSR223 Test Elements.
Resolved after unchecking the below check from sampler
Cache compiled script if available

PyArrow get Metadata from file in S3

I want to get Parquet file statistics (such as Min/Max) from file in S3 using PyArrow.
I am able to fetch it using
pq.ParquetDataset(s3_path, filesystem=s3)
and get the statistics if I download and read it using:
ParquetFile(full_path).metadata.row_group(0).column(col_idx).statistics
hope there is a way to achieve it without download the whole file.
Thanks
I came to this post looking for a similar answer few days ago. In the end I found a simple solution that works for me.
import pyarrow.parquet as pq
from pyarrow import fs
s3_files = fs.S3FileSystem(access_key) # whatever need to connect to s3
# fetch the dataset
dataset = pq.ParquetDataset(s3_path, filesystem=s3_files)
metadata = {}
for fragment in dataset.fragments:
meta = fragment.metadata
metadata[fragment.path] = meta
print(meta)
The metadata is store in the dictionary where the keys are the path to the fragment in the s3 and the values is the metadata of that particular fragment.
to acces the statistics just use
meta.row_group(0).column(col_idx).statistics
something like this will be printed for every fragment
<pyarrow._parquet.FileMetaData object at 0x7fb5a045b5e0>
created_by: parquet-cpp-arrow version 8.0.0
num_columns: 6
num_rows: 10
num_row_groups: 1
format_version: 1.0
serialized_size: 3673

Jmeter - how to write specific variable to CSV file to specific row/column

My csv file looks like:
TC_name, username, password, excpecedCode
ad_test_master_successful_login,username,Test123!,200
What is the easiest way to so i can write to csv to specific row/column, and example to overwrite Test123! with variable fetched from user defined variables?
I know i can read value using JSR223 Pre/post processor with ex:
def line10 = new File('C:/Users/test/Desktop/testData/login.csv').readLines().get(1).split(",")[2]
log.warn("csv as-> " + line10);
There is no such concept as "cell" in CSV files, if you're looking for the code which will replace one string with another, you can do something like:
def file = new File('test.csv')
def text = file.text.replaceAll('Test123!', vars.get('foo'))
file.text = text
If you're looking for a better option you can consider using GroovyCSV library (you will need to download it and place in JMeter Classpath followed by JMeter restart) or consider switching to Excel file where you will have full control via Apache POI library like it's described in How to Implement Data Driven Testing in your JMeter Test article

Writing web service response to excel

I am trying to wrote the response of a restful service response to excel.
In the below once, if my test case response is below one, then i need to write to csv or excel for sheet1 (in excel) TC01, sampleResponse
<user-batch-result xmlns="http://www.xxxxxx.com/api//02" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<records-succeeded>1</records-succeeded>
<records-failed>0</records-failed>
<UsersDetails>
<UserInfo>
<EmployeeID>xxxxx</EmployeeID>
<FeedRecordNumber>0</FeedRecordNumber>
<Status>SUCCESS</Status>
</UserInfo>
</UsersDetails>
You can do something like:
Download tika-app.jar and drop it somewhere to JMeter Classpath (i.e. to "lib" folder of your JMeter installation). JMeter restart will be required to pick the .jar up.
Add JSR223 PostProcessor as a child of the request which returns the above response
Put the following code into "Script" area:
def wb = new org.apache.poi.hssf.usermodel.HSSFWorkbook()
def sheet1 = wb.createSheet("sheet1")
def row = sheet1.createRow(0)
def A1 = row.createCell(0, org.apache.poi.ss.usermodel.CellType.STRING)
A1.setCellValue(prev.getResponseDataAsString())
wb.write(new File('myFile.xlsx'))
Run your test.
If everything goes well you should see myFile.xlsx having Sheet1 and the response of your Web Service as the very first cell value. Feel free to amend this code as required according to your use case.
References:
Busy Developers' Guide to HSSF and XSSF Features
How to Implement Data Driven Testing in your JMeter Test

Resources