Extent Reports with Screenshots & Parallel Execution - Part 2 | Rahul Shetty

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back so in the last video we have seen how to create extend reports for one single standalone test right so in this video we will try to generate same reports for multiple test cases okay so our goal is to create multiple tests and create one consolidated report for all the test case results right and then I will show how to generate report also for parallel test executions so before that we need to do some optimization and then we will implement running the report for multiple test cases okay so in the last lecture we have seen just a standalone everything written in one test instead ideally in real times you should not declare all this reporting configuration in your test case these configuration related stuff should come from separate utilities file so let's create that first we will try to optimize our existing test and then replicate with multiple test cases you have already one package defined right rafalgt Academy so under this package let's create one class and call it as extend reporter so once you create this file your next goal is to take all the data whatever you declared in your test these lines now should come from your reporter in G file because this is now utility which we gonna utilize for all test cases okay we can just reuse this code instead of being read writing in every test simple logic so let me create one new method here inside this method you can copy and paste that code and let's have it in global level okay perfect so now we have successfully removed this code from here you can just now delete it it's no more required from here because we trying to handle it from separate file at the end you can simply say a written extent so you can return this object so you have an error here because you said void which means it will not return anything but here you are returning extent reports object so move your cursor on this and change method return type to extend report okay so now whoever calls this method they will get access to this extent object so ideally in our test case we want this extent object right so that object instead of doing it from before test we were declaring it some other file so automatically at runtime we can call this method so that this method will return as extant object and that extent object we can happily directly use in our test instead of declaring it here so that is the one optimization you can remove it from here you need not declare anything in class level right so right now you have error because this extent should be called from extent reporter NZ which we have not done yet okay and you know what we will not write this step as well in our test cases okay so our test cases will not have any information about extent everything should be taken care in other utilities so that's the beauty of framework development so in the last stand-alone we did everything right so soon after I introduced test engineer asinus I will remove all this from here as well your code will not have any traces related to extent hold on we will soon be there right now so we have done that another optimization what we need to do is changing this driver file so right now you have one test so you have everything declared here but when you start running your framework with multiple test cases so technically you need not create driver system dot set property every time in your test so let us remove these two lines from here instead I would create one more class called we can say a base class where it will have all the information about driver utilities this would be our method name and inside this I will write the code and this method will finally return driver okay so move your cursor and change method return type to webdriver and this way we have a segregated initialization of driver method as well now if you go back to your test you will get error here as well because you don't have driver declaration here right so you can extend base based on Java have the information about your driver right so you can inherit it as a parent class with extra space when you write this so that means you are inheriting all the methods and its variables from its parent class to child the class this is an inheritance concept so by using this you will have an access to that initialization driver method here okay so this is a method right now you can call that method directly in your test aspect that's it so initialization driver and you can save a driver driver perfect so at runtime so when you have control reaches here this will go into this method declare and initialize new chrome driver and returns the driver and the driver you can directly use it here so that way now if I start creating multiple test cases I need not again go through those all lines again awesome so it's better to have driver in global level let's not stick to the test method okay because this may be used again in another method if required in the same class file right awesome so what did we do we'll let us remove this we will not use this I would comment out for now this should also come from separate file great so we have done a minimal optimization where we have removed all extent report related braces into a separate class and also we have removed driver invocation object okay now I would pause this video and I will quickly three more test cases under this test folder so right now we have only one simple test which hit that rafalgt Academy website similarly I will create three more simple test cases and then I will resume the video so that we will save some time all right so I have made an update so I have created three test cases very simple test cases this test will actually hit this website get the title and close the browser that's number one test two we'll go to google.com and again grab the title and it will get closed in test number three it will go to Google in the Edit box in Google search it will enter hello or any random text and then find out the title and close three simple test cases okay I am NOT going in-depth on creating complicated selenium tests because our overall goal in this video series is to make out extent reports but not on selenium right so that's why I created some basic test cases no matter how complex you create concept is still the same okay our goal of generating report will be still be the same it could be three lines of code or ten lines of code so instead of wasting time I have created a simple test cases in the offline and I hope these are self explanatory and you can easily identify what these test cases are okay now and whatever we did in the last lecture I kept that file in the same way so that you will have some reference if you want to go through the entire code in one single file all right if you want to run all these three test cases sequential manner you can create one test in G XML file and you can trigger it okay so let me create one test in the XML file so that all these three test cases can be triggered from that XML file so there is a reason to teach all this okay before I talk about extend reports introduction so because in real time you will not develop a standalone like this so two for me to show in a framework standard I have to make some modifications like this so that way I can really show where exactly extend report fits in the framework okay you will soon understand why I am doing all this right so if I want to create a stingy XML file with which it can trigger all these three test cases you can simply a right click test in G and convert it to test in G right so this is the ready-made test in G XML file which is created for you isn't it so what all you can do is just accept click on next and finish so this way you will have an XML file with you which will execute all those test cases in a sequential manner okay so let's say eliminate this one because this is standalone test and assuming that these three are framework test cases let's try to run all these test cases and prepare one consolidated report with external reports now when you check each and every test there are no traces regarding extend reports so we have learnt in the previous video that if you want to report that test case in extend HTML file the first thing what you have to do is create a test ok then only the object will be created and this object is responsible to report this specific test results isn't it so now but if you see here there are no traces so how to do that we know that we have created one utility which returns extent object but that start test and flush everything should not be done in your test cases for that we will use destiny listeners concept ok so I hope you all know about test engine listeners if not please watch out my testing J tutorial section to understand how listeners work but still I will explain that on high level don't worry so for that create new listeners file so you know our main slide Java package right click new listeners it's very simple I will explain you so this is a simple class now this class should implement test 10g listener interface okay so just ng listeners are nothing but it's an interface is provided by stingy for all your test engine test cases so to implement any interface you have to use keyword called implements so this will actually implement the interface methods so the test ng interface which will implement all these listeners is this one ie test listener so this is the interface exposed by testing G you see that when you move your cursor on this it's clearly asking import I test listener or G dot test in G package perfect now there is an error here move your cursor and it will ask to add unimplemented methods so this is the one way where you can add all the unemployment rate methods so these are the methods we're testing G exposes and you can start implementing these methods right so let me explain you how this methods will help you to solve your problem of creating extent reports but before that in the latest versions of testing G you might not get this option so right now I have an error right so when you move your cursor you might not get this option from testing g7 dot zero dot one if you don't see this option of adding unimplemented methods there is another way of doing it right click on your class and go to source and you can say override implement methods okay so you will be landed on this page so here you need to select few methods of this interface now so we need on test success on test start failure and we will also take finish so these four of the methods right now we require to handle extent reports click on ok and that will fix but if you are using testing Z version less than seven seven dot zero then the simple way is to move your cursor on this and add unimplemented methods all right now let me explain the process here so listeners are designed in such a way that now if you go back to your test cases so this is one test right so this we call as a test engine test because we are marking this with test annotation which comes from test in G so this is test in G test so whenever execution is happening when your project encounters test in G test there are some listeners which are listening to your execution okay so if it receives Dustin G test execution before executing you see that there is a method called on test start so that means before starting any test in G test control of your execution will come inside this block so you can define anything here so this will get executed first then after it will go to the test and it will run that okay that's a listener because residents keep on listening if on test success so that means whenever you are testing a test got success then control will again come to this method because this method is saying that whenever you see any test got success come and execute me so that's what listener is saying all these are listening and if test fails then your control will come inside this method and that will execute this method on test to failure so if something goes wrong come and execute me so right now there is nothing inside so nothing is happening okay if test is skipped then this one so like this there are different methods finally on finish now you have around three des cases here right once all these test cases are finished so that means test ng XML execution is finished once that is done this method will start listening and it will say come and execute me because you finished everything right so this is how these are the different methods we have so the reason why I am introducing listener says if you go back to your standalone test before executing your test first thing what we did is create test using extent object so that way extent object will have knowledge about your test case right so we defined that as a first step in our test here also we have to define as a first step but with listener concept we know that first before executing this control will always come to on test start right so let's write that extent step here only so automatically when you run your test your control will first find out this before executing it will go back to listeners execute your extent create object it remembers the extent and come back and complete your rest of execution it will be nothing like the way how we have done here simple but only difference is this we are defining in start test listener nice right so that's why I am bringing the listeners similarly if test is failed now you are deliberately failing it right but why would it fail every time at the last step it it might fail in the here here anywhere right so whenever test fails you are controlled with listeners automatically goes to this line on test failure after test failed it will immediately come into this block so here you write the step test dot fail so your extent report will now know that test is failed because you are deliberately writing that so your report will get updated as failure okay so that's how we need to integrate so in that way you need not define anything in your test case everything we can wrap it up in listeners okay I hope now you understood why I am teaching all this optimization and listener concepts so now let's use this standalone code extent report demo because we have already defined code here in our last lecture now we need to simply reuse this code and place it in listeners that's it so this is the step which will help you to create a test object for your test case isn't it so when you give create test then only one entry is created for your test in extent report file so this will ideally go on test start so that no matter how many tests in G test cases you run before every test execution as the control we come to this block it will create one object for that test case which run subsequently and it will create entry into that html5 okay so this way you can avoid writing this in all test cases now there is an error which is obvious because you don't have any information of extent so this information is coming from this file extent report L in J file because we created one utility and declared all that code there right so now we use we should call this method so that we will get access to this extent object right so I do one thing I will mark this method as static so that I can directly access this method without creating object for this class okay so if you declare any method as static then you can call that method directly with class name dot method name by skipping creating object if you static okay so what is my class name extent report ng right extend report ng dot and then you can directly call this method so this will return as extent object isn't it so when you call this method this method is the returning extent object and written type of this extent object is extend to reports this extent reports should come here perfect so this is how you declare so there is an error here because you need to import your extent reports so that way this listeners will know that this object is coming from external reports say we are fine and there isn't one more error in this file that's unexpected when you declare any method as static then the objects what you use in that method should also be static so what is the object you used here extend okay so static method cannot take non-static objects that's the Java rule so simply write static here as well and your error will go away nice so you call this method in your listener and now you have access to extent object and you see that error is gone now as you have access to extent objects you have written it here now there is one problem here so earlier in our standalone test to be hard-coded saying that our test case name is initial demo right but now we have generically defined in listeners if there are 10 test cases every test case will come here first to create object before it executes the test right but what test case name you should pass here you should pass that specific test case name right you need you know more can hardcore this so how to get the test case name now let's say test 3 started execution as we have tied up with listener that will first come and execute this but how do I tell that this is now test 3 dynamically so for that also there is a way to do that so this is a object for a test result class so for this class result dot and you have different methods here get method and in that method get method name this one so this will dynamically get the test case name at runtime now when test two or three let's assume test two when test two is started execution so this will detect listener so before going inside this it will first come to this method right so here it's creating an object by creating we were asking get the method in that method we were asking to get method name so what is the method name here this one Google title validation so this name will come and fall into this block okay so if you represent your testicles name with method name so I have created three test cases this is edit box validation okay this will simply enter a name hello in edit box in Google search right and there is a another test which I gave Google title validation this will simply print the title and this is like initial demo which we saw in a first lecture right so all the three names we were clearly telling get the method name which you are executed so that will come here and there is nothing but our test name okay so this is where you can dynamically get it awesome so we did a great job here and we created test object now at the end if tests fail so if you go back to your standalone test you see that if it tests fail we are returning something so let's do that same way on test failure right again there is a problem tests have is not able to detect because here is where you declare this test object but this object is local to this method right so that's why I declare it in globally so that it will have access to all the methods present in that test so you can remove it here good and here also you cannot give a string like this right hard-coded you should get a real error so how to get that real error so it's very simple so there is a result right so this result will actually hold all the output of your test result dot and you have get throwable this is the method which will extract if there is any error present for the test case okay so this method will execute only if there is any error right now for example let's say test three will fail let's fail it okay so I will deliberately fail this so that you will know how it will report yeah so I am actually having an assertion true but inside I am declaring as a fail sorry false so this will only check for true condition but I am possibly writing it false so obviously this step will fail okay so whenever it detects any failure immediately it will come to the listeners of an test failure so here when you say result not get throw bill it will give the error message the reason why that got failed and this error message we are sending into this method so that it will get reported in your file you can see this error message me a fight okay I'll show you how that works awesome and we also know that at the end we need to flush everything okay so once you have completed your test in listeners there is a method called on finish so this will execute after all you're a test in G test cases are executed at the end will this method will execute here we were simply flushing so I hope now you realized why did I teach this standalone code first okay if I teach everything together you might be confused by this time so that's why I took my sweet amount of time in my previous video to explain this configuration code and now we are simply happily coffee-tasting here and learning new optimization framework topics right so that's my plan of handling this part great so we have flushed everything if it fails we are reporting failure if it's success okay so whenever you run any tests if there are no failures by default it will come to this listener on test success so here you can mark something like test dot you have a method called log in log you can say log status status dot pass and you can pass any strings successful these are like so many methods available for you in extant reports it does nothing when you come to this block it simply mark it has a pass in your report and it will simply show a text called successful and one change what you have to do whenever you introduce listeners and if you want to run this complete framework throw tests in the XML file because here you define three test cases right now when you run this file automatically first test one will get executed and next test two and next x-3 everything in sequence will get executed but this XML will not have knowledge of listeners until you deliberately tell ok so let's tell that there is a listeners which every test of this XML should follow that okay then only it will know and go back to that listeners okay if you don't declare this your test ng file will not have any knowledge about where exactly L is not finest so it blindly executes this okay so that's why listener so you can write class - name let me write it here yeah basically you are giving the path where that Lisner file is present so in my framework it's in Rahul Shetty Academy doubt extent reports this is the package name under this package there is listeners file right so I am simply saying class name which you have to look for listeners is this one this is the path so that your XML file now know where exactly illness is present so that it will go there and this way it will detect so that all your test cases now will point out - here perfect so let's run this XML file and now that will trigger all the three test cases and finally this should now prepare one consolidated report which have results of all these three files so ideally one should fail which is edit box validation test because we are forcibly failing it so extend report should also report that okay let's see how that goes so first test execution is done Bravo GT Academy and now I guess it's pointing to google.com and this is the second test great and we are on final test now which will actually enter something into edit box and then it will fail because we are asserting to failure perfect awesome total test three failure one and now we go and refresh your project and here is where reports file is present copy this path fingers crossed and see how that is reported now interval test great you see that it reported three test cases initial demo successful why this is reporting successful because we have written a log here say if it goes well marquita success and Google title validation pass edit box validation failed why you see that everything is detailed I present here all the failure log is present because it is expecting true but found false how come that log is reported here the magic has happened because in listeners you said resulted not get throwable I told you right whatever the error you got that error can be accessed using this get throwable method and we are sending in to fail method so that will get tracked here so this way if something goes wrong from report itself you can read The Lofts and understand which line fail what is the reason everything you can figure it out okay and if you go to dashboard you will see that total three tests to our past one is failed and test ran by dropper jetty awesome right so this is how you can prepare consolidated reports without writing one single line of code also in your test cases related to extend reports great so now if you check back of our agenda we have seen for multiple test execution but not for parallel tests so this execution will go to us if you run them in parallel any idea why that will go for us so just add parallel equals to test you can do it from here parallel equals two classes right so there are totally three classes here right each class have one test but when you declare here parallel equals two classes that means you are telling to this XML file to run all these classes parallely okay so now when you run this test three classes that means three test cases will start execute at a time if that is the case your reporting we might go for a toss what is they show how to fix it how to make all your test cases thread safe and how to introduce failure screenshots all this we will check in our next video Thanks
Info
Channel: Rahul Shetty Academy
Views: 9,004
Rating: undefined out of 5
Keywords: extentreports, selenium, automation, framework, testng, testnglisteners
Id: AwCcs1XqwfI
Channel Id: undefined
Length: 30min 49sec (1849 seconds)
Published: Fri Apr 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.