JMeter after load XML files from a directory how to pass variables to the each XML body? - jmeter

let say I have multiple XLMs files in folder and use jp#gc Directory Listing upload the files. so how do I pass variables to body xmls?
<Address>
<Address1>${address}</Address1>
<City>${city}</City>
<State>${state}</State>
<ZipOrPostalCode>${zip}</ZipOrPostalCode>
<Country>USA</Country>
</Address>
I tried to use jsr223- preprocessor but not success

Use __FileToString() and __eval() functions combination like:
${__eval(${__FileToString(${filename},,)})}
where filename is the variable from the Directory Listing Config
See Apache JMeter Functions - An Introduction article for more information on JMeter Functions concept

Related

JMeterLibException: 'Incorrect log file format'

JMeter 2.12
python 2.7.18
I'm getting JMeterLibException: 'Incorrect log file format' for the below code using robot framework
${logPath}= set variable C:/Tests/output1.log
run jmeter C:/Tests/apache-jmeter-5.4.1/bin/jmeter.bat C:/Tests/100users.jmx ${logPath}
${result} analyse jtl convert ${logPath}
log ${result}
FOR ${ELEMENT} IN #{result}
log dictionary ${ELEMENT}
It seems that your "JMeterLib" (whatever it is) expects some specific JMeter .jtl file format and JMeter 5.4.1 default settings are not compatible with it.
Unfortunately we cannot really help you without seeing this "JMeterLib" output or source code so I can only provide a generic piece of advice:
JMeter .jtl file output is controllable via special set of properties responsible for Results file configuration, once you determine what exact metrics does your "JMeterLib" expect you can amend JMeter's configuration accordingly in order to make it compatible with your 3rd-party software assumptions.
More information:
Configuring JMeter
Apache JMeter Properties Customization Guide
You should use jtl file extension instead of log
${logPath}=set variableD:/Tests/output1.jtl

Getting error while running merged jtl files

I am trying to generate the report in JMeter by merging .jtl files content getting below error -
File '/home/ajij/jmeter_tests_cli/merged.jtl' does not contain the field names header, ensure the jmeter.save.saveservice.* properties are the same as when the CSV file was created or the file may be read incorrectly when generating report
An error occurred: Error while processing samples: Consumer failed with message :Could not parse timeStamp <timeStamp> using format defined by property jmeter.save.saveservice.timestamp_format=ms on sample timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
Note -
If i run 2 independent jtl files then will get run successfully
Command to execute JTL file -
../Documents/apache-jmeter-5.2/bin/jmeter.sh -g merged.jtl -o ./folder
JTL File content -
https://drive.google.com/file/d/1j6kZ7mUj0IbT6hWS0KR3BsZVK4t6wQzj/view?usp=sharing
Quick Help will be appreciated !!!
As per JMeter's documentation:
The dashboard generator is a modular extension of JMeter. Its default behavior is to read and process samples from CSV files to generate HTML files containing graph views. It can generate the report at end of a load test or on demand.
As of current latest stable version JMeter 5.3 generating dashboards from .jtl files in XML format is not supported so either re-run your test with the following JMeter property defined:
jmeter.save.saveservice.output_format=csv
or review your way of "merging" the result files, if it supports CSV output format - go for it.
Did you try removing the XML tags? I haven't tested yet using the CSV you shared, but it might be enough removing the XML tags and merging "manually" the files, I'd merge the files by removing the second headers and the XML tags, remember JMeter process the requests by time so it will order the requests by timestamp

Unable to pass arguments to a .jmx files from build.gradle file through jmRun

i am facing multiple problems with my .jmx file.
I am working with bzm-concurrency thread group
1) it is not recognising the standard format of passing the user defined values
like: ${__P(SERVER_NAME,localhost)} instead of passing "localhost" to my script it is appending "${__P(SERVER_NAME,localhost)}" to my url.
After passing only the value i.e (localhost) it is working fine
2) Also, iam running my jmeter script from build.gradle file and iam unable to pass the url and other details from them
|SERVER_NAME|${__P(SERVER_NAME,localhost)}| --> this code is not working
|SERVER_NAME|localhost| --> this code is working fine
jmeter {
jmTestFiles = [file("src/main/Jmetrscript.jmx")]
jmSystemPropertiesFiles= [file(path of my properties file)] //to add additional system properties
jmUserProperties = ["SERVER_NAME ="+System.getProperty('SERVER_NAME','localhost'), "SERVER_PORT ="+System.getProperty('SERVER_PORT','9080') ]
enableExtendedReports = true //produce Graphical and CSV reports
}
when i run this using gradlew jmRun -DSERVER_NAME=localhost it is not passing the value.
As per JMeter Gradle Plugin - Advanced Usage
Supported JMeter commandline arguments (see full list here):
-p, --propfile {argument}, the jmeter property file to use
-q, --addprop {argument}, additional property file(s)
-t, --testfile {argument}, the jmeter test(.jmx) file to run
-J, --jmeterproperty {argument}={value}, Define additional JMeter properties
-D, --systemproperty {argument}={value}, Define additional System properties
-S, --systemPropertyFile {filename}, a property file to be added as System properties
-r, --runremote (non-GUI only), Start remote servers (as defined by the jmeter property remote_hosts)
-G, --globalproperty (argument)[=(value)], Define Global properties (sent to servers), e.g. -Gport=123, or -Gglobal.properties
So you should pass JMeter Properties via -J command-line argument like:
jmRun -JSERVER_NAME=localhost etc
References:
Overriding Properties Via The Command Line
Apache JMeter Properties Customization Guide

