In this tutorial, you will learn to use JMeter PreProcessor and how do PreProcessors like SampleTimeOut, UserParameter, HTMLLinkParser, etc work:
PreProcessor always executes the action before the Sampler request.
Below is the list of PreProcessors being used in JMeter:
- User Parameters
- Sample Timeout
- JSR223 Preprocessor
- JDBC Preprocessor
- Bean Shell Preprocessor
- HTML Link Parser
- HTML URL Re-writing Modifier
- RegEx User Parameters
=> Click here for The Complete Free Training On JMeter (20+ Videos)
Table of Contents:
JMeter PreProcessor Video Tutorial
PreProcessors are actions that are performed before your actual sampler runs in JMeter. It could be something like storing values of a variable, defining timeouts, etc. The preprocessor can be added at the thread group level or sampler level.
SampleTimeOut PreProcessor can define the sample time out for a request. Any sample taking time which is more than the defined one would be failed. UserParameter PreProcessor is used to define values for variables for specific users.
Example, you could define a value for a variable for specific users and when the sampler is run, values specified in this preprocessor are picked up.
HTMLLinkParser parses all the links from the response of the previous request and then you can hit any of the links on the page randomly. You can specify “ .*” in your path and it would pick any random link present on the page and would hit that with the same protocol used in a specific protocol.
Commonly Used JMeter PreProcessor
Let’s go through a few of the pre-processors of JMeter:
#1) JSR223 Preprocessor
JSR223 Pre-processor performs before the sampler request and runs the operation using the JSR223 script. Several languages that can be used are JAVA, Groovy, BeanShell, JEXL, etc.
Follow the steps below to add this Pre-processor:
- Add Thread Group in a test plan.
- Add Sampler “HTTP Request”.
- Add JSR223 Pre-processor.
- Add Listener “View Result Tree”.
Example:
The following script can be written using JavaScript for modifying the HTTP Request Sampler:
sampler.setDomain("website.com"); sampler.setProtocol("HTTP"); sampler.setMethod("GET");
JSR223 variables available for the use of script are as follows:
- Log: This variable can be used to write to the log file.
- Parameters: Parameters as a String.
- Label: It is a string label.
- FileName: File Name of a script if in case any external script is added
- Vars: This is a JMeter variable that gives the read and write access to the variables.
- Sampler: Access to the current sampler is provided.
- args: String array as a parameter
- props: JMeter properties
- ctx: This is JMeter context which provides access to the context
- OUT: To print the message (system.out)
#2) JDBC Preprocessor
This processor can be used to run SQL queries before the sampler executes.
To add the Pre-processor below steps are followed:
- Add Thread Group to the Test plan.
- Add Sampler “HTTP Request”.
- Add HTTP Header Manager.
- ADD JDBC PreProcessor to the thread group.
- Add Listener as “View Results Tree”.
Example:
In case a user wants to retrieve username and password for users:
- Add thread group to the Test Plan.
- Add Sampler “HTTP Request” (details of required data username and password can also be provided in the body part of the sampler, but it will not serve the purpose, as updating in any of these details will have to be updated in the script as well)
- Add config element “HTTP Header Manager” and provide details for the name and value.
- Add JDBC Preprocessor.
Query type can be selected from the drop-down as Select Statement and Query can be specified to extract the same in Query box.
Variables to be extracted can be provided with comma separation.
- Add Listener “View Results Tree” and run the test plan to view the results.
JDBC Pre-processor has field Query type that can be selected as per the requirement.
As in case the user wants to update the details, Update Statement will have to be used. Similarly, as required, another query type can be selected.
Example of Prepared Select Statement:
In case the user has to cancel the Appointment ID of a patient from the records referring to the name of the patient, the user needs to configure the JDBC connection and then have to add a JDBC preprocessor selecting the query type as Prepared Select Statement. Run the test plan. Results can be seen by the added listener.
#3) HTML Link Parser
This processor is used to parse the HTML response and to extract all the links and forms.
Follow the steps below to add the Pre-processor:
- Add thread group to the test plan.
- Add Sampler “HTTP Request”
- Add Simple controller.
- Add another sampler “HTTP Request”.
- Add HTML Link Parser preprocessor.
- Add listener “View Results Tree”.
Example:
To view how the internal pages can be crawled, below steps can be performed:
- In Thread Group, add Sampler “HTTP Request” and provide the server name or IP. Rename the sampler name as HTTP Request_Main.
- Add Simple Controller to the test plan.
- Add one more sampler “HTTP Request” and provide server name or IP and also provide /. * in the path so that it can crawl to all the links found. Rename the sampler name as HTTP Request_Internal.
- Add preprocessor “HMTL Link Parser” and listener as “View Result Tree” to view the output.
By following the above steps, output for the first sampler will be the website name in request field whereas for the second sampler, output would be the internal pages crawled in the provided website. Refer the below screen shots for the same.
#4) HTTP URL Re-writing Modifier
To manage the session information URL, re-writing technique is used. It has an advantage that even if the cookie is disabled on any system, it still has the potential to work.
To add the Pre-processor below steps are followed:
- Add the Thread Group to the Test Plan.
- Add HTTP Request to the test plan.
- Add preprocessor “HTTP URL Re-writing Modifier” to the Test Plan.
- Add one more Sampler “HTTP Request” to the thread.
We can use this preprocessor as follows:
This preprocessor can be applied to the thread-level and to the specific sampler as well. If applied at the thread level, it will add the argument for every request which is there in the thread.
Session Argument Name: Session ID parameter name has to be specified in this field. Parameter name can be found in response. This parameter will be searched on the page and wherever it is found, its assigned value will be taken.
Path Extension: In case the application rewrites the URL by adding a semicolon and also includes the session ID, this option should be selected.
Do not use equals in path extension: In case the application rewrite URLs without the “=” sign between parameter name and value, this option should be checked.
Do not use a question mark in path extension – If the application is required to stop the query string from closing or ending in the path extension, the option should be checked.
Cache Session ID: In case the session ID is required for the later use, this option should be selected.
URL Encode: In case encoded URLs are required, this option should be selected.
#5) RegEx User Parameters
This preprocessor lets the dynamic value for HTTP parameters to be retrieved from HTTP request with the regular expression extractor. It is set to independent threads in a test plan.
To add the Pre-processor below steps are followed:
- Add a thread group in a test plan.
- Add HTTP request to a thread group with a server name or IP.
- Add Postprocessor “Regular Expression Extractor” to extract values.
- Add another HTTP request to a thread group and provide server name or IP.
- Add “Regex User Parameter” preprocessor to the second HTTP Request.
This preprocessor can be used as follows:
Regular Expression Reference Name: The reference name specified will have the variables which must have been parsed from the first HTTP request.
Parameter names regexp group number: 1 represents that group 1 of the regexp . It will return the names of the variables.
Parameter values regex group number: 2 represents that group 2 of the regexp. It will return the values of the variables.
#6) BeanShell Preprocessor
This preprocessor gets a random value from a specific list. It can generate random strings to assign values to the variables.
BeanShell Preprocessor is added under the sampler.
To add the Pre-processor below steps are followed:
- Add thread group to the test plan.
- Add Sampler to the thread group.
- Add “BeanShell Preprocessor” to the added sampler.
Example:
In case user-defined variable is set to “true” as a value and this value needs to be changed to “false”, the below provided command can be used to do the same. (Delete is user-defined variable, the value of which needs to be changed))
vars.put(“delete”,”false”);
If any external script is to be used, in that case, file can be uploaded for the script in the Filename field. Browse option is provided to upload the external file.
FAQs About JMeter Preprocessor
Q #1) What is a preprocessor in JMeter?
Answer: Preprocessor is the element that is being used to execute the actions before the sampler request runs. There are a number of Pre-processors which can be used to attain different purposes like retaining value in the variable, getting data from databases, etc. Preprocessors can be added either at the Sampler level or at the thread level.
Q #2) What is Beanshell Preprocessor in JMeter?
Answer: BeanShell is a script based and is an inbuilt component of the JMeter. This preprocessor is used to execute the script before the Sampler runs.
Conclusion
JMeter preprocessors – as we discussed runs just before the sampler executes. JSR223 and Beanshell are scripting-based preprocessors whereas JDBC is used for database activities as fetching or retrieving the data are required.
HTML link parser and RegEx User parameter are not used as popularly as other preprocessors but serve the purpose as required and have their own advantages.