Using Post Processor In JMeter (Regular Expression Extractor)

In this tutorial, you will learn to use JMeter Post Processor such as Regular Expression Extractor, BeanShell, JDBC, Boundry Extractor etc.:

Processors are of two types Pre-processor and Post-processor.

Pre Processor executes before the sampler execution. Post Processors are used to execute the response data from the server and to save the specific extracted values for later use.

Post-Processors are actions that are performed after your sampler has been executed. You can use them to perform some actions on your response or extract some values out from the response and save them in a variable that can be used later.

=> Click here for The Complete Free Training On JMeter (20+ Videos)

POST PROCESSOR in Jmeter

Video Tutorial On JMeter Post Processor

Regular Expression Post-Processor uses Regular Expression string to fetch some values from the response of your request. The value(s) which is extracted can be stored in any variable and can be a reference in any further request in the test plan.  Values can be extracted from the response body, header, URL, response code and other provided fields when you add this processor.

JMeter POST Processors

Post Processor executes when Sampler request execution is done. There are various Post processors that are being used in JMeter.

The element list includes:

  1. Regular Expression Extractor
  2. CSS/JQuery Extractor
  3. XPath Extractor
  4. BeanShell Post Processor
  5. Result Status Action Handler
  6. JSR223 Post Processor
  7. JDBC Post Processor
  8. JSON Path Post Processor
  9. Boundary Extractor
  10. BSF Post Processor
  11. Debug Post Processor

Post Processors list

Let’s go through these JMeter Post Processors:

#1) Regular Expression Extractor

Regular expression extractor is used to get the information from the response of the server. It uses Perl type Regular expression for extracting the information i.e. values. This processor will run after each sampler request is executed.

How it works:

Sampler Request->Apply Regular expression extractor-> Information/Value extraction-> Template String generation ->Storage of extracted value/Information in a specific variable name

  • Create a Test Plan
  • Add Thread Group
  • Add Sampler “HTTP Request”
  • Add the Processor “Regular expression extractor”

Apply To: “Apply to” has 4 options.

  • Main Sample and Sub Samples: It applies to both main samples and Sub Samples.
  • Main Samples only: It gets applied to the main samples only.
  • Sub Samples only: It gets applied to Sub Samples only.
  • JMeter Variable: The extraction is applied to the named variable only.

Field To Check: Field to check has a number of options. All the field names are self-explanatory as their name suggests.

  • Body represents the body of the response i.e. content excluding headers.
  • Body (Unescaped): Body of the response which has all the HTML escape code’s replaced.
  • Body as a Document From the number of documents data is extracted.

Name of created variable will have the extracted text saved.
Regular expression will match the pattern against the text to be extracted.’.+?’ represents a single instance of the text placed by the tags.
Template: Template is used to create String for the extracted values match.
$1$ represents group 1
Match No (0 for Random): To get which one to select as variable.

  • 0 indicates a random value from the found results.
  • Nth number indicates the nth number from the found results.
  • Any negative number – indicates that all the data should be extracted.

Default Value: Reference value is set to the default value in case Regular expression does not match.

Regular expression extratctor_options

Add Listener View Results tree.

Regular Expression View Results

#2) CSS/JQuery Extractor

CSS Selector Extractor is used to extract the values/information from the HTML response of the server.

How it works:

  • Create Test Plan
  • Add Thread Group
  • Add Sampler “HTTP Request” to the thread.
  • Add the Processor “CSS Selector Extractor” and provide the below details:

CSS/JQuery extractor Implementation: Two options are provided to choose for Implementation:

  • Jsoup
  • Jodd-Lagarto

CSS selector_implementation

In case none of the implementations is chosen, by default JSoup is considered.

CSS selector expression: For extraction, CSS locator can be used. Say the user has to extract the list of cities from where the flight will depart in a booking site. User can provide CSS locator in the field to get the expected result.

CSS Selector expression is used to select nodes from the HTML response data from the server.

CSS selector options

Add listener “View Result Tree” to view the results.

View Result tree CSS Selector

#3) XPath Extractor

XPath Extractor uses Xpath Query language to extract value from XML or X( HTML) responses.

Let’s take an example for how Xpath extractor can be used:

  • Create a test Plan
  • Add Thread Group
  • Add HTTP Request default config element to the Thread Group and provide Server name or IP.
  • Add two HTTP Sampler’s and provide the details Path in that.
  • Add Xpath extractor and provide details as:
    • Name of the creator: Result will be Stored in the created variable name which is provided in this field.
    • Xpath Query: Query in XPath language.
    • Match No (0 for Random): 0 will extract random value and -1 will extract all the values.
    • Default Value: NOTFOUND
    • Default value NOT FOUND will be returned in case there is no match found.

Use Tidy should be selected for HTML response to be converted to XHTML.

Xpath extractor_created variable created variable_subtitle

  • Add Debug Sampler. It is used to debug the script and its result can be seen via the added listener.
  • Add listener as View Result Tree.

Add debug sampler

#4) Result Status Action Handler

Result Status Action handler let the user select the action to be taken when sampler gets any error.

Below actions can be taken:

  • Continue
  • Start next thread loop
  • Stop thread
  • Stop test
  • Stop test now
  • Break Current loop
  • Go to the next iteration of the current loop

Let’s take an example when the Stop Test Now option is chosen.

