Extent Reports 4 + TestNG Listeners in Selenium WebDriver Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
subscribe and click on the bell icon to be the first to see the latest video please find the full course information with YouTube special discount in the description below hello all welcome back in this one we will learn how to generate extant reports with test ng test listeners using selenium webdriver and Java before watching this video and suggest you to watch these two videos to build a better understanding of basic concepts related to extent reports you can simply go to a browser and type YouTube let's code it will take you to our official channel of let's coded and here just search for extend reports from scratch the title see is what is extend reports and how to generate external reports and the second one is how to attach screenshots to extend reports version 4 so both of these videos are for version 4 you can see it on the title as well please watch these videos these will build understanding of concepts related to extend reports then generating extend reports using test engine listeners will be easy to understand now let's proceed with the test Angeles noise concept let's bring up a clips in our previous video we created this demo test class where we created the extend reports inside the before class and we went through all the test cases right now we need to create the test listeners I have this base package created I'm going to right click on this one new class and the first thing I'm going to create is extent manager I'm going to show you beautiful ways to organize your code I'm going to show you how we can create small reusable components so that we don't have to repeat the code again and again so this class is gonna be extend manager click finish and here let's stay clear private static extent reports and extend however the mouse over this extent reports and it's gonna give us the option to import the extend reports from comm dot events tag dot extend reports then we're gonna create a new method private static extend reports this extend reports is the return type of the method and the method name is create instance now what are we gonna put inside this method we will go to our demo test class and whatever code we had here we'll go inside the extent manager copy this and paste it here we need to copy a few more things from demo test class we will scroll down completely and copy this method get screenshot name I'm gonna put this method inside extend manager let's minimize this and change the name of this method instead of saying get screenshot name I'm gonna rename it as get report name just say continue on this warning and this method does not need a method named as the argument so I'm gonna remove this we will replace this with automation report now how this is gonna help us this is gonna help us to generate a random and dynamic name every time be generate the X on the board now let's minimize this and again maximize the create instance method now we'll create the file name here string file name equals to get report name and this is the method we created and we instantly utilized it now we need the reports directly so I'm gonna create a string directory equals to system dot get property and the key is gonna be user dot dir + + / reports and one more / after the reports semicolon so this is our directory after this we will say new file directory door mkdir s now we will say string path equals to directory + filename and the next line is gonna be extend HTML reporter it's the same way we did here we actually created the reporter here on the top so let's copy it from here equals to new extend HTML reporter and for the path we will use the fat that we created right here right here for the file it says file cannot be resolved to our type and it's not even giving us the import statement options I think the issue here is with the directory name because the directory name was incorrect it was thinking that this is the complete one word which is not the correct case here once I've fixed the directory name here let's over the mouse again and now it gives us the options I'm gonna import file from Java dot IO once everything is done we have to return the extend and now this error should go away so everything is all good all fine and this method should not be private this should be public because we will be calling it outside so not sure what I was thinking when I created this private alright so we have our extend manager created guys please make a note of everything you can note down the import statements and the core as well now once we have the extend manager inside the base class itself we will create another test listener class so new class and we'll call it test listeners click finish and this test listener should implement I test listener interface from test ng now this ID s listener is giving us the error we have to import the ID s listener from our god test ng now this class name is giving us the error why is it so let's read it out it says the type test listeners must implement the inherited abstract method and it's giving us the name of the method so what's happening here since we are implementing an eye test listener this class should implement all the methods which are there in I test listener interface so what we need to do here is we can simply click on this add unimplemented methods and it's going to implement all the methods for us and this is happy now so all of this is just a blueprint obviously there are no implementation as in logic wise we will have to do it but the methods exist now now under the test listener what we need to do here is we need to say private static extend reports extend equals to extent manager dot create instance this is the method we will be calling from extend manager and to make it happy again import the correct statement so instead of writing the code for generating the extend reports we are making you of extent manager right if you see in the demo does class we wrote the code here the complete code if we had like 20 test classes are we gonna repeat this code 20 times the answer is no we don't want to do that that's why we created this extend manager and now in test listener we will be creating the instance of extent reports now the next thing we're gonna say private static thread local extent test and let's give it a name as extant test equals to new thread local extent test let's import the extent test also and all good we are writing thread local because we're gonna make this class as thread safe so that if multiple classes are running in parallel and I'm talking about multiple test classes and they are trying to generate a report in parallel then this class should be able to handle it otherwise if your test listener is not thread safe then you're gonna have issues when you run your automation in parallel now on tests on this method of testing the listener gets triggered right before the test method starts so what we're gonna do here is inside this we will create extend test and this is a local instance of extend test equals to extent dot create test and right here we will say result dot get test class dot get name so if you see the demo test class let me scroll down we created the test like this we just gave it some static name and extended create test now in test listener we are doing extended create test and now in the test listener class we are creating the test as extend dot create test and inside the parentheses making the use of a test result we are getting the class name so this guy is gonna give us the class name and then after this what we'll do is plus and some formatting and then plus result dot get method dot get method name now let's move it to the next line right here so that it is better for Merryn this is brilliant right what we are doing is in this place itself we will create the extent test so that we don't have to do it in every test method now we need to do one more thing we need to say extend test and this is the global instance dot set test so we are setting this instance inside the extent test now this is going to be thread safe so whenever something else accesses it it's all gonna be fine now we have another method call on test success what do we need to do here we will go to our demo test class and we will scroll down from the f-block of the test success right here I'm gonna copy everything from here I'm gonna go back to test listeners I'm gonna paste it here and this is all we need instead of method name what are we gonna do here result dot get method not get method name so we have the method name here we have the mark up here and instead of directly saying dot log we need to say dot get and then dot log this is because we are accessing it from the thread-local all good so that's what we need to do for on test success let's copy the whole thing and go to the on tes skip as well so here we go let's copy it here and replace the successful with skip and replace the color with yellow and status with skip all good this on test fail with success percentage we can simply ignore this method it's not needed and now let's come to the on test failure this is the important method right so what do we need to do here let's go to the demo test class and come to the if condition of test failure so I'm going to copy everything starting from this line to the end of this if condition copy from here and then I'm going to paste it inside on test failure method so everywhere instead of directly using them we have to say extend test dot get let's fix this first and all good we don't have the method name declared so I'm going to right here string method name equals to Brazil dot get method dot get method names I'm gonna copy it right here and all done so this is also happy the take screenshot is not happy we'll fix that as well now here also we need to say get an all good so let's go back to demo test class and copy this complete take screenshot method as well along with get screenshot name method so I'm gonna go to test listeners class scroll down to the end and copy both the methods here now this method complains about the driver previously did not complain about the driver because we implemented this method inside the test class directly and the test class already has a driver instance so now what we need to do here is we need to pass the webdriver instance to this method and let's import the webdriver statement so this method is happy now now we need to scroll up and right here we need to provide the driver instance and now it's complaining about the driver so where do we have the driver we don't have the driver instance in test listener we need to find a way to get the driver instance what are we gonna do here we're gonna say webdriver driver equals 2 and here we're gonna give the name of the test class so I'm gonna say test class using listeners result dot get instance we need to add one more parenthesis here and here and then dot driver this is gonna give us errors right now because we haven't implemented this class so this is gonna be the test class no need to worry about it as soon as we implement the test class this error will go away now let's go to on finish so in the on finish what do we need to do we need to check if extent is not null then we need to flush the extent instance all good so this is also done this completes our test listener let's scroll up this is the class name that we have provided here so we need to create a test class with this name I'm going to copy the I'm gonna go to test classes package right click new and class let's provide this name here and click finish now that we have created the class let's go to demo disk class and I'm gonna copy a lot of stuff from demo disk class and it's just to save some time we will delete this stuff from this just avoiding some typing so we don't need these methods just completely delete them and then let's crawl up we don't need extend manager and extend test here let's delete them we don't need these things but we do need the driver instance we don't need to flush the extent we are doing this from the test listener and we don't need to create the test also here what we need to simply do here is maybe just us this out statement and we can simply say executing successful test so that's about it and then I can copy the same statement to other places also and then this is executing failed test method and then here also executing skip test method so this is our before class after class and the test methods from the after method we can really remove everything we don't need this and in fact we don't need the after method also so we can get rid of it all done this is our test class and we can simply remove all of the import statements that are not needed and now let's go here and I'm gonna scroll up I'm gonna say import test class is dot test class using listeners once we import this class and save the file we should not see this error message now we are getting this it is saying change visibility of driver to public which is just happening because in this one we did not define the web driver as public so we can simply call it as public so that this test listener class can access it that's about it we have our test listener implemented and this is a new test class so come on guys take a look at it let's compare it with demo test class demo test class was everything when inside it and this test class using listeners is such a sweet and smart class it's so beautiful and easy to read code it gives me immense pleasure when I take a look at this kind of code such simple code so no repetition of code is here everything is been done from test listeners and now the big question is how do we make use of test listeners I'm gonna open the test engine or XML file and right here inside the suite we have to say listeners and then we need to say listener and then inside the class name we need to provide the class name of test listener so we have to say base dot test listeners that's it so we're done with it we only have to provide the full class name including the package and make sure you don't make any mistakes here and when we add this line of listeners to the test ng door XML file it binds the test classes with the test listener class and here instead of using test classes dot demo test class we have to use test class using listeners please make sure no typos here it's very important type everything correctly now we are all good let me just minimize this and in our reports and screenshots folder there's nothing they are empty let's go to the XML file right click and run this one it's gonna bring up the Chrome browser there's no action that's it's gonna do and we can see the result in the test ng console one test failed one test skip and one test is successful now let's go to the project refresh it and see if we got the report and screenshots here we see the report is generated and the screenshot has been taken but what do we see here we see that the name of the report is still extend dot HTML which does not sound correct right because we created a dynamic name so let's just quickly open extend manager because that's the class where we created that method so what's happening here if we take a look at this one we do have the get report named method and it's been call here in the file name and it's interesting we are debugging life so it's good that we see these kind of errors because you guys also get to learn how you debug and how you approach to fix the issues so then what's happening we are creating the filename also we are creating the path we are providing the path also to the HTML border but here you go what do we see when we actually copied the code from demo desk class we copied this line as well with the new code we first create an instance of HTML reporter and provide the path here and then we again create another instance and then provide the new path so since this is defined later this path is taken into effect and the report is generated with the name extend our HTML so we simply need to remove this line all good let's quickly double check few other things since we created the new method I'm just gonna double check one more time here you guys found one more issue in the report name also we have the extension as dot PNG this is coming from get screenshot name method right we have to replace it with dot HTML now everything should be good I'm gonna right-click the XML file and run it again it's just gonna bring up the browser and close it and we do have the output let's again refresh the project and here we go now we have the automation report with the new name and we have another screenshot taken so let's quickly right click this one and show in system Explorer and here we go we have the report right here let me just quickly open the report and boom we have the similar report one thing here we are doing is we are getting the package name with the class name and then the test method name so we get the complete name here and then we have a fail test method we have a test method which was kept and then we have a successful test method so skip is in yellow color successful is in green color and fail is in red color and we can see that we have the exception occur and click to see details we can see the assertion error when we click on the details we can again click on it and then we have a screenshot of failure if we click this guy the screenshot also becomes bigger so all good here and then it tells us the test method fail and then we have the dashboard as well where we can see the details of how many tests were run where was the start time end time and the total time taken and the environment values so all looks good we have the report generated using test ng listeners let me just quickly do one more thing I'm gonna replace this theme with the dark theme and then we're gonna generate a report and then I'm gonna show you the difference so quickly right click this XML file and run as test ng sweet and boom that automation is done let's refresh the project again and we have the new file generated so there you go now we have two files here this looks like the latest one let me open this one and hey you go this is the dark theme this looks beautiful guys I actually like it a lot this is the failed one skip one and the successful one and here's the dashboard as well so it's up to you guys whichever one you want to use I can open both of them for your comparison so this is the dark one this is the light one whichever automation report you want to use feel free to use them so that's about it for extend reports one very important thing guys please do let me know in comments which topic you want me to cover for the next video I will read each and every comment and I will try to see which is the most popular topic and come up with a video on that please like and share the video and subscribe us for amazing new videos like this thanks a lot for ending this one and I'll see in the next one [Music]
Info
Channel: Let's Kode It
Views: 12,920
Rating: undefined out of 5
Keywords: How to generate Extent Reports with TestNG Listeners in Selenium WebDriver Java, extent report 4 testng listeners, extent report with testng, extent reports selenium java, extent report 4 with test listeners, extent report 4, extent report screenshot on failure, extent report with multiple test cases, extent reports version 4, extent report with screenshots in selenium, extent report 4 selenium webdriver, extent report 4 selenium java, how to generate extent report version 4 testng
Id: Lapn6VLoqdc
Channel Id: undefined
Length: 21min 54sec (1314 seconds)
Published: Wed Mar 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.