Session 43 – Selenium with Java | TestNG Annotations | … — Transcript

Learn TestNG annotations in Selenium with Java, focusing on @Test, @BeforeMethod, @AfterMethod, and their use in test case structuring.

Key Takeaways

  • TestNG annotations help organize test execution flow and reduce code duplication.
  • @Test annotation defines the actual test methods where validations occur.
  • @BeforeMethod and @AfterMethod annotations are ideal for repetitive setup and cleanup tasks like login and logout.
  • Prioritizing test methods controls their execution order.
  • Proper annotation usage enhances test maintainability and clarity.

Summary

  • Recap of installing TestNG and executing test cases using XML files.
  • Introduction to various TestNG annotations beyond @Test, including @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, @BeforeTest, @AfterTest, @BeforeSuite, and @AfterSuite.
  • Explanation of the @Test annotation and how it marks methods as test cases with priority settings.
  • Detailed discussion on @BeforeMethod and @AfterMethod annotations and their role in setting up prerequisites and cleanup steps for each test method.
  • Use case example demonstrating login as a prerequisite and logout as an exit step for multiple test methods.
  • Clarification on separating actual test logic from setup and teardown steps to avoid code repetition.
  • Guidance on identifying which methods should be annotated as tests and which should be setup/teardown.
  • Emphasis on efficient test case design by reusing login and logout methods with appropriate annotations.
  • Insight into structuring test cases to improve maintainability and clarity in Selenium automation.
  • Encouragement to adapt annotation usage based on specific test case requirements.

Full Transcript — Download SRT & Markdown

