A common mistake in software development is we emphasize so
much on functionality that we sometimes disregard the importance of
performance of web applications. The end-result is the web application, in spite of having exciting features and fascinating UI, is turned down by client. Therefore,
load testing is no more a secondary priority. I use JMeter, a very well-known
open source tool, for load testing. Unlike Selenium, which is a functional
testing tool, JMeter works at the HTTP requests and responses level. Therefore,
when running a test in JMeter, we don’t see what’s happening in the browser.
This is why assertions are very important to ensure that the test passes at every
level and the browser is doing what it is supposed to do. Here are few examples of
how to use assertions. I mostly use Response
Assertion and BeanShell Assertions.
I am going to show their usage here.
1. Response Assertion: This is pretty simple yet
very affective. And this is probably the most commonly used assertion.
Example 1: From Main Sample
Suppose you login to a site and upon
successful login you are redirected to the home page. At this point of your load testing you want to ensure
that the login was successful during load testing and the response to login
HTTP request is the homepage. Consider the following example
Suppose “John Miller” is signing in to the site.
And the welcome page greets him by his name. Now the assertion here would be to
check if the text “John Miller” exists in the response of login HTTP request.
You can do the following
Step 1: Identify the Login HTTP request.
This would probably look like the following
Step 2: Add a Response Assertion as a child
of this request a shown below.
i. Give it a name for better reference
ii. Select “Main sample only”
iii. Select “Text Response”
iv.
Select “Contains”
v.
Use Add button to add one or more verification
text
Step 3: Run the test. If things goes wrong
you will see an error in “View Result Tree” listener as shown below
Example 2: From JMeter Variable
Consider the above example. Suppose you
do not want to verify the homepage by the user name rather you want to verify
by its title. Suppose title in home page looks like this
You can follow the following steps to accomplish this.
Step 1: Identify the Login HTTP request.
Step 2: Add a Post Processor Regular
Expression Extractor as a child of this request. Add this before the response
assertion as added in previous example.
Step 3: Change the response assertion as
shown below
i.
Select JMeter Variable
ii.
Add the reference name of the variable added in previous step
iii.
Provide the title
Step 4: Run Test.
2. BeanShell Assertion: This is not as simple and
straightforward as response assertion. But it offers more flexibility to add
customized assertions. I personally use this quite often.
Example:
Consider the same example we used above.
Suppose you have successfully logged in and you are visiting homepage. The homepage contains 4 meta-tags
that you want to ensure are present every time a user visits the page.
(This is just an example to keep it simple.
You may find a better usage in real life.)
Step 1: Add a BeanShell assertion as a
child of the login HTTP request. And add the following code there
If you notice, it’s a simple java code that
does the followings
i. Get the entire response text
ii. Looks for appearances of “<meta” text in the
response text and counts the appearances
iii. Finally fails the test if count does not match
4.
Step 2: Run the test.
Of course, the above can be accomplished
without using BeanShell assertion. However, this is an example of how flexible
BeanShell assertions can be and how you can use it at your will.
Try this. There are other assertions as well. Once you get
started, you may want to explore them as well. Thanks for reading.
Thanks for great guide. In my turn I would recommend checking out How to Use JMeter Assertions in 3 Easy Steps article which highlights such areas like scope and cost and provides howtos on other assertion types like Size, Duration, XPath, etc.
ReplyDelete