Assertions in TestNG - P5 | TestNG | Testing Framework |

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone i am yada yada and welcome to my channel hbr tutorials in this video i am going to talk about assertions in testing g so here i am going to cover these points basically so what is mean by an assertion and are the assertions mandatory in our test cases if they are really mandatory then how to perform those assertions so these are the points i am going to cover in this video guys so you can consider this is like a agenda for today's session so first thing first what is mean by an assertion so here before i explain the actual definition i want to tell you one scenario so that you will understand it better okay before going for the official definition we are going to look at one scenario so that we will understand what exactly the assertion is and why we are actually using the assertions so generally in our testing either it is a manual testing or automation testing or anything so in our testing what exactly we are doing so we will pick one test case and we will perform some operations and at the end of the test case we are going to verify some outcome right so every test is designed to verify some outcome so based on the outcome only you can say that okay the test is actually passed or the test is actually failed right so here every test should have some kind of a outcome right so based on that outcome we will perform the validations then we will say that okay the test is actually passed the test is actually failed right so this is mandatory in almost every test case either it is a manual test or it is a automation test so here in our automation in selenium webdriver we don't have anything to verify like that right so let me tell you one a small example here so here in my previous class i have created this class right fastertested.java so in this what exactly we are doing we are opening the google chrome browser and we are navigating to google.com then we are entering the text hi tutorials and we are clicking on enter so after that i want to verify the title of the page so i want to verify the title of the webpage i'm able to get the title okay i'm able to get the title using selenium web driven but where i'm exactly violating this one see if you if you want to tell that okay the test is passed and the test is failed you need to verify this title with some expected title right so then only your test will be validated and you can see that the test is passed or failed right but here i'm not doing any validation right i'm getting the actual outcome but i'm not validating this with the expected outcome so i am not verifying this one right so every time this test will be passed only okay so even though the title is actually wrong it is not going to verify anything we are not doing any verification so the test is always passed but this is not the way we want to perform the testing right we want to perform the testing by validating something so we need to validate this right so for validation we don't have anything in the selenium webdriver so the selenium webdriver is not providing anything for the validation purpose this driver.get title is actually returning you the title it is an actual title so you need to have some kind of expected title and you need to implement the logic to validate this right so that logic is nothing but the assertion here so the assertion is nothing but it is an expression which will verify the actual test outcome with the expected test outcome so you are getting some actual test outcome and you need to verify this actual test outcome with the expected test outcomes then the validation is actually completed and you can say the test is passed or failed right so based on the outcome of the validation you can tell the status so that is what we are going to see now so now you tell me are the assertions mandatory in our test cases yes obviously so if you don't have assertions in place right you don't know whether the test is actually really passed or not so always the test might be passed always the test might be failed right so that is the reason we need to have the assertions placed in our test so assertions can tell you whether the test is actually passed or the test is actually failed so to get the status of the test we need to definitely place the assertions in our test case so now let's see how we need to place the assertions so for suppose here i want to verify the title after entering the text and clicking on enter i want to verify the title so how do i verify the title so first let me open the google so i'll enter this text so i need to have some kind of expected result also right so if you want to validate you should have expected as well as actual so here this is the expected title so manually when i open this one right so i'm getting this one so i want to verify in the automation also the same one is coming or not so based on my requirement i should get actually this one so this will be my expected title right so let's write that so here i'll say expected title so let me remove this chart so what is the expected title the expected title is not coming from the selenium webdriver because the expected title we are giving as a user we are actually giving the expected title so what exactly the expected title is so let me copy this iphone google search so this is the expected title that i want to get so now what is the actual title here the actual title is coming from the selenium script right sorry actual title is coming from the selenium script so that is this one so here we are not going to print this and we are going to perform the validations that is nothing but the assertion here for performing the assertions the testing g is providing one class file that is asserted so here you can see we are having one class file that is assert so this is actually a static class guys that means i mean all the methods inside this class are basically static methods only so you don't need to create any object to call the methods inside the asset class as this is a static class so here we need to call the methods so what are the methods that we have so here testing is providing so many methods to perform the assertions basically so those are these things asset equals asset not equals as a true asset false asset null asset not null so if you want to compare any two values like it is a string or boolean or character or it is a byte array or string array or anything so if you want to compare any two values then you need to use the asset equals method so in the similar way in our test sometimes we will have the negative validations as well right so for the negative validations we need to use the asset not equals so we want to definitely verify the actual value is not equal to the expected value so in those scenarios we are going to use the asset not equals method so in the similar way if anywhere you are getting the boolean value from the website i mean the actual value as a boolean value so in those scenarios you can use these two methods for the positive validations you can go for the asset rule and for the negative validations you can go for the asset falls so in the similar way if you want to verify any object value like any object you are creating or any object you are reading from the data so in those scenarios if you want to verify whether the object is null or not null using these two methods you can verify the assertion so here i want to verify two string values right so that is nothing but the title of the webpage so this is the method asset equals is the method so inside this the first parameter is going to be always actual value so here what is the actual value the actual value is nothing but the actual title right and the next parameter will be expected title so we need to pass both the values right actual as well as expected so now this one is completed right so i want to run only this test case so what i can do i can simply click on run from here so let me run this so the test is executed and you can see the test is passed because the actual title and expected title both are equal here so that is why the test is passed so now let me just change this expected title here so now let me run this test so this time my test will be failed so here the browser is not getting closed right that means the test is actually failed here you can see total test run 1 pass 0 and failure 1 so here it says fail test google so what exactly it is showing expected is this one but found so and so so this is the thing right but here we are not getting message okay it is saying assertion error and it is directly giving the expected value and what is the actual value right so some people don't understand what exactly you are verifying here right i mean here it says expected is this one and actually this one but what is this expected i mean you don't know whether this is a title or whether this is a username or anything you don't know right so what exactly this value indicates so that kind of message also should be there in the assertion right so if the test is actually failed you should have a custom message so here you can add the custom message so after actual and expected you can add the custom message so i will simply say title is mismatched so now let me run the test again so this time i am expecting the custom message to be coming in the console or report or test engine result file so this time also the test is actually failed because we intentionally failed the test by passing the wrong expected output you can see the test is failed the test google is failed and assertion error and you can see the custom message so what exactly it is saying title is mismatched now you will understand what exactly the expected title is and what is the actual title is you know the i mean this value is indicating to title that you understand now based on this custom message right so previously we don't understand okay we don't know whether this is a title or we don't know whether this is a username or search text or anything so we don't know that right so that is why it is always important to add the custom message also so based on the custom message only the end user is going to identify what exactly the value is failed what kind of assertion is failed so here the title is failed the title assertion is failed right so based on the message only you will understand that right so this is how you need to basically verify so in the similar way you can use any of these methods basically right so i have used asset equals you can use asset not equals asset true asset false so based on the scenario you need to use these values so that is one thing and next thing is so as i said these all are the static methods only right so every time here i'm writing like as a dot as a dot right so i don't want to write like this so what i want to do i want to simply write asset equals directly so that is also possible if you import the asset class in the static way in this class then you no need to mention asset dot every time so that will be easy right i mean you will reduce the code right so let me import that reference here you can see the suggestion is coming add static input for asset equals so by default when you use that i mean suggestion right so it is going to import only this method so this method is going to import but i don't want to import only a single method right i want to import all the methods so i will remove that one asset equals and i put star here so that it will work for all the methods so now i don't need this asset class import so i'll just remove that so now if i want to use asset true so let me try whether i am able to use asset true or not now you see i'm directly able to call the asset true method also so if you use only asset equals here so again you need to import the reference for the asset true so like this if you are using 10 assertions you need to import the 10 types of i mean import statements here right so instead of that we are going to simply import this way so this is the best way to import so all the assertions will be imported by default so this is how we need to basically perform the assertions in test ng guys so as selenium webdriver is not directly providing the assertions so we have to take the help of any library any external library so here we are taking the help of test engine so you can take the help of june as well or you can take the help of any external library third party libraries like asset j or something so as we are working with the test ng i am showing you how to do the assertions inside the test ng so if you are working with the j in it you can take the help of j unit assertions so those also will be almost similar to the test engine only guys so this is how we need to perform the assertions in test ng so i hope you enjoyed this video if you liked the video please hit the like button and also share this video with your friends so if you have any doubts or if you are facing any issues while performing the assertions please let me know in the comment section below thank you for watching bye bye
Info
Channel: H Y R Tutorials
Views: 2,788
Rating: undefined out of 5
Keywords: hyr tutorials, h y r tutorials, selenium webdriver, selenium java, testng, testng tutorial for beginners, testng tutorial, testng annotations, testng selenium, testng framework in selenium, testng framework for selenium, testng framework, testing framework, testng tutorial for selenium webdriver, testng tutorial java, testng framework tutorial, testng annotations in selenium webdriver, TestNG Framework for Selenium, assertions in testng, soft assertions, hard assertions, hyr
Id: MccoImse-qg
Channel Id: undefined
Length: 11min 40sec (700 seconds)
Published: Mon Aug 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.