How to get the absolute folder location of a Jmeter .jmx project file from within itself?

I have a Jmeter project that is executed by Maven and is able to locate external Beanshell scripts by the path src/test/jmeter/external-scripts-dir/script1.bsh , but when I run Jmeter directly in the GUI on my computer the relative location doesn't work and the tests cannot be ran standalone. This forces me to run Jmeter from Maven.
So, I have a project file located at a Maven layout location like C:\files\git\projectA\src\test\jmeter\Project.jmx but since I ran jmeter from its installation folder at C:\Jmeter2.12 , it cannot find the relative location of the external script I mentioned earlier.
To solve this, all I need is to set a variable to the directory containing the .jmx file. Is there any possible way to do this?
I can dynamically determine the home of Jmeter ( C:\Jmeter2.12 ) pretty easily (using the following code) but that doesn't help me get the location of the project file.
${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer
.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}
Is there something similar to the above code that would allow me to deduce the project file location?
If you're looking for the way to locate current script when you run JMeter in GUI mode you can try the following Beanshell expression:
${__BeanShell(import org.apache.jmeter.gui.GuiPackage;GuiPackage.getInstance().getTestPlanFile();)}
If you brake this down into 5 lines there will be:
import org.apache.jmeter.gui.GuiPackage;
import org.apache.commons.io.FilenameUtils;
String testPlanFile = GuiPackage.getInstance().getTestPlanFile();
String testPlanFileDir = FilenameUtils.getFullPathNoEndSeparator(testPlanFile);
vars.put("testPlanFileDir", testPlanFileDir);
log.info("testPlanFileDir:" + testPlanFileDir);
Your current .jmx file fill be stored as scriptFile JMeter Variable.
References:
GuiPackage class JavaDoc
How to use BeanShell: JMeter's favorite built-in component guide
The solution was (thanks to Ardesco) to do the following:
Use the following variables, set in the Test Plan global variables:
projectHome = ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}
jmeterHome = ${__BeanShell(System.getProperty("user.dir");)}
scriptHome = ${projectHome}/scripts
Thanks to the accepted answer, I was able to solve a related problem.
Now, I can calculate this parameter once and re-use it as a user variable.
JMETER_SCRIPTS_DIRECTORY=${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}
Its usage is ${JMETER_SCRIPTS_DIRECTORY}.
Interestingly, it works in both GUI mode and NON GUI mode (from command line).

Jmeter variable filepath is not resolved in FileToString function

I am using:
${__eval( ${__FileToString(${filePath}\x.xml)})}
to get an XML String from a file. The variable filePath is not resolved and I'm always getting the error "FileNotFound exception". If the absolute path is given, it works. It seems the variable is not resolved before the FileToString function call. Is there a way to use a relative path?
JMeter uses FileServer class to determine it's base directory.
If you look into JMeter log you'll be able to see something like:
2014/07/14 15:32:12 INFO - jmeter.services.FileServer: Default base='/opt/jmeter/bin'
In my case it's /opt/jmeter/bin
So if I drop x.xml file into /opt/jmeter/bin folder it would be resolved via ${__FileToString(x.xml,,)} function.
There is a property which can control base directory for relative paths resolution:
# Prefix used to identify filenames that are relative to the current base
#jmeter.save.saveservice.base_prefix=~/
You can set it to any other folder and JMeter will use it as the base for paths resolution.
The property can be changed in:
jmeter.properties file
user.properties file
overriden via -J command-line argument as
jmeter -Jjmeter.save.saveservice.base_prefix=c:/xml_payload/
jmeter.properties and user.properties files live under /bin folder of your JMeter installation, FileServer base is usually the same folder.
See Apache JMeter Properties Customization Guide for more details on JMeter tuning using properties.

Resources