How to Implement TestNG listeners in Selenium Webdriver

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone welcome to learn Automation calm this is Mukesh today in this video tutorial we are going to cover test ng listeners that is one of the most important topic in testing G or you can send selenium as well so you might have heard of testing G listeners and selenium listeners like webdriver listeners right both are different so we will talk about testing Jesus now first and in the next video we'll discuss about the webdriver lessness so let's discuss about the high-level agenda for today so we'll discuss what is listeners different type of listeners in testan G different ways to implement the same and how to implement in the project so different ways we'll talk about different classes and interfaces that we have in testing G so if you talk about listeners it's very important feature of testing G which will allow you to customize your logs or you can say reports of test engine right so we have some log features in testan G where you write like test start a test finished closed script failed so now using this listeners you can totally customize the logs okay or you can fully customize the reports as well so once you get the basic knowledge of listeners I will show you how you can customize this reports and logs using the listeners and it is one of the best feature of test in G if you use in your recent project and if you see the name itself says listener it will listen to your commands since it's a test in G listener it will listen to the test in G events then based on the events it will perform accordingly so will see this basic example today and once you get the basic knowledge of listeners I will show you how you can implement this listeners in your project and customize the reports and logs now you must be wondering like you we have only one listener but not we have different type of listener in Destin G okay now you need to decide which listeners to choose for a particular task so this list I have taken from their official site that is Destin J dot o-r-g dog documentation so let me show you this link so if you see this test in G official website we have different interfaces which will allow you to modify the test in G behavior and these interfaces are broadly called test engine listeners so these are the list of listeners but do not worry we need as of now only eye test listen every listener having a different you can say responsibility to do the particular task as you can see by the name IRA Porter this will change the reports I suit listeners I test listeners so today in this video will basically deal with eye test listeners like how exactly your test behavior and status pass/fail skip based on that we will customize the methods so if I go back to the slide again because you do not worry about others let's focus on eye tests listener today okay so now again now this is very important in terms of your interview now we can implement this listeners either using a class called test listener adapter or else we can implement an interface called I test listener so if you know a little bit Java in Java we have one topic called interfaces where you have some methods which is unimplemented so in a test listener interface you will find so many methods which are not implemented so we will implement and based on our requirement we'll make the changes so in this video I am focusing on AI test listener in the next video we'll discuss about test listener adapter but as of now I will recommend just focus on eye test listener so now here also we have two ways so either we can implement on the class level or we can implement at suite label so let's discuss on the class level first and once we are done with class level and what are the challenges you might face in the first approach then we'll talk about the second okay so let's move ahead to the cliff's this is where project learn automation I will create a separate package called listeners demo I will create this let me close this let me create a fresh class and I will say test I will say test ng listener so this is a class okay and I had given this name called test in G listeners so let's try the first way today we need to implement an interface called I test listener okay so if you say implements I test listener it will ask you what exactly it is so as we know eclipse feature it will give you suggestions like which listener you want to import so you can see the first option right eye test listener orz daughter stingy so let's select this now I can see it is giving me some issues like it is throwing me an error so let's put mouse over here and it says they're two quick fixes either you can add an implement method or else you can make this class as abstract we need to click on the first one call add and implement methods and you can see it gives me so many method which does not have an embody right so let me remove this unnecessary code which might confuse you so now if you go one by one so just focus on these what you want to perform when your test fails what you want to perform when your tests skip what you want to perform when it has start success and so on so as of now body is blank so you can customize this and good thing about this is every method you can see these four methods have one parameter called eye test result it means whatever your test result comes it will store into this particular object called ERG zero so I will just rename it called result it gives me like proper feeling like it will store the results only right so whatever the result it will store into this object and I will use this object to get the data so let us say I want to write I will say test cases failed and details are plus so whatever the details you want if we simply say result dot you can see so many predefined methods like it will tell you when it ends so it will return you the time in millisecond it will return you the host start time status and all I need as of now the test case name VISTA case has been failed now the question should come into mind why only this method because let's say if you have 15 test cases and after 15 only one test case is getting failed so this particular listener will provide you the detail with this information like this case field and the details are with the test case name so in the same way I will write these four methods only pass/fail escape and success and I will say test case skipped and details are test case started and details are test case success and details are this I am NOT implementing these four you can also implement like what you want to do on finish on a start on test failed bit but with success percentage but ideally we need to implement only these four which is required so this method will only execute when that particular condition match let us say if your test case fail then only this will execute other else or else these will execute so if your this will be the by default every time okay whenever test starts it will give you the information if test case pass it will give you this information is cube failure okay so now our listener is ready now we need to implement this listener into a regular project so I will create a new class and let us say my first test case so now you can start writing your test cases and you will see the test engine listener will automatically check which test case is turning in it will give you the details so let us write the first test case I will say public void Google title very fact I am justing any dummy example you can take your own example and you can start using it if I simply say test I need to add test and G library and I need to add test in G package as well my system is slow today so if you put mouse or import test so let us write I will say webdriver Drive or equal to new Firefox driver I will maximise driver dot get dot google.com and finally I will print the title driver dot get title and I will close the application if you are not sure how to write this code you can refer my previous videos where we discussed how to write your first test case so now you can see this is my test we simply perform when opening Firefox maximize open Google get that it will close it but how does it relate like I need to use this listener there is no connection right because this is separate class this is also separate class so how do we connect these so the first way simply type L listener okay so you can see there is a separate a notation called listener that you have to use and you can use this listener so I will say go to this package that is listener demo dot and use this listener called test ng listener and use the class file it is giving me one error so let's use here okay it's not allowing again okay let's take a let's check to their official site maybe we are making some small mistakes so listeners and that's all so you can see notice giving us you need to keep this listener at the class level so now go back and we were trying here right so let's control X and control V we need to keep this listener at the class level not at the individual test level and if I go back to the previous and test case details not keep only details such a test give details are so now this is our regular test and we have bounded this test with this listener called test engine listeners that we created today that is result in this particular package and we are bounding with the class file so let's run this and check so you can see it automatically started and say test case is started and desk is details are Google title verify even though we have not written here but it is taking everything from the listeners so this is just log4j warning can avoid it so you can see now I do not have to write the logs it will automatically start giving me this default log because we have specified in our listeners so it helps us to save so much time and effort as well you don't have to write this you can see our test completed this was the starting then it has printed the title and test case X is for a test case details are google verify right so you can see now this listener will be applicable throughout the class and whatever test you have inside this class will use this listener so if you take an example if I create one more test and this time I will not open like just give em great there's some changes this time I will not write this code because it will take some time so I will simply say this out test to dummy and forcefully I will fail this test I just want to show you if and it has fails so forcefully I am failing it so I will use one class of testing G called assert which generally we use for assertion and I will give the false condition so assert true will only pass your test case if it gets true now I want to fail this desk is forcefully so what I did is I pass the value as false so if a cell true will get false it will fail your test so let's run this and check how it works so now this listener which we have added it will be applicable for this test case and it will be applicable for this test case so now this is started Google started and it will close and now it will check the second SK so so so you can see it is executing two test cases one is Google title verify that is passed this is failed because we have given now if you see test cases started test case details a Google test verify Google and it has finished this is one test test case second started and it is this title match it has printed this and it is failing also because if failure happens it will execute the failed method and it says task is failing test case details are this then white is failed expected word true but found false so like this you can see this listeners are very helpful for generate logs and provide you some details about the test and I just modified and just for the example purpose you can change based on your company requirement all these now take an example I have one class two test cases but what about ten classes if I have ten classes and I want to implement this listener is this helpful no why because I am binding this listener to the class level so let us say if I am writing ten more classes every time I have to add this listener into each and every class so that is not the recommended way so we have another way that I was actually talking we can implement on suite level and this is the recommended way first one was only to show you how we can implement using class slave on class level and state level so let us go back and you need to remove this sorry you need to remove this line number 11 and simply create an XML file so simply right-click on your Java file go to test Angie and click on convert to test Angie so it will create one XML file let me give tests in g6 dot XML or let's say test in g mukesh dot xml and click on finish so you can see destiny mukesh dot xml created and this is my test case now i have to implement this listener on the suite level so after the sweet tag we need to add when i call listeners tag so you can see after the suit we need to copy this and we need to add here as of now we have only one listener so i will remove one and here it is asking give me the package and give me the listeners name so if you scroll up what is the package name listeners demo so I will simply copy the package name listeners demo and our listener name is destined G listener now you are done so what actually does it will apply this listener throughout or testsuite it does not matter how many classes you have you can have ten classes and fifty test cases this listener will be implemented throughout your suite because you have implemented this on the suite level right so let's run this and you can see there is one error is coming so you can just remove it because we don't want to run parallely so if I run this test in jammu case dot xml run as test in g suite see again getting the same thing this case is started in task is details I googled title verify since Firefox is taking time so execution is little bit slow so in the next videos I will use Chrome because chrome is faster and it doesn't take much time okay now it will do the same thing same execution yeah it will print the title this case success and another test field so like this if you have ten classes this listener will be implemented in all so this is what I have for this so now let us have a quick recap what we have covered we discussed about listeners which allow you to customize logs and report of destined G and it performed particular tasks based on the implementation which you have done so we'll use this for customizing log and reports so the different type of listeners presents in this example we have discussed only I test listeners so you can use eye reporter eyes suit listener as well the two ways either use extending this test listener adapter which we are going to cover next or we can implement using eye test listener interface then we discussed at the to label first one at the class level second one on the suite level so class one we have discussed we have added only at the rate listener and the class label but the issue was it was only applicable for this class later on we added one separate tecala sness we closed her and we added the exact listener that we created and this is our listener we created a class implements I test listener it gave me seven unemployment method we implemented for which is required for our test this is what I have for today so thank you so much for watching this video in case if you like this video please subscribe to my channel share with your friends in case if you had any doubt feedback just comment below and if you want anything from the code side just drop me an email to my email id Mukesh or to any editor at learn - automation calm thank you so much have a nice day bye bye
Info
Channel: Mukesh otwani
Views: 132,448
Rating: 4.9144187 out of 5
Keywords: Selenium, Webdriver, Automation, Java, TestNG
Id: -a5aHd0gsGw
Channel Id: undefined
Length: 24min 21sec (1461 seconds)
Published: Thu Mar 10 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.