Customise output with TestNG IReporter listener

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back so in our selenium with java series today we are going to see the test ng listener so in our previous session we have seen the test change listener that is i test listener and we have seen what are the different interface methods we have available and we kind of implemented those methods to kind of what you call to have set in task before the test case execution if the test case is executed then if it is passed failed or it is skipped then what is that the next step it is to do so all those customizations we can do by using the test listener now today we are going to see another listener that is ireporter now it is again another good reporter that the test ng is providing where you can really get a lot more information like in the sense you can count how many test cases got passed how many test cases got failed what is the start date and the end time so that you can subtract from the end end time to the start time and you can get okay this is the exact time it took and now what is the need of these details okay so the details see always the html report is not only the one which gives us the information there are times where we want to check our console or let's say that we want to send the couple of details or the summary of our result like how many test cases got passed failed how much time it took uh all those details we have to uh save it into a log file so that we can refer it later so all those informations we can extract it by using this i reporter listener now let's go to the documentation which says exactly same thing so which is saying that it has only one method that we need to implement and that is the generator report now this generate report provides us couple of what you call uh the parameters one is the shoot or the list of suits suit is nothing but in the test nj xml we are providing this suit right because we always have one shoot that's why we are giving only the beginning and end of the shoot but in reality we can have multiple shoots inside the testnj xml so that is what it is telling so whether you execute one or two suits those information will be captured here now the next thing is that the output directory it means that this particular whenever you are calling this generate report and then implement something it creates another html remote for you other than the index.html or emailablereport.html which we have seen in our previous session okay so that also the new html report also i will be showing to you guys now let's see what this is saying this method will be invoked by test ng when all the suits have been run it means that after all of the execution this generator report will be executing okay so for this what i have done i've created one new package here into my code editor and i have taken a test ng test it's a sample test let me just fail it here so i can just say that this test case is failing and this is i'm skipping intentionally and this is what it will be executing uh or we are making it a pass so for this let me just do one thing let me add couple of more test cases here okay so my test cases are ready so here i have nearly three or four five test cases are here now the second thing is that i will be creating a new class and i will say that reporter manager okay reporter manager now here what i'm going to do i will be implementing that particular reporter class which is nothing but ireporter so it's basically not a class but an interface that's why it starts from i okay now here if i implement this what i need to do i need to implement all the methods of this ireporter right so let's right click this and let's say generate and here implement methods and you can see there is only one method that is giving me to implement it and let me just remove this one and as i was showing you there in the test ng documentation i have this shoots and then the output directory which i will be taking these objects and i will be playing with the data to populate the output okay so now here let's see what i can do with this suits so this shoots is a collection right it's a list what i can do with the list i can loop it right so what i can do and what is the type of this it's a i suit right so i should of shoot of what the suits which i got it from the parameter now inside this i need to write something like let's say that i want to print the individual shooting so what i will do here i'll say shoot name and i will be printing that it shows so when i say should dot it will give me get name so this will print my shoot name that's the first thing i'm doing now after this what exactly this should gives me some result right so let me do the use this shoot object and let's see what and all methods available so if i say get results so this will give me the results now let's see where i should assign this should get results see this one is a list of i should so that's why i've taken this object as i should right for this get result where should i assign the best thing you should see is that what is the return type of this particular method so you can see it is a map map which is holding a string and i suit result so that is what i am going to do here so i will be creating a map with a string that is my first or the key and then this is my i shoot result you can see i suit result and i have to give some name here right so let me just give as results equal to this one that's it so that is how you will be looking into what this method is returning and you can create that type and let's map and now after this what i will be doing with this result and this is a map right so again like how i have looped down to this list i will be looping this uh what you call map as well how do i uh kind of what you call loop a map actually so see the thing is that this is my string and this is the i shoot result right so what i'm going to do i need to get the individual i suit result that's how i can get the pass fail and all those things so let me copy this one and i will say i shoot result and then here some name i'm giving and here what is that so the results sorry this results will give me the values right so that is what i'm taking here now in this what i'm going to do so i need to now play with see it's all about you are breaking down the collection to individual level and then you are getting the or extracting the information out of that that's what i am doing here now this sr what this sr will give me this suit result so this will give me some test actually now here if i say sr dot get i will get some test context here now what is this test context again see that what is the return type of this so the written type is i test context so now for this what i'm going to do i test context and then i will be giving some name here let's say test context okay now here once i got it then what i'm going to do so this dc i need to play with that now let's see what are the different methods available for this one is i can get the test name so this t so this test context is nothing but your test actually so if i break down into an xml see this is my one shoot actually you can have multiple shoots in an xml at this moment i have only one shoot so the for loop will be running only once now this suit has what actually all this test is there right so these are nothing but the test context or test element we call that is what i have extracted now here now the second thing that i am going to do is that i need to get some information about this test like how many classes are there what is the name of this all these information i can extract it let's do that so for this let me do some formatting so that we can see the same thing in my output let's say get name so this is nothing but my test name okay so this test is nothing but the test tag or the test element name from an xml file so for this here i will be saying test dot get name here okay so test dot get name now what is the second thing that i need i will just give some space here and then let's add some more information into this tc dot i will say get start date actually so you will see end date here right the same thing there has to be a start date right so i'm capturing this start date here okay the same thing i will be taking tc dot get end date here okay now what else we can get it uh one more thing is that i can get the output as well so where exactly it is storing the information so you can see get and get output directory let's see what these things really printing for us and at this moment let me just do some customization here so that we can see the output very clearly okay so these are some information i'm getting it fine now so this is just giving you the test details right what about the individual test case uh details how do i fetch it that's our main intention right to get it how many test cases fail fast and all those things so let's see what else it is giving here now if you see if you call this get you will see something called as get failed test here now let's see what this get sorry this is get failed test actually so this get failed test is a method now this i will be getting all the methods out of that okay now let's see what this get all methods is giving is a return type so you can see it's a collection of a test method actually so can i do like this can i write collection as a type and then i will see what is that collection generic that is nothing but i test ng method right so i test ng method here you can see this is what it is returning right so i will be taking some variable here and i will say failed methods and then equal to i will be taking this one that's it now what i can do i will take this failed methods and i will play with again this failed method object what i can do with this let me print here now i will say total fail method so i can count it because it is a collection right so it's easy for me to count so total failed methods are i can say like this and then i will say plus and come back to the new line and here i will say failed methods dot what i can say size okay as simple as that so i can get the size now the second thing is that you might say that okay you have got only the size only what about can we print this one yes you can print because this failed test method failed methods will give you so many different things like for an instance let's say failed methods okay now this failed methods is a collection right so what we need to do first thing is that we need to have a for loop here now this collection will have what actually collection of i test ng method right so i will be taking this one here so and here test method i'm just writing something like this and then here that collection the actual collection now here what i'm going to do i will print all the methods actually those are present so i will say test method and then i will say like this okay and here i will say so here i can do like this total fail methods count is this one now here i will say another thing and i will be listing here all these things now in this case i can directly call this tm dot i can play now you can see so many different things i can get the description of that okay and the next thing is that i can get the test method name as well so let me print it that first here so if i say tm dot if i put the same thing and then let's see what else is there in this so tm dot get method name will be there you can see get method name so i can print these two things okay i can print all the details so this will give you the total count and then it will give you what are the failed methods those are available for us okay now let's see one more thing actually now we have got only the failed test right can we get the same thing any other stuff like here how i got get failed methods right the same thing let's give all other stuff there so let's say that you want to get all the past test cases okay so i will say past so this is my the actual object right the tc that is the test context so i will be using the same tc dot instead of get failed methods do we have anything called as get fast methods you can see get password test is there now if i put dot now this will give me get all results like what i was getting here right get all get all methods i can get it even i can get all results as well and now here what i'm going to do directly i don't want to print what and all it is really finish past right i just want to know that how many test cases got passed because failed only where we want to get the actual test count or actual test case names actually now so this is skipped right so i will say skipped test count but instead of this one let's see if i have a skipped yes i have a skipped test now you can put all this now see these all things are looping down in this for loop only this for loop which is the i i shoot that individual shoot actually which we are getting it okay this is my first suit so this inside that the first suit only i am kind of looping everything now this is exactly the second for loop which is the actual for loop which is consisting of if you have multiple shoots then this for loop will be looping around at this moment i will be just executing one testing xml with one shoot so you will be seeing all these details but yeah these things will not change whether you have one shoot or two suits because we are printing this shirt right so you can come to know first look one name will be coming second loop second shoot name will be coming and goes on now here let me print the output directory because i told you right there is a html report also generating but where exactly that html report is there can we see that report yes you can see that so for this what we can do we can directly just print this one actually so for this let me just take and output html report path i will be printing this one now where exactly this is there now this is something actually this one this one directly i can use it because it gives me a straightforward string here so i'm just keeping it here fine so that is how now here i have taken this one as a system.out.println right but you can take it this one and you can put it into a properties file or into a log if you are using log4j or slf4j you can use those things and you can say log.info log.error whatever you want you can do that or any other let's say that you want to print it to do an json file or an xml even though you can do that it's just you are extracting the data now how you are going to play with the data that's up to you okay so at this moment i'm just printing it into the console so that we can see that okay now we have created this reporter manager right all these details how do i link this one to my test cases it's the same concept how we have used our test change listener there are two ways i already informed right one is you can set it into your test case testing g dot xml level or else because if you do this you can specify multiple test cases or multiple java classes so that all of your test cases will take the listener there is another way if you are just executing one java class file which is consisting of multiple test cases you can do it at this test case level as well so how do you do that so listeners and then you just need to give the file path name so you can say i reporter it's reporter manager right so reporter manager dot class as simple as that let me just show you with because i have only one test method sorry one test class so that's why i'm using this one how do you run this one now you can just right click and don't run this just this one you have to run the entire class right i'll show you two ways one is from the test level and another one from the xml level now here you can see it is giving you a couple of things here what is it and everything let's see what is exactly the i reporters output that is nothing but this one see here this shoot name is default shoot because we didn't run it from an testng.xml file here here shoot name we are giving here right but we don't really give any shoot name into this test kit that's why it is taking it as a default shoot and now here you can see all these details the test name again it is taking the project name so this is new tab.java whatever it is my project name and then the test start time see this is what i wanted to show you guys so you can take this one as a string and then the end time as well so this is my end time so if you subtract from end time to the start time you will get the exact test case runtime for all these test cases how much time it took really and then the test report output directory so what is it it's a test output folder now you can see you have a test output folder there is a default shoot and you can see the project name whatever the project name dot html if you right click open in browser and chrome you will see the exact name or the html report you can see so the thing is that it's my old report let me show you the exact report here so i'll go to the configuration and go to the listener and i will do the use default report so that it will generate the report from this particular file i mean the java file which you're running that's the limitation of the test ng if you're running it manually here from the what you call code editor itself so let me again run it so that i can see the updated report okay now let's again see the reporter let me rephrase this and you can see which is my current date it is showing how many test cases got passed failed and you can see the individual exceptions as well if it is gift what is the exception if it is failed what is the exception and everything okay and the detailed whatever you actually added right this information you will not really get it into the into your html file it will be into the console or wherever you are printing that like as i said if you are sending it to a log file you can see those details into a log file and you can see it's kind of giving a summary of your test cases how many test cases failed what are the name of those test cases how many passed how many skipped what is the html report path as well so all these information you can get it now this is for a quicker way if you want to send to your high level manager or your business analyst or your product owner you can give all these details because they really don't care about what is the exception why exactly all these test cases failed and everything that you can analyze and later give a detailed information to those persons later now they quickly i let me show you how we can get the report if i want to run it from an xml file so i will be copying this one and i will paste it again and let's rename it as reported listener so here i will say reporter listener click on ok add to the github and here instead of this actually here what i'm going to do report dot reporter manager right the report manager reporter manager okay and here what i'm going to do if this is i have sorry report dot this is test right so test change test i have only one class file but that is holding five different test cases right so let me just keep like this and you can keep this observation here so i will say i will just change a bit so that you can see now here regression underscore i reported so you can see these details in my output let's run this one before to this let me just disable this one so that this listener will be coming into the picture okay now let's run this and you can see you will probably will get the same report only and all these details but before we were getting default report right now you can see it's the suit one hyphen or underscore i reporter is coming and here this is my test tag which i updated into the xml and all other things you can get it properly here you can see the test output folder before it was default coming right but now under your test output folder if you go to this it was coming under default shoot but at this moment it won't be coming there but you will be getting another actually shoot one underscore i reported so that is what actually we need to see oh so the reason is that why it is not coming because the same concept i have to go here i did the configuration choose this one go to the listener and then use the default report okay click on apply and okay and again run this test if you are using from what you call maven command or in jenkins or everything you won't be getting from the test hyphen output rather you will be getting into the target and surefire report actually here you will be getting all those details so that you don't need to do all this edit configuration we have already discussed about these things before right now see after i have edited the configuration here you can see the report and if you click on this and open in browser chrome you will see the same report only there is not much difference the only thing is that you will get the name as a the name regression one uh underscore i reporter that is actually coming from your testing xml file that's the only difference here okay so hope this session is helpful and you understood how we can use the ireporter interface of your testing sheet okay and how you can play with and you can explore more i have just shown the overall actually what and all the scale of features that you can utilize and get the benefit out of this reporter but yeah if you go inside these objects and click on dot you will get the return type and if it is a collection you have to loop it if it is not a collection then you can get all the properties like your description your test case name how many test cases got passed failed and you can play with those data okay so that's pretty much it for today uh do practice and let me know if you get any issues finding the reporter html or getting the information out of this fine so stay tuned and do subscribe to this youtube channel so in our upcoming session we will be seeing how we can really update the html report at this moment we are all looking into the default reports right so can we send some logs from our test cases into this html report to give more information about that that is what we are going to see okay so stay tuned thank you for watching guys
Info
Channel: qavbox
Views: 194
Rating: undefined out of 5
Keywords:
Id: -_uj47FJ7to
Channel Id: undefined
Length: 26min 15sec (1575 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.