We have also mentioned the assertion in some previous articles written on Selenium web driver and Selenium IDE. In JMeter, assertion confirms the validity of the test plan by
Read Also: Handling Assertions in Selenium WebDriver with TestNG
Table of Contents
Types of Assertions in JMeter
Following are the common types of assertions used on JMeter.
1. Response Assertion
Response assertion is the most commonly used assertion in JMeter. It allows you to add pattern strings that were compared against the various response from the server. It will match the various response patterns such as containing text, response codes, response headers,
Example of Adding Response Assertion
Before adding assertions in the JMeter test plan you have to add a sampler i.e. HTTP Request and some listeners such as “View Results Tree” and “View Results in Table” as given in the previous tutorial “Getting Started with JMeter“. Use the following steps to set up response assertion after adding samplers and listeners in JMeter.
- First, right-click on the Thread Group name, go to Add>Assertions, and click on Response Assertion.
- You can choose and apply the assertion to “Main sample only”, “Sub-samples only”, Main sample and sub-samples” etc. In this example, I have selected “
Main sample only”. - Now, select one of the fields to test such as “Text Response”, “Response Code”, “Response Message” etc. Here we are comparing Response Code equal to 200.
- There are different pattern matching rules available, select the option “Equals” and add “200” on the pattern to test to check whether the testing URL returns a response code “200” or not.
- JMeter has a special listener to view assertion results, to add assertion results, go to Listener from the thread group and click on Assertion Results.
- Now execute the test plan and observe the test results through “View Results Tree”, “Assertion Results”, “View Results in Table” etc. The assertion will be passed if the expected response code is returned otherwise it will be failed.
2. Duration Assertion
Duration assertion will check whether the response was received within the expected time. The assertion will be passed if the response was received within the given amount of time and it will be failed if not received in the given time.
Example of Adding Duration Assertion
Use the following steps to set up duration assertions in JMeter.
- Same as adding response assertion, you can add duration assertion. For this, right-click on Thread Group, go to Add>Assertions, and click on Duration Assertion.
- Specify the duration in asserts in milliseconds. In the screenshot above, I have added 3000 milliseconds in duration.
- Run the Test Plan and observe the results. In the screenshot below, on the five HTTP requests, two of them failed and others are passed. Here one of the failed
requests has taken 3961 milliseconds for loading.
3. Size Assertion
As the name suggested that size assertion checks the size of the response. In size assertion, we set the limits for the size by specifying the size to be equal to, greater than, or not equal to a given number of bytes. The assertion will be failed if the size of the response received does not match the conditions provided.
Example of Adding Size Assertion
Use the following steps to set up size assertions in JMeter.
- To add size assertion in the JMeter test plan, right-click on Thread Group, go to Add>Assertions, and click on Size Assertion.
- Specify the size to assert in bytes and also specify the type of comparison. In the screenshot above, I have added the size “71031” in bytes and selected “=” on the type of comparison.
- Run the Test Plan and observe the results. In the screenshot below, among the five HTTP requests, two of them failed and others are passed. In the screenshot below, two requests failed due to the request size being 71030 bytes.
4. XML Assertion
we will use XML assertion in JMeter to check whether the provided URL consists of valid XML data. If the invalid XML code is found, failure will be reported with an assertion failure message.
Example of Adding XML Assertion
To verify XML assertion, you just have to add XML Assertion same as the way of adding other assertions in Jmeter, and run the test plan. The following screenshot shows the test result when tested with an HTML page.
5. HTML Assertion
Similar to the XML assertion, HTML assertion validates the HTML syntax of the HTML document. It will check whether the response received is a well-formed HTML, XHTML, or XML document and show the validation messages.
Example of Adding HTML Assertion
You can add HTML assertions going through Thread Group same as adding other assertions in Jmeter. You can select the document type and HTML formats such as HTML, XHTML, or XML as shown in the screenshot below, and run the test plan to view the results.
6. XPath Assertion
The Xpath assertion will find the specified HTML element in XPath format is present or not. It also returns the number of occurrences if the target element was found.
Example of Adding Xpath Assertion
You can add Xpath assertions going through Thread Group same as adding other assertions in Jmeter. You can select the document type and HTML formats such as HTML, XHTML, or XML and run the test plan to view the results.
Comments are closed.