00:04
Speaker A
So in the previous class, we have seen how to install TestNG and how we can write test cases in TestNG. Also, we have seen how we can execute TestNG test cases using an XML file, like a single test case and multiple test cases. And also, we have seen TestNG default report, how we can generate it using an XML file. So today, we'll see some more types of annotations. We have discussed about the test annotation, and apart from it, we have some more annotations. So we'll discuss what other annotations are provided by TestNG, how we can use them, and where we can use them, and so on.
00:23
Speaker A
XML file so today we'll see some more type of annotations so we have discussed about uh test annotation and apart from it we have some more annotations so we'll discuss what are other annotations are provided by test NG and how we can use it and where we can use it and so on
00:43
Speaker A
So the next concept is annotations. We already discussed about the @Test annotation, and this is an annotation that must be used. So whichever method we need to make as a test, we can specify that method as a test annotation. Along with the test annotation, we can also provide the priorities.
01:02
Speaker A
okay based on the priority the test methods will be executed so when we can call is a method as a test method when you specify test annotation then only we can call that method is a test method and
01:15
Speaker A
Okay, based on the priority, the test methods will be executed. So when can we call a method a test method? When you specify the test annotation, then only we can call that method a test method. And whatever validations we do, we include all the validations only in the test methods. Only in the test methods, we can include the validations.
01:33
Speaker A
method and at the rate after method at the rate before class at the rate after class at theate before test at the rate after test after test and at the rate before suit at the rate after suit so these are the few
02:07
Speaker A
Okay, so apart from this test annotation, we have a few more annotations. So we will discuss one by one. So the next one is @BeforeMethod and @AfterMethod, @BeforeClass, @AfterClass, @BeforeTest, @AfterTest, @BeforeSuite, and @AfterSuite. So these are a few more annotations which we have in TestNG.
02:26
Speaker A
with before method after method anotations before method after method so let me take a small sample test here so based on that we'll try to understand where we can use before method and after method so for example I have different steps in my test case so in my first step I log to my application
02:47
Speaker A
So before method, after method, before class, and after class, before test and after test, before suite and after suite. Okay, now we'll see today what these types of annotations are, how we can use them, and where we can use them. First, let us start with before method and after method annotations.
03:05
Speaker A
normal prerequisite so on once I log application then I can do some more verifications let's say I want to verify a search functionality okay and then after that I want to log out from application once you done the testing of search functionality then I want to again log out from application and
03:25
Speaker A
Before method, after method. So let me take a small sample test here. So based on that, we'll try to understand where we can use before method and after method. So for example, I have different steps in my test case. So in my first step, I log into my application.
03:44
Speaker A
test this is actual test we want to perform and then I log out from application and one more time I log into application then again I do another test Advan search and finally allow word from application if you have a steps like this okay so some steps are repeated and there are some
04:02
Speaker A
But this is not the actual validation I want to do. I just want to log into the application. This is a prerequisite. But once I log in, I want to verify something. Login itself is one test case. We can separately create a test case which includes the validations. But here, this login is just a normal prerequisite.
04:23
Speaker A
search functionality then how to log out from application so this is the entry point this is the exit Point similarly to per from Advan search login is an entry point again log out is an exit point so if you look at these particular steps login is repeating multiple times even log out
04:42
Speaker A
So once I log into the application, then I can do some more verifications. Let's say I want to verify a search functionality. Okay, and then after that, I want to log out from the application.
05:00
Speaker A
we will Define these two methods as at theate test annotation this one and this one because these are the main test which we are going to do and rest of the steps are what prerequisite entry and exit criterias so what we can do is we will identify when this login is repeating
05:18
Speaker A
Once you have done the testing of search functionality, then I want to again log out from the application. And after logging out, one more time I log into my application, and then again I will do another test on advanced search functionality. And then again, I log out from the application.
05:42
Speaker A
we need to repeat the login multiple times before starting every test method in the class that is a login so in that particular case we no need to write a login multiple times so according to the test case we have to implement the login methods to two times actually but we don't need to write
06:02
Speaker A
See how I have framed all the steps. First, I log into my application. Let's say I want to perform a searching test. This is the actual test we want to perform. And then I log out from the application. And one more time, I log into the application. Then again, I do another test, advanced search, and finally log out from the application.
06:21
Speaker A
you have created your test case accordingly you have to decide so this is not mandatory every test case should be like this no according to the test case you need to figure out okay so which one is actual test we have where exactly the validation is included accordingly you need to decide so here
06:37
Speaker A
If you have steps like this, okay, so some steps are repeated and there are some prerequisites. Suppose what are the main tests we want to do? In this, there are two tests. You can create a login-logout test separately, but here, according to this particular test case, login is a prerequisite for search testing.
06:53
Speaker A
logout is an exit criteria but I don't want to do any validation on the log out I just want to do validation only on the search and Advan search if your test case is like this then you can identify
07:06
Speaker A
Similarly, here also, logout is exit. So once your testing is completed on search functionality, then how to log out from the application. So this is the entry point, this is the exit point. Similarly, to perform advanced search, login is an entry point, again logout is an exit point.
07:30
Speaker A
before search and before Advan similarly if you have another test before every test method I want to execute this lockin so before that what we need to do we need to identify the actual test methods we specify them as at theate test annotation and then we will identify which method is should be
07:49
Speaker A
So if you look at these particular steps, login is repeating multiple times. Even logout is also repeating multiple times. And when it is repeating, we need to identify.
08:09
Speaker A
execute log out one more time that means log out I want to execute multiple times after completion of every test method so can create logout method only once and that you can specify after method after method before method after method so when you specify this annotation what is an
08:32
Speaker A
So first of all, we will identify what are the actual tests we are going to conduct in these steps. So search is a main actual test, advanced search is another main test. So first of all, we will define these two methods as @Test annotation, this one and this one, because these are the main tests which we are going to do.
08:49
Speaker A
test similarly Advan logout also will execute multiple times after completion of every test method in the class so this is the functionality of the before method after method so before test after these are all having different context so we should not compare one annotation with
09:09
Speaker A
And the rest of the steps are what? Prerequisite entry and exit criteria. So what we can do is we will identify when this login is repeating.
09:24
Speaker A
not discussed about before test after test right hold on for some time right so this is my test case one so let us try to implement this we'll see how it is going to execute multiple times before every test method and log out will execute multiple times after every test method suppose you
09:42
Speaker A
So login is repeating before every test method. If you look at these tests, search is an actual test. So before that login, we have to execute. Advanced search is a main test, so before that login should be executed. That means login we want to execute multiple times before every test is started.
10:04
Speaker A
43 so inside this let's say new class annotations demo one and we should not take any main method because we are going to create a test NG classes okay now this is my class all right so inside this class uh I'm going
10:31
Speaker A
So we need to repeat the login multiple times before starting every test method in the class. That is a login. So in that particular case, we do not need to write login multiple times. So according to the test case, we have to implement the login method two times actually, but we don't need to write it multiple times. We can just write only one time, and that you can repeat multiple times.
10:55
Speaker A
features and then finally we will integrate the web driver script and from now I'm just going with the normal statements system. pm and this is so login and I'll create another method void uh search and here this is search and I'll create one more method void
11:31
Speaker A
When I want to repeat this before executing every test method, I want to execute this login. So according to the test case, you need to know that. So you have to read the test case thoroughly and how exactly you have created your test case. Accordingly, you have to decide.
12:02
Speaker A
log out okay now I have created all the methods related to all the steps but login I created only once log out also I created only once I not repeated multiple times so first of all we will specify main test this is the test annotation search is main test so here I'll put
12:23
Speaker A
So this is not mandatory every test case should be like this. No, according to the test case, you need to figure out which one is the actual test we have, where exactly the validation is included. Accordingly, you need to decide.
12:47
Speaker A
engine. annotations and log out I will specify after method after me so this also we need to import from org testng annotations okay so now we created all the methods but login I created only once even log out also created only once specify that as a before method after method so what I
13:10
Speaker A
So here, according to these steps, I want to do validation in the search and advanced search. But again, login and logout, you can create another test method in a separate class. But according to this particular test case, login is just a prerequisite. I'm not going to do any validation on the login.
13:28
Speaker A
once executed and see the the execution order so annotations will control the execution so once you specify the annotations you can create these methods whichever order you want so order is not not important in the last class also we have seen in which order you have created these
13:45
Speaker A
Similarly, logout is an exit criteria, but I don't want to do any validation on the logout. I just want to do validation only on the search and advanced search. If your test case is like this, then you can identify the main test, you can specify with the test annotations, and login is repeating multiple times before every test method. So I can specify this as before method annotation.
14:07
Speaker A
also no problem you can put these methods in whichever order you want but finally we will get the exe execution will happen based upon one annotation now I have just executed now uh see this execution first these are the four methods I have created five four uh totally
14:26
Speaker A
Before method. So why is it a before method? Because I want to execute this multiple times before every test method, before search and before advanced search. Similarly, if you have another test, before every test method, I want to execute this login.
14:38
Speaker A
executed after that log out is executed so this is one set is completed now again one more time login then Advan search and log out can see login is executed two times before every test method started log out executed two times after completion of every test method and if you see the
14:59
Speaker A
So before that, what we need to do, we need to identify the actual test methods. We specify them as @Test annotation, and then we will identify which method should be executed before every test method. So that we can specify as a before method annotation.
15:20
Speaker A
only for those methods why any guess why only it is giving test methods output it is saying only past only test methods other methods it won't provide why because validations we will include only in the test methods okay so test NG will consider only test methods are the actual test
15:43
Speaker A
Similarly, logout, I want to execute multiple times after completion of every test method. See, after the search method is completed, logout should execute once. And after advanced search is completed, then I want to execute logout one more time.
16:00
Speaker A
be populated in the console window in the tree structure only test method search and advanced search methods are displayed but we have created some output statement print statements right so that is the reason the print statements are got printed here okay they just printed here okay so
16:20
Speaker A
That means logout I want to execute multiple times after completion of every test method. So you can create logout method only once, and that you can specify after method.
16:41
Speaker A
you're not actually consider as a test but if you want to consider as a test you can create another test login login method we can put that as at theate test annotation there's a different test and which includes the validation whether login is successful or not but here as per the test
16:56
Speaker A
After method, before method, after method. So when you specify this annotation, what is an advantage is we do not need to write multiple times. So you can see login here, here two times are repeated, but we can write only one time and specify before method annotation. So that will automatically repeat multiple times depending upon the number of test methods which we have in your test.
17:16
Speaker A
after method so before method will execute multiple times before executing every test method in the class similarly after method will execute multiple times after completion of every method in the class is it clear to everyone please confirm in the chat box before method and after
17:35
Speaker A
Similarly, advanced logout also will execute multiple times after completion of every test method in the class. So this is the functionality of the before method and after method.
17:55
Speaker A
participate in the results but execution every method will exist but only results we will get only for test methods because ultimately we will see only test methods are passed or failed this is an execution part okay before method after method now we'll go to another level first we
18:17
Speaker A
So before test and after test are all having different contexts, so we should not compare one annotation with another annotation. So I'm just going step by step. You will understand once I complete all types of annotations, then you will understand the differences when we use them.
18:32
Speaker A
and then I will combine all of them then we will see the execution order how exactly they will execute so finally I will show you the hierarchy of execution okay but normally we don't use all annotation in one test or one class depends upon the scenario depends upon the test cases
18:48
Speaker A
by considering multiple factors we will try to use specific type of annotations but we don't put everything in one test there is no necessity like that okay so next one we'll see before class and after class before class and after class when we can use this before class and after class now I'll
19:07
Speaker A
try to change the same test case in the different manner let's say this man test I remove these annotations okay now I have a test case like first I want to log into my application and then I will
19:29
Speaker A
do search test and immediately I don't want to log out and I immediately do Advanced resarch only four steps first I will log to application and this is also prerequisite entry and I will do searching test and then I will do Advan search so both the tests I will do one after another and
19:51
Speaker A
finally my once my my testing is completed then I will exit from the application I log from application only one time so if you look at these par steps according to this test case search and Advan se are the main test that I want to perform so I can put them as a test annotation
20:10
Speaker A
and Advan search is also main test so this is I put as a test annotation now if you want to do these two tests what is a prerequisite login is a prerequisite so first of all I have to log to
20:24
Speaker A
my application that to only one time then I can do these two tests after completion of both the tests then I will come out from application so I want to execute my log out so here login is only one entry log out is only one exit so before execution of all the test I want to execute login
20:43
Speaker A
only once after execution of all the test I want to execute login only once so login is only once we will execute log out also will execute only once but when they will execute before starting all the tests login should execute after completion of all the test
21:07
Speaker A
logo should execute that's our requirement now where you will specify these two tests in the class right you will create a one class test NG class in the class all the test methods will be defined so if you put this login as before class okay before class and I'll put logo
21:29
Speaker A
is a after class log out as a after class then what happens is login will execute only once before starting all the test because all the test will be part of the class only right that means before class means this login method will execute only once before class before class in the sense
21:49
Speaker A
before starting all the test methods and log out will execute only once after completion of all the tests that means after completion of the class execution so before starting the class after completion the class before starting the class means what before starting all the
22:06
Speaker A
test methods after completion of class means what after completion of all the test methods because the class contains the test methods okay so according to this particular test case I want to execute login only once before starting all the tests I want to execute log out only once
22:26
Speaker A
after completion of all the test methods that means the login I can specify with before class and log out I can specify after class there is no reputation here only once it will execute before class and after class that's it in between if you have any number of methods test methods all the
22:46
Speaker A
test methods will execute but you can have 10 test methods but still login will execute only once log out will execute only once so this is a criteria now let us see practice Ally I will try to change the same stuff let's copy this I create another copy and this time I will try to change
23:12
Speaker A
this okay same method same step just I want to change The annotation so login I'll make it as a after test this is after class sorry before class okay and this is after class so rest of the methods are same search
23:50
Speaker A
and Advan search is exactly same now before class we'll import from a test. annotations and after class also we have to import port and if you don't have unwanted annotations you can just remove them okay or if you say organize Imports it will remove unnecessary
24:11
Speaker A
or unused Imports so now we have only three anotations so login are defined as a before class log out are defined as after class and rest of them are a two test methods now execute now see the console output first so these are the four methods got executed see
24:32
Speaker A
login executed only once then Advan search and search and then log out executed only once and if you see the result it is only test methods are participated so even before class after class other than the test method if you have any other methods which are associated with any other
24:50
Speaker A
type of annotations they won't be participate in the results but they will execute but they don't participate in the results so if I look at the same thing is happened login executed and two test methods are executed and then log out executed but results will shown only for Advan
25:07
Speaker A
search and search even if you see this result here search and Advan search so this is before class and after class means what the before class method will exib only once before starting all the test methods and after class method will execute only once after completion of all the test method
25:27
Speaker A
so this is a function of before class and after class clear everyone please confirm in the chat box so understood the difference between before method after method and before class and after class okay and one more important thing is suppose when I use before class annotation in the test so
25:49
Speaker A
there is no necessity of using after class not not mandatory so if you have a step like this suppose after completion of the search and advest if you have any exit criteria then I can put log out here after class I can specify or else you can specify only before class is enough after class is
26:09
Speaker A
not mandatory but if you have a step that you have to execute after all the test method then you have to specify after class otherwise in the same class you can use only before class alone similarly in the here also before test after test also suppose I don't want any test me if I don't want any
26:26
Speaker A
method which I don't want to execute multiple times after every test method so then I can use only before method so there is no rule like if I use before method you must use after method like if I use before class I must use after class there is no rule like this you can use only individually
26:44
Speaker A
depends on the test case you need to decide okay so this is before method after method before class and after class now let us discuss about before test and after test before suit and after suit so first two types of annotations these annotations we can use a class level in one single class is
27:04
Speaker A
enough but if we want to go next level before test after set and before suit after at least we need to have two to three test cases at least we need to have a two test cases that mean two
27:15
Speaker A
classes which we need to have because according to the XML we have to use it we need an XML file and then we can use before test after test before suit and after suit so before uh showing the functionality let us try to understand XML file one more time so we understood what is a
27:33
Speaker A
method what is a class but what is a test and what is a suit that we need to understand so if I ask you what is a test so normally in the test cases we specify the test methods right there are
27:46
Speaker A
independent test cases they are tests actually within the class but as per the XML file the definition of test is different as per the class what is a test which whichever method we specified the test annotation that is a test that is fine but according to the XML file the definition of
28:06
Speaker A
test is totally different so according to the XML file what is a test means it is a collection of classes collection of classes is called one test according to XML file so we'll try to understand the hierarchy once when to use before class when to use before test when we understood the class
28:24
Speaker A
and methods but we need to understand what is a test and what is a suit then we will see where he can use and how can use before test after test before suit and after suit so now let us go to an
28:35
Speaker A
XML file let me create a small XML file here so XML file we create from the suit tag some name my suit so what is a suit contains a suit contains a test tag test is also having some
28:50
Speaker A
attribute let's say my test and what's the test contains a test contains a classes tag inside the classes we will create a Act class let's say class name equal to some XY Z and closing class and I have another class let's say class name equal to some a c and closing the class after
29:11
Speaker A
that we will close the classes tag and after that we will close the test tag and after that we will close the suit tag so this is actual XML structure right now if I look at this XML structure so there are different levels are there so again inside the classes we can also
29:31
Speaker A
have a methods inside the classes we can also have a methods so this is the main hierarchy now before test and after test so first level is before method after method this is a class level next level is test level so where exactly the test we will be considered suppose in this XML
29:51
Speaker A
file if you look at this XML file how many test we have included in this XML file how many test we have included in this XML can you imagine how many test we have included in this XML only one okay because classes level we will consider this is one test this is another test
30:16
Speaker A
but we put everything in one test tag so this is actually one test okay this suit contains only one test so if you copy this another copy like this I put another copy okay and here this is my test one
30:33
Speaker A
and this is my test two and in the first test I include xyj and the second test I'll include only ABC now you tell me how many test we have in this XML how many test we have two tests this is one
30:50
Speaker A
test and this is another test in the first test I have included one class you can also include multiple classes even in the test two also have included one class you can also include multiple classes okay now if you run this XML file this class will be executed this class also will be
31:10
Speaker A
executed so two classes will be executed right and uh this first class is part of the test one second class is a part of the test two so now I have two independent tests now come to before test and
31:23
Speaker A
after test suppose in XY Z class or in ABC class in one of the class I have created a method which is defined with before test and another method is defined with after test so you can create those method either in XY Z class or ABC class wherever you want one method I defined with the
31:44
Speaker A
before test another method is defined with after test now when the methods will execute when those methods will execute so before test method will execute before every test is started after test method will execute after completion of every test XML level okay we need to understand from
32:07
Speaker A
the XML point of view so as for the XML I have a two test so before test method will execute only once before test method will execute before every test method started after meth after test method will execute after completion of every test that will also execute multiple times but before
32:28
Speaker A
every test methods will execute and after test method will execute multiple times after completion of every test method okay but when we will create those methods where we will create those methods before test and after test where we will create in XYZ class or ABC class can
32:46
Speaker A
you guess where we can create those two methods before test and after test you can create in any class either in XYZ or in ABC wherever you want want you can create so those methods will execute according to The annotation so let me show you this so for that reason so I need to have two
33:06
Speaker A
classes so uh I will create two different classes so for that I'm creating a separate package uh in the same day 43 I will create another pack let's a pack day 43. pack and inside this I'm going to create a multiple classes so observe very carefully little bit confusing but you
33:28
Speaker A
need to focus a lot I'll clarify questions later first of all listen and focus so I'm creating a new class just observe I'll name it as a C1 so in the C1 class in the C1 class I will create uh two
33:44
Speaker A
methods you can see I I created ABC is a normal test method and another method called before test method I created here before test method I created so this I already imported so one is a normal test method another one is a before test method in the same package I will create another class called
34:07
Speaker A
C2 and in the C2 I will create one test method and one after test method so XY is a test method and at is a after test method now look look at these two classes first so this is my C1 and this is my
34:33
Speaker A
C2 both are there in the same package okay fine so C1 is having their own test method called ABC C2 is also having their own test method called xyg now in the C1 we have addition method BT which
34:45
Speaker A
is actually before test so this is a before test method and at after test method I created in the another class if you want you can create both the uh methods in one class suppose I can put this here no problem or I can put both in another test another class no problem or you can divide
35:07
Speaker A
them into two different classes no problem because at the run time annotations based upon annotation it will decide when to execute that method okay so now I have two classes C1 and C2 in the C1 I created one test method along with the before test and the C2 I created one more test method
35:25
Speaker A
along with the after test so now I have these two classes now how we can execute multiple classes together how we can execute multiple classes together we should execute them through XML file XML file if you want to execute multiple classes we need XML file right so let's create an XML file
35:48
Speaker A
in the same pack C1 and C2 the XML should contains C1 and C2 let's create our own XML file first we'll create XML file automatically how to create an XML file for these two classes select the package right click and here you can see uh one option called test NG here you can say convert to
36:11
Speaker A
test NG so where you want to create this XML file you just select the path can click on the browse selenium project SRC test Java and my package name is day 43 inside the day 43 pack select and then
36:32
Speaker A
say finish okay now if you look at that XML file which is got created so this is my XML file and you can see this is my suit so this is my suit XML and there is only one test as of now you can look
36:50
Speaker A
at this I have only one test and which contains a C1 and C2 okay now just observe I'm running the test and before test after test annotations we already kept in these two classes okay I have only one test as of now if you have a one test just observe the behavior you can just run as a test
37:14
Speaker A
in the suit yeah now if you look at the console window so before test method is executed only once before starting these two actually these two are included in one test actually see C1 and C2 are included only in one test so C1 C2 collectively we can consider as one test so before starting
37:37
Speaker A
the test before test is got executed after completing the test after test method is got executed this is our expected now I will divide these classes in two different test so I'll keep another test entry okay now whenever you create a multiple test entries the name should be different
37:59
Speaker A
okay I can consider this is my uh test one and this is my test two okay now here I can keep only C1 I remove the C2 and here I'll keep C2 I remove C1 now how many test we have two tests now
38:16
Speaker A
how many times before test method will execute two times will execute before first test before second test similarly after test method will execute put two times after completion of the test one after completion of the test two now observe the result I'm running as a test yes I not saved
38:41
Speaker A
it test one and test two C1 and C2 let me run it so it is still executed before test method only once after test method also only once we go back to the results so here it will show you only
39:05
Speaker A
C1 and C2 so let me remove this threads I think this will exud only once just a moment okay so it is executed only once so this is my first test and this is my second test so before test method and after test method so this
39:37
Speaker A
before test after sorry guys I done some mistake actually in just a second go to C1 this is a before test go to C2 so this is after test okay let me save XML file then execute okay so they have executed only once so before test method he's executed only once before
40:19
Speaker A
starting the all the test so after test method also we executed only once after completion of all the tests so it is not executed multiple times guys remember this this will not executed multiple times only once but before starting the all the test after completion of the all the test now if
40:36
Speaker A
you see the difference between before class and after class do you see any difference between these two do you see any difference before class and after class is a class level single class whereas before test and after test is a multiple classes because in the first test we can include
40:55
Speaker A
multiple classes in the second test I can include multiple classes so just observe I put this uh C2 also in the test one and also I'll put C1 in the test so now test one and test two contains the
41:11
Speaker A
same test cases okay but now just observe how many times the before test after test method will ex still one time if I look at the output see this is the before test method and after test method you
41:25
Speaker A
can see now the this time it is executed multiple times why because this time we have included multiple classes see this this is one execution from the test one and so this is another execution from the test two so this is one round of execution this is another round of execution so if
41:50
Speaker A
you put only one single class in the test one and test two so before test method after test method we'll execute only once okay before starting all the test after completion of all the test but if you have a multiple classes or multiple classes you specified in the test so if you look at here
42:08
Speaker A
before test method is executed two times and after test method is executed two times this is the behavior so if you have a one test then one test in each one class in each test then before test method will after test method will execute only once suppose if you have a multiple classes
42:27
Speaker A
in each a test then before test and after test method will execute multiple times so this is a Behavior now if you look at this before starting the test before test executed after completion of the test after test is executed this is one round of execution now again before starting the test
42:47
Speaker A
two before test is executed after completion of the test to after test method is got executed so this is how the before test after test is got executed so first level is a method the next level is a class above the class is a test so these are the three levels so test contains a
43:09
Speaker A
class class contains a method right according to the XML similarly execution also will happen like this so method is a lower level next level is a class next level is a test so what is the next level high level above the test suit suit is a high level in the last level suppose
43:28
Speaker A
what I want to do is before executing these two test that means before starting the suit and after start after completion of the suit I want to execute few more methods then I can specify in any class now I let me create another class let's say I'm creating new
43:48
Speaker A
class and I call it as a C3 and in the C3 class I'll create few more methods in the C3 I'll create one test method and this is after suit and also I will put before suit you can put these methods in wherever
44:15
Speaker A
you want no problem so I can put here this is a before so this is after suit this is a before suit call it a BS so order is not important so you can write in whichever order you want
44:36
Speaker A
and you can write in whichever class you want I put like this okay now if I look at this this is my C1 and this is my C2 and this is my C3 three classes I have in the
44:52
Speaker A
C1 I have one test method C2 also have one test method C3 also have a one test method called pqr now before test I before test after test I have divided in two different classes okay and then uh C3 I put both before suit after suit I kept in C3 itself okay so
45:15
Speaker A
now yes you can have a multiple suits also okay now let me close all of them now go to XML file now currently I have a one suit now what is my requirement is before starting the suit after
45:31
Speaker A
completion of the suit I want to execute few methods so I already created those methods in the C1 and C3 so in the suit I have multiple test cases now observe this is my suit I don't need to
45:42
Speaker A
change anything but I will add also add C3 so here I will add C3 and also here I will add C3 okay now I have a two test test one and test two test one contains a C1 C3 C2 C3 I want to executed
46:01
Speaker A
and again I want to execute C1 C2 C3 as part of a test two these two are the part of the one single suit okay now we'll see the suit level execution and test level execution also execute yes now look
46:17
Speaker A
at the console window and base this is this we need to understand very clearly just a second yes so look at this how exactly it is executed see this is a suit level after suit uh see after suit okay I think I have not changed that uh name one second let me
46:51
Speaker A
change so let's go to C1 so this is before test okay this is also C1 before test C2 after test so this is before suot okay this is after suot okay now go to the XML file so okay let's have it one single class okay C1 C2 C3 C1 C2 C3 okay fine perfectly fine so
47:22
Speaker A
C3 what is the problem in this yeah so this message is wrong actually this is is before suit okay so this is before suit all right so let's go back and execute XML file one more time okay now let's try to understand this so before sort and after Su executed only one so
47:51
Speaker A
this is very high level execution so before starting XML after completion of the XML now before test after test before test after test two times executed one time and second time why because I have a two tests in my XML file okay now in the first test we have a three
48:14
Speaker A
classes C1 C2 and C3 so C1 C2 C3 are executed and again same C1 C2 C3 are the part of the test two so here also C1 C2 C3 are got executed so this is a hierarchy means suit contains a test test
48:30
Speaker A
contains a classes okay inside the classes methods are created so these methods are got executed so this is hierarchy so before method after method we execute at the method level before class after class will execute at the class level before test after test will execute at the test level
48:49
Speaker A
before suit after suit will execute at the suit level so this is the hierarchy so now we will combine all annotations in one class only one single class I will take I'll put before suit after suit before test after test before class after class before method after method and we
49:06
Speaker A
will run through XML file then we will see the exact order order of execution so let me close this okay now I will create one single class which contains all types of annotations I'll I'll name it as all annotations
49:29
Speaker A
and finish it so in this I will put all types of annotated methods one after another okay just import all these uh packages so instead of importing each individual package simply you can put star because all annotations are part of annotation package so when you put start one single input is enough
50:00
Speaker A
which is applicable for all the annotations okay now just observe one single class I created this is before suit after suit and this is before test and after test this is before class and after class and this is before method and after method and I have two test methods T1 tm1 tm2
50:23
Speaker A
these are the two test methods and all methods all the levels I specified now once you executed we'll see in which level it will execute so we will create an XML file to execute this because the test level and then suit level if you want to see those effect we need to run through
50:39
Speaker A
only XML file okay so you can also run this directly uh if I just run as this uh you can see before suit after suit also will execute because this is all considered as one suit by default so that's the reason we got an output here and if you want to run through XML file
50:57
Speaker A
you can also run through XML file so for this I'm creating a new XML file or automatically can create and just say test NG convert to test NG and I want to create this under test Java day 43 okay so this is the XML file which is got created only one suit one test one class which
51:30
Speaker A
contains a class okay let me execute run as testes okay now you can see in the results only test methods are participated only two methods even in the console also only two test methods have got participated now execution so we'll see the hierarchy how exactly they have executed if
51:51
Speaker A
I look at this first one suit is a high level so before suit after suit and uh before test method so this is a before test and here it is after test before test after test and then before class after
52:14
Speaker A
class okay where is a before class after class this is a before class where is an after class here it is and before method after you can see before method after method executed for every test method so this is one round this is one because before method will execute multiple times before
52:34
Speaker A
every test method so after test after method will execute multiple times after every test method so so before method after method before method after method above this we have a class so before class after class above this test before test after test Above This suit before test after suit
52:54
Speaker A
so this is a hierarchy so first is a suit then test then class then is a method so before method after method will execute here and class contains multiple methods so before class after class and here before test after test this is before suit after suit so this is the hierarchy of executing
53:23
Speaker A
all the annotations so ultimately what we need to understand from this is annotations will control the order of execution and control annotations will decide when to execute those methods and other than test annotation rest of the annotations will be participate only in the execution but
53:44
Speaker A
they will not be participate in the results okay so according to your test case according to your requirement whichever annotations are suitable you need to use the type of annotations so we don't use all inations in one single shot or one single test according to your test cases you
54:03
Speaker A
can create different type of annotations normally when you create your test cases you will have a main suit let's say you have a sanity test suit sanity suit in this you will can have only sanity test cases let's say 1 2 3 4 and in each and every test case there are different methods test methods
54:24
Speaker A
are there so this is all one group right so before suit after suit you can keep here and Sanity if I consider is a class so before class after class or before test after test you can put here and
54:38
Speaker A
for every method before method after method you can put inside the class so first you need to thoroughly understand the test case then only you will know exactly what type of annotations we can use in your test so that's the most important so understanding the test case understanding the
54:55
Speaker A
steps is most important important before using annotations concept okay so this is all about test NG annotations hierarchy and if you look at the picture if I just type Google test NG annotations and go to any image you will see the hierarchy like this so test is a a bottom level
55:21
Speaker A
then before method after method will execute then test method this is a test method okay so before before test method before method will execute after completion of test method after method will execute so above this before class and after class above this before test and after test above
55:37
Speaker A
this before suit and after suit okay so this is a hierarchy of the annotations clear everyone so far so I put all annotations in one single class that is this example so before suit after suit before test after test before class after class before method after method and two test methods and then
56:03
Speaker A
I have shown you how exactly they have executed in which order they have executed so if you run through XML file or normal XML normal class see before suit executed first and after suit executed and the last because here we only have a before suit there is no after suit because we
56:21
Speaker A
haven't run through XML file so we should execute through XML file run as test suit so now you can see before suit after suit inside this before test after test and inside this before class and after class before method after method before method after method because we have two test methods
56:44
Speaker A
so before method after method will execute two times because we have a two test methods and we have only one suit so before suit after suit we have only one test so before test and after test we have have only one class so before class method and after class method okay this is a
57:02
Speaker A
hierarchy same whatever I have shown you uh in the picture same thing you can see so you can see this one you can see this is the same hierarchy we followed so first test then before method after method before class after class before test after
57:26
Speaker A
test before suit and after suit so this here also it is following the same hierarchy first level is a method so this is a method where is the actual test method this is the test method one this is the test method two now observe before and after before test after before
57:44
Speaker A
method after method again here before method after method about this before class and after class about this before test and after test above this before suit and after suit so this is exact hierarchy yes if you have a multiple suits are present then we can use before suit and after
58:08
Speaker A
suit so most of the times we don't have such type of requirement most of the times we don't we don't have we never see the such type of requirements because we will maintain the separate suits okay one multiple XML files we will maintain so each XML is considered as one suit actually so that's
58:27
Speaker A
the reason very rare cases we I've never seen to execute before suit after suit that means execute before executing all the suits after completion of all the suits there is no such kind of a scenarios most of the times because we will consider individual suit this is one suit
58:42
Speaker A
for grouping this is another suit for parallel execution this is another suit for executing the test based on the priority and another suit for master which contains everything so we'll maintain separate suits we don't combine all the suits into one suit most of the times okay so
59:00
Speaker A
this is this is all about annotations Concept in the coming sessions so you will understand more about this because as and when we required we will try to use different type of annotations so this is just we understood at very high level all kinds of annotations of hierarchy all these
59:16
Speaker A
things and once start using these annotations you will understand what exactly they are and how exactly they will work okay now let us move on to the next concept very very important Concept in test NG called assertions assertions so what is the meaning of assertion assertion means
59:39
Speaker A
a validation point or we can say validation so whenever you want to do some validation in test NG method we have to use something called assertions okay so before using assertions we will try to understand why we need assertions so normally how we have done validations earlier in
59:59
Speaker A
the previous classes we have done different type of validations right so how we have done those validations suppose I have a actual title and my expected title how we have done validations we have just compareed them by using IF condition if so and so title equal to or if expected title
60:18
Speaker A
equal to actual title then test is passed or is fail we have put only condition statement but when you put the conditional statements there is a problem in that okay we will see what is a problem and how we can you overcome that by using assertions so I will create a small test
60:37
Speaker A
just observe I'll show you the problem with the conditional statement I'll name it as a assertions assertions demo okay so uh how we have done earlier uh sessions are totally different so let me put like this let me create one simple test method void uh title test or void test title I just want to verify
61:25
Speaker A
some title of the web page okay I can create test method like this so test title so make sure in test NG also we have to follow certain uh naming conventions whenever you create a test method the method name should always start with the test keyword so that is representing it is
61:43
Speaker A
a test method okay so when I said test title what does it mean this particular test method is going to test the title of the web page so I'll keep this as a test annotation and here I'm taking two
61:58
Speaker A
variables let's say string let's understood let's say actual or expected title expected underscore title equal to suppose I say open cut this is my expected title and let's say I have captured my actual title from application let say actore title equal to let's say open shop okay let's
62:21
Speaker A
say I have two different titles my expected title is open cart but actual title which is captured from application is open shot or open cart open shop okay now I want to verify these two titles are same or not and accordingly my test should pass or else my test should fail now in the
62:42
Speaker A
previously how we have done by using IF condition let me do like this if expected underscore title title dot equals because both are strings I can compare actual title okay then I can say system.
62:58
Speaker A
out. pinten test passed else system.out.print and test failed okay now this is a test method I have implemented so let me just take both are same for now I say both are same actual title expert title both are same now I'm comparing both the titles if both are equal my test will pass both are not
63:25
Speaker A
matching then my test is SP this is my test so now tell me when I execute this test will it pass or fail when I execute this test method will it pass or fail can you imagine will it pass or
63:39
Speaker A
failed just basic understanding so we have done this many times in the previously right before so as per our expectation it should pass because both the titles are correct exactly the same so if condition should match so my test should fail should pass so let me execute this run as a test
63:59
Speaker A
NG test see the message whatever you have given here testies got passed so both are equal titles are matching so testies got passed I got a message and also I got something else here past what does it mean the test title is what the name of the test method so it is saying past so no problem no
64:22
Speaker A
objection everything is good so whatever condition we validated that is also perfectly executed and my test method is also got passed perfectly fine now I'll just change this open shop okay my titles are different so now according to these values what is our expectation my test should be failed
64:46
Speaker A
my test should be failed because condition will never reach just observe when I run as a test Ng observe the output see test failed that means if condition is also executed perfectly fine because both are not matching so test is got failed now we can see this one what about the test method test
65:14
Speaker A
method should also fail right test method should also fail but it's not failed it is still passing it so what we need to understand based on this only whenever the test method is got passed or failed only that result will be populated in the report okay if you look at this tree structure
65:35
Speaker A
also see it is saying it is passed it should not pass right it is saying here it is passed even if you see the testng report or any other report it will show you only past what does it mean test
65:48
Speaker A
NG will take the result of the test method it will not consider what exactly you have done internally okay so the test method status will be updated in the reports and results in everywhere so internally even though this conditional statement is got failed our test method is still passed this
66:10
Speaker A
is the problem when I use conditional statements as part of validations okay so how we can overcome this problem for that we use something called assertions okay by using assertions we can make the test method passed or failed based upon the condition even assertions also we can validate the
66:32
Speaker A
conditions so now let me show you how we can solve this problem I will comment this if condition if else condition then I'll put one assertion okay how to put assertion in the test NG is by using assertion class there is a predefined class in testng it's called assertion and inside this in
66:53
Speaker A
assertion class there are so many typ typ of methods are there sorry assert is a class not assertion assert is a predefined class inside this there are so many methods are there let me write once again Asser dot you need to put Asser dot when I say dot you will see all the methods just a
67:15
Speaker A
second asset is a a predefined class like let's say I'm trying to write Asser dot you can see there are so many methods you can see ass doert equals Asser do assert not equals there are so many assertion methods are there so I'm taking one method called assert do assert equals and
67:42
Speaker A
here I will pass two parameters Boolean parameters it will take asset. asset equals and I'm passing this expected title as a first parameter I'm passing actual title as a second parameter okay so what this will do asset. asset equals method will compare both the values after comparison if
68:06
Speaker A
both are equal then this assession will make my test method pass okay if these are not equal this will make my test method failed okay so let me try to execute now when I run as a test n test see
68:26
Speaker A
currently both the titles are not equal so what it is saying it is got failed previously it was showing pass but this time it is getting failed test title is got failed why it is failed we are expecting this one but actually found is this one so both are mismatched that's the reason it's got
68:44
Speaker A
failed so our assertion can make failure of our test method but our conditional statement cannot make our test method failed okay suppose if you put both are same open cut open cut now ass will definitely will pass see my test method is got passed even in the result also it will show pass
69:11
Speaker A
okay suppose if you a different one previously the results also the tree structure also showing pass earlier but when I change these two definitely it will fail after failing if you go back and see here see it is got failed the failure case it is showing along with the exception
69:30
Speaker A
so this is the usage of using assertions okay so instead of using print statement instead of using conditional statements most of the times we try to use assertions so only assertions can make our test methods passed or failed only that particular status will be logged into the reports okay so we
69:53
Speaker A
don't see these kind of outputs in the reports we will see the only assertion outputs in the reports yes if you have a multiple test methods in the single class each test method is having different type of validations you can put multiple assertions in multiple test methods okay and one
70:14
Speaker A
more important thing if you still want to use assertions along with the conditional statements you can still use it okay for example I just still want to use conditional statement I still want to use conditional statement but what you can do instead of putting print statements you can
70:34
Speaker A
use assertions so ex if both are equal I'm saying test passed right in the same way you can use one assertion here asset do asset true and here also pass true so this is another type of assession so
70:50
Speaker A
what this assertion will do here we are expecting true and internally we are passing true both are true so this will make my test method past and same thing I will write it here s. as true I'm passing false what this will do this particular I'm expecting true here but I'm passing false both
71:13
Speaker A
are different so this assertion will make my test method failed so if you want to use assertions along with the conditional statement you can still use it this is the assertion this is another type of assertion here this assertion will compare two values okay and this Asser true as true
71:33
Speaker A
this assertions will take a Boolean value as a parameter and this is also valid now just observe both are totally different but still instead of passing my test it will fail it that's our expected now you can see test title is failed but earlier when I use only if condition my test is
71:55
Speaker A
still passed but when I use conditions along with assertion it will also make my test method failed okay so you can use either directly one assertion one single statement is enough most of the time we go with this otherwise if you still want to print some messages in the console window and
72:14
Speaker A
so on then you can go with this one conditional statement along with assertion But ultimately we should use only assertions assertions only can make our test method pass or fail properly only that particular status will be populated in the reports so
72:30
Speaker A
conditional statements output only display in the console window this will not project in the report but only the assertion results will be populated in the report okay so everybody's understood when I use conditional statement what is a problem how we can
72:47
Speaker A
overcome that problem using assertions so I will discuss number of types of assertions different type of assertions are there I will discuss in the next example so asset is a predefined class which contains different assertion methods various type of assertion methods we can do different type of
73:05
Speaker A
validations yes so conditions will create a conflict at the report level it's true yes okay now let us discuss what are the different type of annotations are there in test NG there are all annotation there are hundreds or 50s hundreds of annotations are there all of them
73:31
Speaker A
is categorized into two main categories okay two kinds of assertions two kinds of assertions are there first type of assertion just now whatever we have seen they all comes under hard assertions and another type of assertions are called Soft assertions hard assertions soft assertion
73:56
Speaker A
so we will try to understand hard asss first and then later I will show you soft asss so most of the times we use only hard assertions I'll show you why so whatever assertions we are accessing through asset class all of them are comes under hard assertions okay let me create new
74:15
Speaker A
class hard assertions and whatever methods are there in hard assertions same methods are in soft assertions also but a slight difference is there I'll tell you what is the difference so let me create one class hard assertions demo and here I'll try to use a few types of annotations
74:36
Speaker A
few types of ass sessions and based on that we'll try to understand so I'm just creating one test method and all these annotations we put only in the test methods we should not put in any another annotated methods okay so now let me show you some assertions some sample
74:53
Speaker A
assertions all assertions are accessible from asset class so my first assertion I say asset dot asset equals so what this assertion will do is it will take uh this assertion will compare two values it will compare two values if both are equal my test will pass if both are not equal my
75:18
Speaker A
test will fail yeah asset is from testng you can see here so org. testng from test we have to import this asset class and make sure the same class is there in J unit also at the time of importing you have to carefully import this same annotations are there in junit also so if
75:37
Speaker A
you put the cursor here it will show you all different packages you can see or test NG and log forg different uh things it will show you so you need to carefully import from org. test only okay now asset. asset equals this method will compare any two values if both are same my test
75:59
Speaker A
will fail test pass if both are not same my test will fail and what type of values it will take all kinds of value we can compare two strings we can compare two numbers we can compare two Boolean values anything so let me put two strings here for example let say XY Z and second value
76:19
Speaker A
also I'm taking s so both are equal so what this method will do it will compare these two and if both are equal my test will fail if both are not equal my test should fail so when execute
76:32
Speaker A
it both are equal so my test is got passed suppose here XY Z 1 I put so definitely my test should fail so my test should fail here you can see my test is got failed so asset doet equals We compare
76:51
Speaker A
the strings and also asset do set equals we can compare the numbers also if it is a string we put in the double quotations suppose if you want to compare the numbers let's say here I'll put 1 2
77:04
Speaker A
3 here I'll put uh 3 4 5 so both are numbers still it will compare if both are not equal my test method will failed see now so expected is 345 and found is 123 so it is got failed so
77:19
Speaker A
comparison we can also Compare the numbers okay and suppose if I put like this now you tell me S doet equals here I'll say ABC here I'll say one 2 3 one is string one is number will it compare one
77:39
Speaker A
is string another one is number so let's execute and see it is still false so it is a number it is a string both are not equal anyway it is compared okay so for example let's say uh I I I'll write
77:58
Speaker A
like this suppose I say 1 2 3 in the string format 1 2 3 in the numeric format now tell me whether it will compare or not true or false here 1 2 3 is the string format here 1 2 3 is a number
78:15
Speaker A
format so it is still failed why it is failed because it is a string right it is a number there is a difference so two strings we can compare two numbers we can compare but one string one number
78:36
Speaker A
also you can compare but result will be false okay if both are numbers there is no problem like this but here 1 2 3 is a string that we cannot compare with the number so this is asset do asset equals
78:53
Speaker A
and the opposite method is also there asset do asset not equals suppose here I'll say asset dot asset not equals not equals negation negation kind so any data types can be compared which e yes all data types are accepted in asset do asset equals similarly we have asset not equals opposite
79:17
Speaker A
now you guys can tell me the output here I say 1 2 3 here also I say 1 2 3 now tell me what is an output of this true or uh passed or fail or true or false Asser do not equals I'm expecting
79:33
Speaker A
what not equals I'm expecting but here both are equal right but I'm expecting not equals but it is both are equals then obviously it will return it will return false it will fail fine so execute so definitely it is failed we are expecting not equals but both are equal that's
79:58
Speaker A
the reason it is getting filed okay now see this we expecting not equals and here 1 2 3 and 3 4 5 now this time it will pass why because we're expecting they should not be equal also they
80:18
Speaker A
are not equal 1 2 3 3 4 were not equal so we are expecting not equals and the values also not equals so so ultimately our test will pass because expectation and actual both are same so my test should pass why it is not okay I should comment this the first one is got
80:37
Speaker A
executed so my test is got passed so 1 2 3 4 three are not equal I'm also expecting not equal so both are passed asset do equals asset dot not equals similarly we also have some more methods asset
80:56
Speaker A
dot asset true so this method will take a Boolean expression as a parameter asset to true we are expecting true here but here I'm passing also true then what is the output of this fail or passed definitely it will pass why because we expecting true I'm also passing true so definitely that will
81:20
Speaker A
pass okay and suppose asset asset true but here I'm passing false false I'm passing definitely it will fail because I'm expecting true here but internally I'm passing false a Boolean expression we are passing so definitely it will fail so based on that suppose you can tell me I can say asset
81:46
Speaker A
dot asset true sorry asset do asset true of 1 = 2 tell me passed or failed instead of Boolean value I can put the expression right because this expression will also return the Boolean value this expression is also return
82:15
Speaker A
the Boolean value I'm using relational operator relational operator is always returning the a Boolean value so here I'm expecting true but 1 equal to two is true or false false so true or false is conflict so definitely it will fail okay same thing if you write like this s do
82:39
Speaker A
true I say 1 = to 1 so I'm expecting true here and the value is also true 1 equal to 1 is equal to True will return so this will definitely pass so this is Asser to do asset true asset do asset true similarly the opposite asset do asset false is also there can
83:03
Speaker A
say asset dot asset false and here I'm passing 1 equal to 2 now tell me what is an output of this asset. asset equals asset. asset false so I'm expecting what false I'm expecting and 1 equal to 2 is what False only false and false both are true so ultimately this will pass and similarly let us
83:30
Speaker A
take this one as doet false and here I say 1 equal to 1 so here I'm expecting false but 1 equal to 1 is a true both are different so it should fail it should fail okay so these are all few types of
83:52
Speaker A
annotations and to fail this we can also have have one more anotation called suppose without putting any expression or without putting any condition I just want to fail my test method intentionally I want to fail my test method but for that I don't have any condition or any matching
84:09
Speaker A
or anything I just blindly want to fail my test method simply what you can do is we have one more method called asset dot fail so this method will directly fail my test method without having any verification or validation or condition anything is not needed you can simply just fail my test
84:31
Speaker A
method see it is just failed so these are all few annotations which we have and we can use them in our Automation and only through automations only through assertions we can do validations okay so now these are all comes under hard assertions these are all comes under hard assertions hard
85:00
Speaker A
assertions are the methods which we can directly access from the asset class so these are all static methods that's the reason we're able to access from the asset class directly static methods we can directly access through classes right no object nothing is required so these are
85:16
Speaker A
all hard asss but there is a problem in the hard assertions so let me tell you what is a limitation which we have in the hard hard assertions so that's the reason sometimes we prefer to use soft assertions and tell you I'll tell you when to use hard assertions when to use sort of sessions so
85:34
Speaker A
before that let us try to understand the what is a problem with the hard ass sessions and why we how to use soft as sessions so let me create another class and this is most important uh concept during
85:49
Speaker A
interviews they will definitely ask what is the difference between hard session soft sessions and when to use hard assists when to use soft asss we need to understand hard versus soft assertions okay now to demonstrate the problem with the hard assertions I will
86:11
Speaker A
create a small method here void uh I'll name it as hard assertions okay I can say testore hard assertions this is one test method okay so we will try to understand what is a problem with the hard sessions so here I'm typing some message let's
86:36
Speaker A
say this is testing some statement I have printed and other than other than this I'll put some more statements or two statements after that I put one assertion here asset dot asset equals you can put any type of assertion I can say 1 comma 2 fine after putting this assession I also have few more
86:57
Speaker A
statements here I'll put some more statements so here I'll say again testing after the assession I will put some more statements like this okay now when I run this code first two statements will execute and if assertion is got passed so rest of the statements will execute perfectly fine but if
87:23
Speaker A
the assertion is got failed if the assertion is got failed then rest of the statements will not be executed so if the assertion is got failed rest of the statements will not be executed that is a problem with the hardest session so let us try to execute let me take a positive number 1 comma 1 so
87:43
Speaker A
we are expecting equals one and one is also same so we are expecting the test should pass and all four statements should be printed in the console window if I look at here all four statements got executed print statements are executed first two next two and my assertion is also got passed this
88:01
Speaker A
is perfectly fine now suppose when I make my assertion failed then only first two statements will execute as soon as this is got failed rest of the statements will not be execute when I execute this observe yes see only first two statements are executed when hard deis is got failed rest of
88:25
Speaker A
the statements are not executed this is a problem with the Hardis so normally whenever you create a some test method the last statement should be an assertion after putting an assertion you don't want to write anything most of the times we don't write anything because in the test case what is
88:45
Speaker A
the last step of the test case most of the times is a validation right so after completing all the steps in the last step we do some validation same thing put in the test engine method so most of the times 99% of the times in the test method the last statement should be assertion
89:04
Speaker A
so even though assertion is got failed that will not impact anything else that will work as usual okay so no problem in that but in case suppose I have few statements I want to still execute after the assertion also even the ass is got failed I just want to execute few more statement
89:25
Speaker A
then you need to go with the soft assertions hard assertions cannot use but that is very rare case only 1% scenario but 99% of the times we use only hard assertions why because hard assession is a last step last statement in the uh test method so even if it is got failed it
89:48
Speaker A
will exit that will not impact anything okay so this is the problem with the hard assession so after hard assertion is got failed if you have any other statements to be execute so those statements will not be execute so this will automatically exit from the execution this is hard
90:08
Speaker A
ass okay now how we can overcome this problem with the soft assession so first of all let us understand what is a soft assession let's create another method I will try to I will try to do the same thing with the soft assertion void testore soft
90:30
Speaker A
assertion okay now just observe very carefully I just copy the same code okay first one I commenting I will not run this code now come to the soft assertion in the soft assession we don't access this method directly from the asset class so method names are exactly the
90:52
Speaker A
same so whatever methods earlier we discussed in the hard assertion asset do asset equals asset do not equals asset true asset false same methods are there in soft assertion what only difference is these methods we we have to access from an object we have a soft assert
91:10
Speaker A
class okay we have a predefined class just like asset class we also have a soft asset class so for the soft asset class we have to create an object let's say sea equal to new soft asset okay now we need to import this soft asset class from org testing. assets now instead of
91:34
Speaker A
using this asset we have to use this object this is called Soft assession same methods nothing is difference only one difference is hard assertion methods we can directly access from assert class whereas soft assertion methods we can access from the object of soft asset class that's the
91:58
Speaker A
only difference and whatever methods are there in the asset class same methods are there in the soft asset also the only difference is hard asset and Method we can access from the asset class and soft assertion methods we can access from the object from soft Asser class okay now this is my soft
92:18
Speaker A
assession okay now I put some more statements just observe how it's going to execute I'll put test annotation for this and previous one I already commented so this will not be executed So Soft Asser is a not a static class okay this is a a class and these these are all not a static methods
92:37
Speaker A
so whatever methods are there in the soft asset class they are not static methods so that is the reason we have to access only through object okay but in the asset class methods are static methods so that's the reason we're able to directly access from thetic class but in the soft ass
92:54
Speaker A
class methods are not static so we have to create an object right now let us execute this soft assession we will see whether after the statements our soft session will fail even though it is got failed rest of the statements are executing or not we will see that so run as test NG test okay now
93:17
Speaker A
we can see that see you you notice a different Behavior I put 1 comma 2 my soft assess should fail but what happens it has got passed and moreover it is also executed all the statements okay executing all the statements we that we are expected because we use software session because
93:40
Speaker A
of soft assertion fail still it will execute the rest of the statement so that's the reason we prefer to use soft assertion so this expectation is fine all statements are got executed but what about this one this is the main important thing right but soft assession should fail but what
93:58
Speaker A
happened here it is past so what is the problem in this is whenever you use a soft assession at the end of this method you have to write one more method additional method is required that is SCA dot Sea Dot assert all this method is mandatory okay so you can use soft assession that's fine but
94:28
Speaker A
end of this method you have to use S aall method additionally otherwise this will always show past soft assertions will always pass by default okay so if you put this ass at all then only this will do the right validation that is a rule okay so now when I execute this observe this very carefully
94:54
Speaker A
see this time all four statements got executed also my ass is got failed so even though my ass is got failed still rest of the statements are got executed perfectly fine if you see the tree output also it is showing failed so that is a usage of soft assertion so you can put any
95:16
Speaker A
number of asss but at the end of the test method you put only one method called sa. asset all okay so this asset all is especially for soft asss in hard asss we don't need this in hard asss we
95:34
Speaker A
don't have such type of methods because if hard assession is got failed it will exit rest of the statements will not be executed but what is our requirement if the assertion is got failed still I want to execute the rest of the steps but what is the side effect of this is my test is
95:54
Speaker A
passing even though I put invalid expression my test is passing it is executing all the steps but other impact is what my test is passing that should not pass that is the reason additionally we should use asset all method so you can use any number of soft assertions but at end of
96:11
Speaker A
the test we should use asset all method we have to call this and then only you will get exact output so can we create soft asset method at the class level yes you can create okay but the problem is the objects you cannot create right objects you can create only in the
96:32
Speaker A
main method or in the test methods so inside the class here you cannot create soft asset essay equal to new soft asset you cannot do like that but the variable you can create you can say soft asset SCA you can put variable like this and this variable you can initiate in one of
96:48
Speaker A
the methods here we can initiate but the object we cannot directly create in the class that a problem the variable you can create but object you can can create because here we don't have a main method objects can be created only in the main method but here we don't have
97:06
Speaker A
a main method alternative is a test method so only in the test method we can create an objects so this is the difference between hard assertion and soft assertion everybody's clear if anybody ask you during interview you should explain very very clearly what is hard ession
97:24
Speaker A
what is soft session what is the problem with the hard session and what is the advantage of soft session so very very important concept but most of the times we manage with hard sessions so we don't need to go with the softare sessions but if you still want to execute
97:41
Speaker A
some more statements after the software session uh you can go with the software session so can we create soft asset method at the class level I said no right we cannot do we can create a variable soft asset is a class it is again basic Java
98:01
Speaker A
fundamental soft asset is a class that means we can create an object so where we can create an object of the class either in the main method we can create but here in the test NG we do not have a main method so alternatively we can create inside the method
98:18
Speaker A
only in the test method we can create object of the software set and by using that object we can access all the methods from the soft asset but if you want to use the soft ass variable commonly for all test methods then you can create soft asset variable like this and that
98:37
Speaker A
you can initiate in multiple methods that is a context soft asset method at the class what is software set method you cannot create any software set methods because already methods are available in the software set class class you can access and you can use it that's it we don't want to
98:57
Speaker A
create any new methods in the software set class not in a method level or class level whatever it is we are not going to create any new methods in the software set this is a predefined class and these are predefined methods so we just want to access them and use it in our automation that's
99:14
Speaker A
it yes object you can just create only within the test method but the variable you can create cre at the class level suppose if you want to use this softare set variable in multiple test methods then you should able to create this at the method level S Class level variable and this you can initiate
99:36
Speaker A
instant you can create in one of the test method and you can use in all the test method you can use it so that is a process yeah the variable you can just create but you cannot say soft asset equal to
99:48
Speaker A
new soft asset that you cannot do that yeah this is B basic Java concept yes all right so this is all about soft assertions and hard asss let me write some piece of notes here so that you can
100:06
Speaker A
understand so hard asss we can access from asset class and all methods are static so if hard assession failed then rest of the statements will not be executed and hard soft asss we can access through soft assess object like this we need to create an object
100:29
Speaker A
and then we can access soft ass sessions and if soft assession got failed then rest of the statements still execute so these are the differences between hard sessions and softw sessions so just try one round of execution and see one round of practice you can do and get
100:47
Speaker A
familiar with these options okay so next class we will discuss some more features from test all right so I'll stop here for today's session tomorrow we'll continue
Topics:SeleniumJavaTestNGTestNG annotationsSelenium automationBeforeMethodAfterMethodTestNG test casesTestNG prioritiesSelenium testing

Frequently Asked Questions

What is the purpose of the @Test annotation in TestNG?

The @Test annotation marks a method as a test case in TestNG. Only methods annotated with @Test are considered test methods where validations and assertions are performed.

How do @BeforeMethod and @AfterMethod annotations help in test automation?

@BeforeMethod and @AfterMethod annotations are used to define methods that run before and after each test method, respectively. They are useful for setting up prerequisites like login and cleaning up like logout, reducing code repetition.

Can we control the order of test execution in TestNG?

Yes, TestNG allows setting priorities on @Test annotated methods. Tests with lower priority values execute before those with higher values, enabling control over the test execution sequence.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →