Hope you all can get the idea and can understand how to use Response Assertion in your test plan via this post: Response Assertion in JMeter. Next step, I will talk about the properties of this element. It will help you to use the Response Assertion more efficiency and can adapt your requirement in the best way.

1. Apply to:

This is for use with samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources, Mail Reader or samples generated by the Transaction Controller.

  • Main sample only – assertion only applies to the main sample
  • Sub-samples only – assertion only applies to the sub-samples
  • Main sample and sub-samples – assertion applies to both.
  • JMeter Variable – assertion is to be applied to the contents of the named variable
    For example, we have a variable TEST with the value is ABCDEF12345, and we want to assert that variable contain text ABCD, then we should do as following:
    – Input name of variable into JMeter Variable field
    – Input the text which you want to assert into Patterns to Test
    props-assert-jmeter-variable.png
    The test will pass if variable TEST contains text ABCD, otherwise it will FAIL

2. Response Field to Test:

2.1 Response Field: Instructs JMeter which field of the Response to test.

  • Text Response – the response text from the server, i.e. the body, excluding any HTTP headers.
    props-assert-text-response.png
  • Document (text) – the extracted text from various type of documents via Apache Tika (see View Results Tree Document view section).
  • URL sampled
    props-assert-url-sampled.png
  • Response Code – e.g. 200
    props-assert-response-code.png
  • Response Message – e.g. OK
    props-assert-response-message.png
  • Response Headers, including Set-Cookie headers (if any)
    props-assert-response-header.png

2.2 Ignore Status: Instructs JMeter to set the status to success initially.

Firstly, we show know something about the HTTP Response Code. Base on that, we have 5 types of HTTP Response Code:

1xx: Informational
2xx: Success
3xx: Redirection
4xx: Client Error
5xx: Server Error

In JMeter, by default, only the request with Response Code 1xx, 2xx and 3xx are counted as the success request (display in green). The rest one: 4xx and 5xx are failed request (display in red).

Example: the URL http://jmeter.apache.org/test is not correct and it will return 404 response code. This is correct and matches our expectation, it should display green in jmeter in this case. So how can we achieve this?

First, let see what JMeter show:

props-assert-404-fail

Then, if you want to make the test become green, please do the following steps:

– Add Response Assertion under HTTP Request
– Select Response Code in Response Field to Test
– And check the checkbox Ignore Status
– Input 404 into Patterns to Test

It means the test will pass (ignore status) if the response code is 404.

props-assert-404-pass.png

Now let see how it work with the same test as above

props-assert-404-pass.gif

You can see, with the help of Response Assertion and option Ignore Status, the test is now green, even the response code is 4xx (404 in this case).

NOTE: this will have the effect of clearing any previous assertion failures, so make sure that this is only set on the first assertion.

Example: I added 2 Response Assertion under the HTTP Request. First Assertion I try to assert a text which is not exist. And the second Assertion I try to assert which return true and check Ignore Status.

Let see what will we get:

props-assert-ignore-status-2.png

Even the Assertion 1 returns FALSE, but the result still GREEN. No, please focus the result and see what we have.

props-assert-ignore-status-3.png

It shows FAIL with Assertion 1, but the Ignore Status in Assertion 2 made the result become green, it’s so dangerous due to get the wrong result. So please pay attention and only apply for Ignore Status in the first Assertion.

3. Pattern Matching Rules:

Indicates how the text being tested is checked against the pattern. But why there are 4 kinds of rules: Contains, Matches, Equals, Substring? And seems the rule Contains and Substring are the same, and rule Matches and Equals are the same ?!?

And this is the difference:

  • Contains – true if the text contains the regular expression pattern
  • Matches – true if the whole text matches the regular expression pattern
  • Equals – true if the whole text equals the pattern string (case-sensitive)
  • Substring – true if the text contains the pattern string (case-sensitive)

So in the meaning, Contains and Substring are the same, it’s true if the text contains your expectation. And Matches, Equals are the same, too, it’s true if the whole text matches with your expectation.

But in the way to use: Contains and Matches use Perl5-style regular expressions. Equals  and Substring use for plain text, case-sensitive.

Now let go through some example to understand clearly about these rules.

3.1 Equals and Substring

Substring: I will create an HTTP Request to access this link http://jmeter.apache.org/download_jmeter.cgi and verify that it contains text Download Apache JMeter

props-assert-substring1.gif

Then I will add more assertion to verify text Archives and Verification of downloads

Very easy, don’t need to create more Assertion, if all the text are the same Field (example Text Response), then we just need to add more line in the Patterns to Test.

props-assert-substring2.gif

Equals: It’s the same but please remember, it must match the whole text, and we rarely use this case so I will not show the example here, but hope you all can get the idea.

3.2 Contains and Matches

(Same as above, I just talk above Contains, we’ll rarely use Matches rule)

All characters match themselves except for the following special characters:
. [ { ( ) \ * + ? | ^ $

There is some basic regular expression of Perl5-style which you can easily apply to the test:

By default, the pattern is in multi-line mode, which means that the “.” meta-character does not match the newline. In multi-line mode, “^” and “$” match the start or end of any line anywhere within the string – not just the start and end of the entire string. Note that \s does match new-line. The case is also significant. To override these settings, one can use the extended regular expression syntax. For example:

(?i)
ignore sensitive case
(?s)
treat target as single line, i.e. “.” matches new-line
[a-c]
match any single character in the range ‘a’ to ‘c’
|
will match either of its arguments
\s
space
These can be used anywhere within the expression and remain in effect until overridden. E.g.
(?i)apple Pie
matches “ApPLe Pie“, or “ApPLe pIe
(?s)Apple.+?Pie
matches Apple followed by Pie, which may be on a subsequent line.
Apple(?s).+?Pie
same as above, but it’s probably clearer to use the (?s) at the start.
Appl[a-f] Pie
matches Appla Pie, or Apple Pie, or Applc Pie, or any characters from a to f  after Appl
Apple|Pie
matches if the response contains Apple OR Pie
Apple\sPie
matches Appla Pie with space between Apple and Pie

I create a test plan and demo all these cases, you can download it here and try by yourself.

NOTE: You must follow this link to install plugins Dummy Sampler before running my demo.

3.3 Checkbox Not

It may also be selected to invert the result of the check.

Advertisements