JMeter β Response Assertion
The response assertion in JMeter is used to confirm the existence of a specific string (pattern) or certain attributes, like response code, response message, etc. The pattern string can take the form of a number, word, statement, etc., and can appear in both the server response and the JMeter request.
As an illustration, take the JMeter script I created for an online store where I can order products. The program creates an order number once the order is shipped, indicating that it has been shipped successfully. By using an assertion, I can verify whether or not an order shipped successfully during testing by checking the order number generation statement.
How do I add ‘Response Confirmation’?
Follow the steps below:
- Select the βSamplerβ element whose response needs to be validated.
- Right-click on the element
- Hover the cursor on βAddβ
- Hover the cursor on βAssertionsβ
- Click βResponse Assertions
What are the ‘Response Assertion’ input fields?
There are the following input fields for “Response Assertion”:
Name: To give the assertion’s name.
Please leave arbitrary remarks, if any.
Apply to: Defining the search parameters.
Subsamples and the main sample: Use this search scope if the request is being redirected so that the specified pattern string may be searched in the responses to both the original request and the redirected request, and then the results can be confirmed.
primary example only: Use this search scope if the request isn’t being redirected and the specified pattern string value appears solely in the main request’s response.
Only sub-samples: This search scope can only be used when a request is redirected and the pattern string value is present in the response to the redirected request.
Useful JMeter variable name: Select this option and enter the name of the JMeter variable in the text field if the specified pattern string value is expected to be present in any JMeter variable.
Field to Test: This choice is used to hone the pattern’s kind.
Text Response: By selecting this option, JMeter is told to look for the specified pattern string in the response’s body. The response’s header excludes from the search scope.
Response Code: Only response codes like 200, 302, etc. are checked and validated by JMeter.
Response Message: Only the response message, such as OK, Error, etc., is checked and validated by JMeter.
Document (text): Using this option, JMeter can look for a pattern string in the document that the server has returned.
Response Headers: Only the header portion of the response, including the cookie data, is searched for the specified string.
Request Headers: By selecting this option, JMeter is told to look at the request’s header section. When the request is redirected and the pattern string value is present in the sub-requests header, it is beneficial.
Sample URL β When you choose this option, the search is limited to URLs only.
Request Data β This option checks the string in the request body before JMeter sends it to the server. The request header is missing.
Ignore the two-part statement status: Pass/Fail. JMeter is told to check for the 200 code for a successful response in the first section before looking for and validating the pattern string provided in the second section. The combination of the results of both criteria, that is, the response code and the affirmation, evaluates the overall success of the sample. When the Ignore Status option is checked, the assertion is validated even when the response status is forced to be OK (such as 4XX or 5XX). Prior to further checks, the status (response code) can be set to OK via the “Ignore Status” checkbox. Make sure this has the effect of clearing previous failed assertions.
Pattern Match Rule β By using this option, you can make the rule match a specific pattern string.
contains: This option should be chosen if the regular expression token entered in the “Pattern to test” field matches the text you want to match in the captured statement. For example, the expected pattern is “Your order number is (.*?)”. Even if the order number (which is a dynamic value) is checked, the sampler will find the phrase “Your order number is perfOrder987650”, which confirms that the order has been shipped and produced.
Match: In this case, the regular expression declaration and the pattern must match completely. An example of a response code is 2XX. The pattern would then be 2dd.
Equals: This option tells JMeter to use the case-sensitive function to match the exact text.
JMeter checks and checks the substring derived from the captured string.
No β Selecting this option tells JMeter that a specific string pattern should not appear in the response (or query). It is a claim checker that reverses. Example: You can enter “500” in the Patterns to test field, “No” as the pattern matching rule, and “Response code” as the field to check. When the 500 status code is received, JMeter continues through the sampler.
Or: If there are multiple patterns and you want to pass the sample if only one matches, check this box.
Patterns to try: You can list the patterns you want to try here. Each pattern is examined independently. If “O” is not checked, subsequent patterns will not be checked if a pattern fails. All other possibilities are the same, there is no distinction between posting a single assertion with multiple patterns and setting up multiple assertions with a single pattern.
Custom Error Message β This field can be used to enter a unique message that will be displayed if the claim fails.
Several Important Guidelines for Assertion
Avoid adding too many assertion items because doing so could slow down the test request’s throughput.
So that the response can be correctly checked, the assertion pattern (particularly for text messages) should be unique on the page.
Under the test plan, you can specify a global assertion that will apply to all samplers. The response code is primarily verified using the global assertion.Β Β Β