Stop Test now option new

  • Create a Test Plan
  • Add processor ->Result Status Action Handler and select Stop Test Now option
  • Add Configuration element ->HTTP Request defaults and provide the server name or IP
  • Add Sampler-> HTTP Requests with path resources/
  • Add another Sampler with incorrect path
  • Add another Sampler with a path for Homepage.
  • Add Listener ->View Results in table and run the script.

server name or IP new

Since Stop test now was chosen as an action, the processor did not allow the next sampler to run.

#5) BeanShell Post Processor

This Processor executes after the Sampler.

BeanShell Post Processor

BeanShell Post Processor Test Plan

Reset bsh.Interpreter before each call: For each sampler, the interpreter will be recreated in case this option is selected.
Parameters: Parameters are the ones that will be passed to Bean shell script.
Script Files: Script file is the file in which the Beanshell script is stored to run.
Script: Script is the Beanshell script.

#6) JSR223 Post Processor

In this processor, the script is applied only when the sampler has been executed.

Let’s take an example for the same:

  • Create a Test Plan
  • Add Thread group
  • Add Sampler ->HTTP Request to the thread and provide server name or IP.
  • Add the Processor “JSR223 Post Processor” to the thread. Select language Groovy as in the below screenshot:

JSR223 Post processor_select language Groovy

Write a script using the syntax as below and observe the results in logs:

Write a script using the syntax

Logs can be written with variables as well, as displayed in the below screen:

Logs can be written with variables as well

New variables can also be added. Refer to the below screenshot for the same.

New variables can also be added

Add a Debug sampler to the thread and listener as View Results Tree and run the test.

Add a Debug sampler to the thread and listener

#7) JDBC Post Processor

SQL queries are run after the Sampler has executed to make the changes which need to be reverted after the testing is done or are not required.

It can be used by,

  • Creating a Test Plan
  • Add JDBC Connection Configuration with details of URL and credentials.
  • Add JDBC Request providing the inputs such as variable names and output as Result variable name. Query type can be selected from the list as shown below:

select statement

  • Add Debug Sampler to view the variable values.
  • Add Listener View Results tree to view the results.

Add Listener View Results new

#8) JSON Path Post Processor

This Processor gets the data from the JSON response using JSON Path syntax. It is created under the Sampler which has a response.

  • Create a Test Plan
  • Add thread group to the test plan.
  • Add Sampler HTTP Request.
  • Add config element as “HTTP header Manager”.
  • Add Processor as JSON Path processor
  • Add the JSR223 processor and select Bean shell language and provide a script.
  • Add listener “View Result tree”.
  • Run the test.

JSON Path Post Processor_View results Tree

JSON Path post proceesor_default values

Name of the created field is the one where the result is stored. $.name in JSON path expression represents all names to be in the result. 0 match number is for random value to be chosen. If nothing is returned in that case it will show name_not found.

#9) Boundary Extractor

Boundary extractor extracts data from the boundaries of server response after the sampler has been executed. Once data/values have been extracted, the Template string needs to be generated and then the results are stored in the provided variable name.

How to use:

  • Create Test plan
  • Add thread in the test plan
  • Add sampler-> HTTP Request in the thread.
  • Add Boundary extractor processor and fill in the details as shown below in the screenshot:

Boundary extractor_body field

Name of Created Variable: Result is Stored in the created variable name which is provided in this field.
Left Boundary: Value of the left boundary to be found.
Right Boundary: Value of the right boundary to be found.

  • Add the JSR223 processor to get the result printed in logs.

Add JSR223 processor

#10) Debug Post Processor

This Processor is used if in case any sampler needs to be debugged, it is added as a child to that sampler.

Debug Post Processor

FAQ About Post Processor

Q #1) Can JMeter be used for Unit testing?

Answer: Yes, it can be used for Unit testing. It can be used as a Unit testing tool for Webservices, FTP, LDAP, Java database connectivity (JDBC) database connections, TCP connections.

Q #2) How do I save a request body in JMeter?

Answer: To save a request, the “ View Result Tree” listener can be used.

Add->Listener->View Result tree ->Configure button-> Select in which type data has to be stored

Q #3) What is the execution order of JMeter elements?

Answer: Below listed is the execution order of Jmeter elements when multiple elements are there in the thread group.

  • Configuration Elements
  • Pre-processor (Only if they are applied to any sampler)
  • Timers (Only if they are applied to any sampler)
  • Sampler
  • Postprocessor (Only if they are applied to any sampler)
  • Assertions (Only if they are applied to any sampler)
  • Listeners

Q #4) What is JSR223 in JMeter?

Answer: JSR223 is a scripting API for Java languages. JMeter has elements like JSR223 Sampler, JSR223 Pre-processor, JSR223 Post processor, JSR223 Listener. JSR223 helps in the execution of script which are written in various languages.

Q #5) Is JMeter good for API testing?

Answer: It is an open-source tool that can be used for API testing as well as for functional, performance, load, and automation testing.

Q#6) Which language is used in JMeter?

Answer: Groovy language is mostly used and is suitable as Groovy scripting language is easy to use and can be integrated with Java to enhance more scripting potential.

Conclusion

Post Processors, as the name suggests, run post the sampler execution. All the processors play their own role and extract the data from various responses as Regular expression extractor uses Perl type regular expression to extract values from server response, CSS selector extractor uses CSS selector to extract data from HTML responses.

XPath extractor uses XPath Query language to extract data from XML or XHTML responses. JSON Extractor uses JMESPath Query language. Result Status action handler handles the result by allowing to stop the thread if the sampler fails. Boundary extractor extracts values from the boundaries of the server response.

=> Click here for The Complete Free Training On JMeter (20+ Videos)