capturing screenshots of failed test cases using TestNG Listeners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is SMY welcome to QA automation classes in the last class we have uh like implemented the custom listener uh class which was implementing it test listener and there we have uh uh created two methods right so on test failure and on test success and we ran we created about testng.xml also last time and we ran it right so if you see we are mentioning The Listener here and the classes class name we are giving here right and test name are giving and there are two test methods we're including both of them okay and when you run testng.xml this is the Run results okay so here if you see so one of the test case is passed and another test case is failed open login page is passing sample sample fail test is failing so if you go to the test suit level and see so total test run two passes one and failure one so one test case is passing one test case is failing so we can do one thing we can go to the reports folder and we can open the extend spark report. HTML and let's go and analyze okay so if I open this one okay so I can see there are now two tests right so one is the open login page another one is the sample file test how you are seeing these two test because if you go to the test class TC 01 open login page if you see here there are two at theate test annotation right so one is the open login page another one is the sample failed test so these two test cases are there inside the same test class so if you see in the report also similarly two tests are there here tests right so there are two tests test open login page and Sample fil test so if you see open login page right so we have already gone through this section right so now there is one more new thing that got added over here that is the last statement if you see status is now pass and what is the time it is pass and what is the name of the uh method right so if you go to the test case right uh base stes right not Bas stes let's go to the custom listener there there only we have implemented right so if you see so here we are seeing result name do gate name to uppercase let's put a separator over here pass okay and extent color green so if you see the extent color it is coming in the green so if you see right so up to this is the name and then this is the pass that we have put just now I put a separator so it will come separator in the next one okay so this is the one and uh now go to the fail test so if you go to the fail test Let me refresh okay so let's go to the fail test so let's see here what we have so so here let me run it one one more time I think I didn't refresh it properly okay so here we have two status. fil status. fil we should be ideally seeing both of them okay so let me close this everything let me rerun it so that I can see this separated properly okay let me do that okay let's run it one more time uh let's Okay test.xml I need to run right okay so it was also running the test.xml okay so now uh let's wait for some time let it finish the execution so now the browser is opened automation s.com expanded the window okay so done okay so now if you see here also so it ran two times one pass one failure okay so now let's go refresh it let's see anything is there so let's go to reports okay and in the reports open exent spark report and go to the Chrome open it with the browser so we saw the past scenario right so now if you see there is a semicolon in between right so semicolon and pass similarly let's go to the fail test so in the fa test if you see these things we have already seen this is a sanity test right we are saying this is a sanity test if you go to your test case you can see that has been annotated with the sanity test what is the test case test Java test cases okay so if you see this has been marked with the assign category is the sanity test okay so now there are two category also one is the uh smoke here if you see smoke and here if you see the sanity so here uh if you see click sanity and smoke both will come here you can filter also if you want to see only sity you can see if you want to see only smoke you can see right there is a filter also so if you want to see both of them we can see both of them also right just refresh it will go away so now we have understood this pass concept now go to the fail concept so in the fail if you see what is happening so one info is there let's go to the test case and understand so what we have in the fail test case we have one info status. info starting the second fail test run so let's see so starting the second fail test run is coming info and then two fails are coming right so here if you see I have given uh so yes starting the this is the one and let's go to the custom listener if you go to the custom listener you can see when the test case actually got failed then what we're going to do there are two steps we are going to do here so one is the uh log it as start. fail and show the name right and then show the message actually what exactly why exactly it is getting failed right get throwable do get message that ex one is generated so it will show that message right and then another one is uh put that result. get name get that result to the upper case that and then Mark it as the fail in the red color right so if you go to the uh UI report you can see there are two events here this is one event this is another events okay but for past case we only created only one event right so that's why you see only one info event is there and then one fail and one fail two fails are there right so if you want you can configure multiple events or if you want per uh thing like you can configure only one also right so here there are two if I go to the this Statistics right so here if you come and see right it's saying one event passed two events failed and four others so why two events fa right so because here we have configured two events yes or no so if you go here and see there are two events so if you want to see only one event you can go and comment out it's better to go and see the uh reason why exactly it failed and anyhow like uh showing the name of the method is anyhow it is shown here right sample fail test right so there is no need to duplicate this one so we can do one thing we can get rid of this uh second one right or we can get rid of this first one whatever you wish you can get rid of if you see here here we're showing this one as pass then if we want to this be in sync then we can keep the last one this test name as fail right and we can get rid of this uh assertion right so that also we can do so let's get rid of this one okay anyone you can get rid depending upon your uh like how you agreed like how you want to show your report so you can do that and now if I run now okay so let me run again so now my expectation is I should see only one in the event failed in event so because another one I'm um commenting it out right so let's see after the execution what happens and till now if you see we have not yet integrated our code to capture the screenshot right we are not calling right so on the test fer what we're doing we're just only capturing this event we have not yet calling that capture screenshot that we'll be doing next okay so let's go to the spark report and quickly open this one and see okay so if you see this is also only one event is there right so if you go to the statistics you can see only one event should be there see one event failed right perfect perfect so now uh we saw uh like uh when there is a failure uh how can you see the report how does the report will look right so you can configure those events and those events will directly show in the report extend report extend spark report okay so now Our intention is to capture the screenshot also right so we want to capture the screenshot whenever it fails so we had to write that code capturing the screenshot right so this capture screenshot we have to write in the same custom listener class so let's go to the custom listener class automation here it is right okay so here we have to write that so let's say uh base steps okay so base stepes dot Okay taste. add Okay add screen capture from path this is what we need to use so in the test to the test object we have to add screen capture From the Path so let's select this one and then okay we have to pass the uh base steps okay base steps dot we have the method there capture screenshot right and we have to pass the driver instance so which driver so obviously the driver is from the Bas steps right so Bas steps. driver if you see lot of the places I'm using Bas stes do bases dot right so instead of that we can import this B step okay because these are all static that also should work right and it is showing you the uh warning right what it is unhandled exception it might through the Java i. IO exception input output exception it might throw so you might need to add it with the uh um like throws clause or you might keep it within the TR Cas so let's SK it within the TR C so now perfect okay so it is done so now if I run my uh test case one test case is going to pass and another test case is going to fail for the fail test case for the pass test case we'll mark it with the green color and when it is failing so we'll mark it with the red color and then on top of this additionally we are going to the add screen capture from the pass we are going to capture the the screenshot and where the screenshot is going to get generated it should get generated inside the reports there should be a folder called screenshot and inside the screenshot uh screenshot and then random mathematical number.png file should get generated and that screenshot should also be present in the report okay because that's what we're saying right so to the test what is this test right so if you see this is the extend test test so from the extend reports test we are adding this right so this screenshot should also get added to the ex report okay so let's quickly run this one and see what is happening so we can run from here also we can go here and even we can run from here let's run and wait till the execution finishes then we should be ideally able to see this screenshot folder getting created and screenshot getting attached to the uh extent spark report let's see okay for the first one uh it got passed second one it got failed right so let's go and see see when there is a test okay this is one concept I want to tell so at the rate test is there so we have some before test and after test test right for every test class those before and after test are going to get executed no matter what what happens right so what is that before and after those are defined where in the base stes only those are defined right so before class right so before class what will happen it will go read the properties file and all those things it will instance at the browser and it will go to the URL if you see it is also navigating to the URL right and then after that it will go to your test respective test annotation method and then whatever is written it will try to do perform all those and then after it is done it will execute the after test what is the after test is the tier down okay it will quit the driver so now if you see for the uh there are two test here right so first for the first test so the before before this one got executed this is before class okay this will happen once actually okay so if it is before test it could have happened two times okay so now okay so just let's go and see right here if you see the screenshot folder got created okay and if you see double click you see the it has captured this this screenshot okay so this is where it got failed and now if you see the extent spark report and if you open here right so you see this is pass this is a fail test case right sample fail test case so for this one also if you see it has navigated through here and it has taken this screenshot in the sign up or login page it got failed okay so because uh yeah so let's go to the code and see what is our base types Bas tpes okay uh Bas stpes okay so this is our before class and this is our after class okay so yeah so correct so okay so if you see uh uh this this has been already already done okay so now if you see the screenshot is getting generated over here and in the your spark report we can see the report is getting created okay and you can see the screenshot is getting attached for the second second run which is getting failed okay so that's all uh to this class guys and uh maybe we can do one thing we can go ahead and update this one in the custom listener right so we are adding all the Bas stpes Bas stepes so we'll get rid of this base stpes okay so let's get rid of this base stpes I'll say this test we'll just import okay just import okay so now you just say test test test okay you don't have to say base types Bas tpes Bas types right I think this is better this looks good now okay if you want I can rerun it one more time and we can see one fed one pass okay so okay so this is showing that ass error so we can see individually also see this is click on sign up and login page sample fil T is directly going and doing that error okay so initially URL is loaded navigating to the URL clicking on the sign up or login link clicked on sign up or login link then verify page title expected page title after page title right and then it's executing the second test and there what it is happening so it is uh actually going and there it is failing okay this is just a sample thing that I created over here just for the fill just this name sec to show you how the how it is going to capture the screenshot and all but here we will going to write the actual test and then we'll try to faill it okay so we'll see that okay and uh this is all to this session guys if you see if you guys need uh any um any answers to any of your questions just put them in the comment section I'll get back to as soon as possible please do subscribe to my channel as well as please uh share this with your friends and colleagues so that they can also get benefited out of it and uh this is the uh like end to the report and uh next session onwards we'll start with the data interesting right we'll try how to read the data from the Excel and all okay so stay tuned to Q automation classes guys thank you bye-bye
Info
Channel: QA Automation Classes
Views: 262
Rating: undefined out of 5
Keywords: Take screenshot for Failure Test Cases in Selenium, TestNG Listener, capture screenshot in selenium, capture screenshot in selenium testng, full page screenshot in selenium, how to capture screenshot in selenium webdriver, how to take screenshot for failed test cases in selenium testng, how to take screenshot in selenium, screenshot in selenium, take screenshot in selenium, take screenshot in selenium webdriver, takescreenshot in selenium, testng screenshot on failure
Id: qKnfCLPIO2I
Channel Id: undefined
Length: 14min 6sec (846 seconds)
Published: Thu Jan 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.