In JMeter, Response Assertion is used to validate the presence of a particular string (pattern) or some fields like response code, response message etc. The pattern string could be a number, word, statement etc. which could be present either in the server response or in the JMeter request.
I have created a JMeter script of an e-commerce site where I can place an order to buy some products. Once the order is submitted, the application generates an order number which confirms that the order has been submitted successfully. To validate the submission of an order during the test I can use Assertion which will check the order number generation statement and confirm whether the order has been submitted successfully or not.
How to add ‘Response Assertion’?
Follow the below steps:
- 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 input fields of ‘Response Assertion’?
‘Response Assertion’ has the following input fields:
- Name: To provide the name of the assertion.
- Comments: To provide arbitrary comments (if any)
- Apply to: To define the search scope.
- Main sample and sub-samples: In case, the request is redirected then use this search scope, so that the given pattern string can be searched in the response of both the main and re-directed requests and then validated.
- Main sample only: When the request is not re-directed and the given pattern string value is present only in the response of the main request then use this search scope.
- Sub-samples only: When the request is re-directed and the given pattern string value is available in the response of re-directed request only then you can use this search scope.
- JMeter Variable Name to use: If given pattern string value supposes to be available in any JMeter variable then you need to select this option and provide the JMeter Variable name in the text field.
- Field to Test: This option is used to refine the type of pattern.
- Text Response: This option instructs JMeter to search the given pattern string in the body of the response. The header of the response does not include in the search scope.
- Response Code: JMeter checks and validates the response code only like 200, 302 etc.
- Response Message: JMeter checks and validates the response message only like OK, Error etc.
- Document (text): This option allows JMeter to search the pattern string in the document returned by the server.
- Response Headers: The given string is searched in the header part of the response only including cookie data.
- Request Headers: This option instructs JMeter to search in the header part of the request. It is helpful when the request is redirected and the pattern string value is present in the header of sub-request.
- URL Sampled: The search scope will be URL only when you select this option.
- Request Data: This option is used to validate the string available in the request body which is sent to the server by JMeter. It does not include the request header.
- Ignore Status: Passing/Failing of assertion having two parts. The first part instructs JMeter to check the successful response code (i.e. 200) and then in the second part, the given pattern string is searched and validated. The overall success of the sample is determined by combining the result of both the conditions i.e. response code and the assertion. When the Ignore Status checkbox is selected, the Response status is forced to be successful even it falls under the unsuccessful response category (like 4XX or 5XX) and continues to validate the Assertion. The “Ignore status” checkbox can be used to set the status (response code) successfully before performing further checks. Note that this will clear any previous assertion failures, so make sure that this is only set on the first assertion.
- Pattern Matching Rule: This option is used to set the rule to match the pattern string.
- Contains: If regular expression token is given in the “pattern to test” field and the captured statement contains the text which you want to match then this option need to be selected. Example: The expected pattern is “Your order number is (.*?).” The sampler will be passed if it finds the statement (say) “Your order number is perfOrder987650.” which is a confirmation message that order is submitted and an order number is generated; irrespective of checking the order number (which is a dynamic value).
- Matches: In this case, the regular expression statement should be fully matched with the pattern. Example: Response code should be anything 2XX. Hence the pattern would be 2\d\d.
- Equals: This option instructs JMeter to match the exact text with the case-sensitive feature.
Note: ‘perfmatrix’ and ‘PerfMatrix’ both the string treated differentially.
- Substring: JMeter checks and validates the substring from the captured string.
- Not: This option instructs JMeter that particular string pattern should not be in the response (or search). It is an invert assertion check. Example: You can select “Response Code” as a field to check, “Not” as a pattern matching rule and write “500” in patterns to test. Then JMeter will pass the sampler until status code 500 is received.
- Or: In case of multiple patterns, you want to pass the sampler if any one of the patterns matched then this option is check marked.
- Patterns to Test: Here, you can write a list of patterns to be tested. Each pattern is tested separately. If a pattern fails, then further patterns are not checked (if “Or” is not marked). There is no difference between setting up one Assertion with multiple patterns and setting up multiple Assertions with one pattern each (assuming the other options are the same).
- Custom failure message: A custom message can be written in this field which is displayed in case of the assertion failure.
Some Key Rules related to Assertion:
- Do not add so many assertion elements, it may impact the throughput of the request during the test.
- Assertion patterns (especially text messages) should be unique on the page to validate responses properly.
- You can define Global assertion under the test plan which will be applicable to all the samplers. The global assertion is mainly used to validate the response code.
You may be interested:
- Performance Testing Tutorial
- Performance Engineering Tutorial
- Micro Focus LoadRunner Tutorial
- NeoLoad Tutorial