đ
JMeter Beginner Tutorial 3 - How to use Assertions - YouTube
Channel: Automation Step by Step
[0]
Hello everyone and welcome to this session
on JMeter Beginner Tutorial this is the third
[9]
session of the series and in the earlier two
sessions we saw how do we install and setup
[15]
JMeter on Windows as well as on a Mac machine
and we also discussed that because JMeter
[22]
is a Java application and therefore it is
independent of the platform and operating
[27]
system.
[28]
So it does not matter whether you are using
JMeter on a Windows machine or on a Mac machine,
[34]
it will work exactly in the same way on both
operating systems, the only difference that
[40]
we saw was the way we start JMeter on Windows
and on Mac.
[44]
Okay.
[45]
So in case you have not seen the earlier sessions,
I would request you to go back and see those
[49]
sessions because they're very basic and deliver
you know, step-by-step how to install and
[57]
you start using JMeter.
[59]
In the second series, we'll also see how we
can create a very simple test plan and how
[64]
we can create a performance test for a web
application of your webpage.
[70]
Now, this is the third session and this is
for assertions, and assertion in very simple
[79]
words is our check on the response.
[86]
So when you send a request to the server,
we get back some response and if you want
[92]
to do some checks on the response whether
that response called you want to check whether
[97]
we have received 200 response code or we want
to check some texting the response we may
[103]
also want to test the format of the response
or we may want to test the duration of the
[110]
response that time it took to send that response
or you can also check the size of the response
[119]
right, so there are various checks we want
to do want response and all these checks are
[123]
called is assertions.
[126]
Now in JMeter let us go to our test plan so
this was a test that we created in our last
[133]
session, and this is my Thread Group.
[137]
We are doing the test on this application
lifecharger.org.
[144]
And we had added two listeners one is the
View Results in Table and View Results in
[150]
Tree.
[152]
Now if I have to add assertions, I go to my
Thread Group which I have named Users, do
[159]
a right-click, Add, and assertions.
[162]
And we can see there are a lot of assertions
available in JMeter.
[169]
In the session we are going to focus on the
very commonly used and very basic assertions
[175]
and in this list response assertion is the
most commonly used assertions so I click here
[181]
and I get this window for Response Assertion.
[185]
So let me also write it down here, the first
assertion is Response Assertions.
[194]
Now if you look at this window of Response
Assertion, you have the option to apply this
[200]
assertion to âMain sampe and sub-samplesâ,
âMain sample onlyâ, âSub-samples onlyâ
[206]
and there is some âJMeter variableâ we'll
talk about it later maybe in some later sessions
[211]
but just to talk about the very basic use
of this assertion.
[217]
Now the second option is Response Field to
Test.
[221]
So in this case I want to test the response
code that the server returns.
[227]
You can also test some text in the response
or some document and so on, and I want to
[236]
check whether the response code equals too
and I can write the code here.
[241]
So I am writing 200 so click this add button
and I mention 200 here.
[249]
So what this assertion is going to do, it
will check the response code and compare with
[256]
200.
[257]
If this matches it will pass it otherwise
it will fail.
[261]
Let me make it 5 users and Ramp-Up Period
letâs say 1 second and I just want 1 repetition.
[274]
So let me start this
and you can see the results have generated
[285]
here , in the Tree as well and everything
is in green that means it has passed, right.
[292]
And I can also cross verified by going to
the Results Tree and we can see this is 200
[299]
and every response given 200, right.
[302]
So that means my assertion has passed.
[304]
Now what happens if the session fails?
[307]
So to deliberately fail the assertion, what
I'm going to do is, I'm going to make it something
[312]
else letâs do 201.
[314]
So now it should fail because the server is
giving back response code 200 and I am expecting
[320]
201.
[322]
So let me run this again and let us see the
results now.
[327]
So you can see everything is in red.
[330]
Here also everything is in red and if I expand
this you can see this same response assertion
[337]
failure and it is clearly telling us that
we expected 201 but it receives 200 so that
[346]
is why it's a failure.
[349]
So this is how you can test the response assertion
and to give us a better view of the sessions
[356]
results we can Add a Listener called Assertions
Results.
[364]
So if I run this again and see at the Assertion
Results you can see every time an assertion
[374]
fails our result is locked here and we can
see what is a failure.
[379]
Comparison is 201 and received 200 so it's
a failure.
[383]
So let me revert it back to 200 and see what
happens now.
[387]
Iâve done it again, now you can see in the
Results Assertion results I'm not getting
[396]
any exception and I'm getting all clean here
as well as in the table also everything is
[402]
clean.
[403]
So this is what Response Assertion is and
this is how you can use it.
[407]
Let us see the second assertion and that will
be: Duration Assertion.
[416]
So this is also a very commonly used assertion:
Duration Assertion so as the name indicates
[423]
duration is we are checking the duration of
the response and again we can apply it to
[430]
âMain sample and sub-samplesâ and so on
and if I say let me first quickly look at
[435]
the duration I'm getting a duration of around
1.5 seconds this is a millisecond so you can
[444]
1.5 second let me say let me put here 1500
which will be 1.5 seconds this is in milliseconds,
[456]
right.
[457]
So any response which takes more than 1500
milliseconds will be a failure and we will
[465]
get notified in the Assertions Results.
[469]
So let me run it again and let us see if we
get any response beyond 1500 millisecond and
[477]
yes there are I think all the response took
more than 1500 milliseconds that's why everything
[483]
is in failure and you can see this is the
Duration Assertion failed.
[488]
âThe operation lasted too longâ it took
2169 milliseconds but should not have lasted
[493]
longer than 1500 milliseconds, right.
[496]
So let me change the duration and say this
is 2200 milliseconds and run it again.
[509]
Let me see, this time everything passed, because
all the response was less than 2200 milliseconds.
[519]
If I again make it 2000 millisecond and run
it let me see if anything fails.
[529]
No still everything was less than 2000 milliseconds,
right?
[533]
So now this is passing.
[537]
So this is how we can have our Duration Assertion
and the check on the response time.
[545]
The third Assertion that we are going to see
here is
[553]
Size Assertion which is also quite commonly
used Assertion in JMeter.
[560]
So Size Assertion as the name indicates it
is it has to do something with the response
[565]
size.
[566]
Now if we see our worth size in bytes is somewhere
around 30235 bytes.
[574]
So let me say 30235 and equals to.
[583]
So if my size in bytes is equal to this particular
number then it should pass, else it should
[590]
fail and I can have multiple comparisons here.
[596]
So let me run it again and see if all the
Size Assertion passes or not.
[603]
And yes I have got one failure and you can
see âThe result was the wrong size: It was
[609]
30228 bytes, but should have been equal to
30235 bytesâ,right?
[613]
And if I go to my Results Tree I get the same
message here.
[620]
The other requests also fail because of some
other assertion failure which is like the
[625]
Duration Assertion in most of the cases.
[628]
Let me make the Duration Assertion some higher
number so that this does not fail and run
[635]
it again.
[638]
OK, so now we can see there are only 3 failures
and they are due to the Size Assertion and
[648]
the same thing is displayed here as a well as in the results tree
so there is all 3 failures and all failures are
[654]
due to the Size Assertion.
[656]
So this is how we can check the size of the
response and obviously, I have already made
[664]
a strict check by selecting the equals option
we mostly select either greater than or less
[672]
than or greater than equal to or less than
equal to.
[675]
So if I say the size should be greater than
or equal to this particular number and run
[681]
it again, let me see this time what happens.
[684]
See, this time everything passes.
[687]
This time all the Assertions passed so we're
seeing all green here.
[694]
So this was the Size Assertion.
[697]
The next Assertion, that we're going to see
is âHTML Assertionâ.
[708]
The fourth Assertion is HTML Assertion.
[712]
So what this means is we are going to check
the format of our response and it is actually
[721]
going to check that the format is a valid
HTML or not.
[726]
In case it is not a valid HTML it will throw
out some address and it will fail that Assertion.
[732]
So if I run this test now, see we have a failure
for HTML Assertion and it is saying there
[741]
are 38 errors and 110 warnings.
[747]
Now if you go back to your HTML Assertions
you can actually provide a threshold for the
[754]
errors as well as warnings.
[756]
So let me say that my threshold is 38, if
there are errors more than 38 it should fail
[763]
it should give out a message a re-message
and 110 other warnings.
[770]
Let me run it again, now this time it did
not fail, because I have affiliates in the
[780]
HTML format but the failures are 38 and 110
warnings and I have made the threshold equal
[787]
to that, in case I make it 37 and let me make
it 109.
[793]
This time it must fail.
[796]
So we can see, there 38 errors and 110 warnings
allowed at 37 and warnings allowed 109 so
[805]
there's a failure and we have caught the results
here the same we can see in the Tree View
[811]
as well.
[813]
Now here is the thing, this is telling us
the number of errors, number of warnings but
[819]
where exactly is the error and what element
is missing or what note is missing we are
[823]
not aware of that right?
[825]
So to get a report on that, what we can do
is we can actually write this report to a
[831]
file and you can see this is the filename
we can browse it and let me say I'm giving
[838]
I want to store all the you know errors here
and I can say errors only.
[847]
So it will only log the errors and now if
I run this, so here are the errors and if
[856]
I go to this file, so this file is here.
[862]
Now you can see the test given us all the
you know errors, what was missing.
[867]
This is not nice: âdiscarding unexpected
<header>â and all those things so this is
[872]
a very good test to validate your HTML and
in stand-alone web application this might
[879]
not be a very, you know, concerning thing
but if you're web applicant has to you know,
[887]
partner with 3rd party apps and they have
to talk to each other, then these issues can
[894]
be very big and you will have to resolve this
right.
[897]
So JMeter provides us this way to check out
XML, HTML and so on.
[902]
Similarly you can see we can check do I check
on XML on or XHTML as well so if I select
[910]
XML and do the same run.
[919]
So here everything passed.
[924]
So you can do a check on XHTML as well and
this HTML, right?
[928]
So this was a HTML Assertion.
[932]
Let us look at some other Assertions.
[935]
There is XML Assertion as well so again XML
Assertion will check the format and if it
[945]
is a valid XML then it will pass otherwise
it will fail so we can say this is a check
[955]
on the XML format, right?
[959]
So see, XML Assertion is failing and it is
saying âThe entity âraquoâ was referenced,
[969]
but not declared.â.
[970]
And again you can write this Assertion to
a file as well and you can make a check on
[980]
your XML and you can actually fix this thing,
right?
[984]
Whatever is the Assertion failure you get
in your web app.
[990]
One other Assertion that we should look at
is XPath Assertion.
[997]
Now XPath Assertion will mostly be used in
API testing so we'll be having a dam on API
[1005]
testing with JMeter in the coming sessions
and then there will be you know, looking at
[1010]
how exactly we used XPath Assertion for now
I'll just explain that you know, in the response,
[1017]
if you want to check some particular node
or some particular XPath or some value at
[1021]
some particular XPath, we can use XPath Assertion.
[1025]
So XPath Assertion, we will look into more
details in the coming sessions but just for
[1033]
you know our basic understanding we want to
assert some XPath or some value or some node
[1039]
in the response and we can use this.
[1042]
So these are all the Assertions or I should
say the most commonly used, or the most basic
[1049]
Assertions which we should be knowing to use
JMeter sessions.
[1052]
One thing is that you should take carer's
when you add a listener for Tree and you add
[1061]
a listener for a Assertion Results these listeners
take up a lot of memory, so when you want
[1068]
to put the same test on load, maybe you can
disable these listeners but if you want to
[1074]
do a check on your application format and
other things, you can add this listeners,
[1083]
otherwise you can you know just drop these
listeners or disable them.
[1087]
So this was all about basic Assertions in
JMeter.
[1092]
In the coming series of this tutorial we will
look at various types of listeners in JMeter.
[1098]
So that's all about Assertions in JMeter.
[1102]
Hope you like it , thanks a lot. :)
You can go back to the homepage right here: Homepage





