Get Knowledge from Video instead of Content:

Monday 30 January 2017

JMeter - Assertion - "Response Assertion"

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 number, word, statement etc. which could be present either in the server response or in the JMeter request. 
Example: I have created a JMeter script of an e-commerse 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 order during the test I can use Assertion which will check the order number generation statement and confirm that whether all the orders have been submitted successfully or not.  
How to add "Response Assertion" element?
You can follow the below steps:
1. Select "Sampler" node whose response need to be validated.
2. Right-click on the node
3. Mouse hovers "Add"
4. Mouse hovers "Assertions"
5. Click "Response Assertions"

How to remove "Response Assertion" element?
You can follow the below steps:
1. Select "Response Assertion" node
2. Right click on "Response Assertion" node
3. Click "Remove" (Mouse) or "Delete" button from the keyboard
4. Click "Yes"    

What are the attributes of "Response Assertion" element?

"Response Assertion" has the following attributes:
  • 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 given pattern string can be searched in the response of both main and re-directed request and then validated.
  • Main sample only: When request is not re-directed and the given pattern string value is present only in the response of main request then use this search scope.
  • Sub-samples only: When 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 suppose 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 resquest is reditected and 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 request header.
  • Ignore Status: Passing/Failing of assertion having two parts. First part instruct JMeter to check the successful response code (i.e. 200) and then in 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 as well as the assertion. When the Ignore Status checkbox is selected, the Response status is forced to successful even it falls under unsuccessful response category (like 4XX or 5XX) and continue to validate the Assertion. The "Ignore status" checkbox can be used to set the status (response code) successful before performing further checks. Note that this will have the effect of clearing any previous assertion failures, so make sure that this is only set on the f irst 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 regular expression statement should be fully matched with 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 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 pattern 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:
1. Do not add so many assertion elements, it may impact the throughput of the request during the test.
2. Assertion pattern (especially text message) should be unique on the page, so that response can be validated properly.
3. 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.   
  
Next Topic:
Apache JMeter - Assertion - "Duration Assertion" Element

Previous Topic:

Apache JMeter - "Assertions" Element

Related Topics:


No comments :

Post a Comment