Capture Screenshots using Selenium WebDriver | Selenium WebDriver |

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to my channel H hair tutorials in this video we will see how we can capture the screenshots using selenium webdriver so basically while working with selenium in some scenarios we have to take the screen short side what is happening in the browser that we want to capture it actually ok so let's say like you are executing some 100 test cases you will not be sitting in front of the system and Cyril so if there is a failure in one test you will get the message but you still want to see the screen where exactly it is failed in the screen so in that case you need to capture the screenshot and store it in your local disk right or you can say if you want to see the step by step process how it is executed randomly ok or if you want to document it how it is executing but those scenarios you need to capture the screenshots basically so the requirement we have but we don't know how to do it so in this video we will see how we can achieve that ok so first let me open that Eclipse so here I just create one class file I will name it as screenshot test so I need a main method also so here I need the code for launching the browser right so I will just copied from my previous class so I have copied the code so here if you see I am launching the chrome driver and I'm opening w3schools dot-com homepage and I am clicking on the navigation bar ok so after this step I want to capture the screenshot okay so for capturing the screenshot selenium is providing us one interface that is thanks screenshot so you can see you should type the a C so here it says I in the starting right so that means it is a interface it is coming from or Qi dot open Q dot selenium so this is an interface that we need to use for capture in the screenshot but as you know like we cannot create an instance for interface right you can create the instance for class but you cannot create an instance this right so how you're going to use this take screenshot method and in this interface basically so here you should understand one thing so whatever the screenshot that we are taking that we are actually taking the browser screen on the right so whatever is happening in that application that is inside browser so you want to capture the screenshot of the browser right so let's say like just open this one okay so this one I want to capture this one the screen right so this browser instance is actually stored inside driver this driver variable by so this driver is actually having this browser instance so that means we need to take the help of driver and this takes screenshot combinely then only we can capture the screenshot okay so here what I will do I will cast this driver in to take screenshot so let me just right here take screenshot driver so here if you see I'm casting this driver in to take screenshot and I am storing in PS okay so now I will use this PS variable okay so ts dot here we have a method called get screenshot as okay so using this method you can actually capture the screenshot so if you see here this method is actually having one parameter that is output type so we need to pass the output type how you want it okay so let's see what exactly this output type is so let me just type output type okay if you put a dot here it shows so we have three types basically basic ste four bytes and file so when you use base64 here it says output of the general type is Java dot language string that means if you use this base64 this is going to return you a string value okay a string value of base64 encoded code okay whatever the screenshot that you are capturing for that image one base64 encoded value will be there so that way I'll do it will return as a string format so the next one is bytes in this case it will return you in the byte array format you can see here okay and the last one is file so if you use this one it will return you in the form of fine okay so the file is coming from java dot io package so first i will show you with file then we will move on to base64 and bytes also we'll see all these things ok so I will just use the file semicolon so here this is actually captured in the screenshot and it is storing in the temporary memory ok but we want to utilize this right here we are not mentioning anything like save in this location or say with this name or something like that right so we want to utilize this one and save it in to your local disk ok so further first we need to store this file right so here it is returning as a file instance so I need to create the file instance here I will say final file so next I want to store this file into my local okay so for storing we are going to use one class that is file details okay so this file it will see is coming from War G dot Apache dot comments that I want okay so in case if you don't see this file Luton's what you will do is just copy the file you tails like this paste it here to the Google ok because just you know you don't know where exactly this file utens is coming from in that case you just type within the Google file utils so here you can see up our see comments I bow so go to the mavin repository just search for that one Apache Commons Iowa so this is the first leg of it okay so this library you need to add in your palm file if you are using a normal Java project you need to download this one in the form of a jar now once your comments by one jar okay so then file util start we have a method called copy file how can we want to copy this file instance empower local right so in the copy file it asks me for two variables one is the source fine that is this one because we have the screenshot in the form of this file right so that is the source so let me just put it here and the next one is destination pipe where exactly you want to store this so here I am going to create a file instance again with the file path and file name so here what I will do I will store it in the same project so here I don't have any screenshots folder right so I'll just create one screenshots so I have screenshots folder created so for referring to the current project you need to put a dot ok then the folder name so inside this folder I want to store this sa image 1 ok and you need to give the extension also thank you want in the PNG format or JPEG format ok so there are some image formats right you can use any of those image format ok so here I have used PNG format so my file name is image 1 ok so as we are dealing with this file your deals it may throw some exception IO exception so you can just surround it with the Tri cache or you can throw the exception itself so now till here it will copy the screenshot into local so after this what I will do I will quit the browser so let me just execute this so now it has to open the Chrome browser navigate to w3schools then mouse over on the navigation menu ok then take a screenshot and store it in the local see how quick it is so it has actually taken the screenshot so this file location I will go ok so so here is the location ok so you can see it has actually captured the screenshot see the navigation menu so this is how you actually capture the screenshot so in this scenario we actually use to this file utils right so and use this file output iris file okay so we have already seen this so let's just see the next one okay we have three types of output type right so we'll see the next one so here ii TS start get screen shot this is the method we are going to use and we are changing the output type here okay so now what I will do I will give the type as base 64 this time this one okay so this is going to return me a string value so I will store that value in the one string I will name it as just a pair 64th chord okay so using this basic sticker now we have the image whatever the captured image that is in the format string so now we need to convert this string into image okay so further you can use file output streams okay you can create one instance per file output stream so while creating the file output string you need to mention the file okay just like we created here we need to create one file also here like what with what name and weight it has to look it ok where it has to store basically so I'll just copy this I'll change the name to image - okay we have already tried PNG right so we will try with jpg now so you need to actually write this one use this page 64 and create this file right so you know right for output streams are used for writing the data into the file so generally for writing what we use we use a write method right so this right method is actually taking byte array or integer or byte array and integer variables right but we don't have anything like this string okay this basic steeper so what I need to do I need to convert this base 64 into byte array let me do it outside this one okay so here I'll just take a byte array I'll name it as byte array only so we have a class called basic stipple available in the Java okay the what we need to decode it trail because this one is encoded one bay 64 encoded code so get the decoder dot you can see we have a method called decode so this decode is having byte array as a parameter or string as a parameter so in with the string one right so we need to pass this encoded value here this basic c4n quadrant value so let me just put a semicolon so this one is converted into byte array okay so now this by the end we need to pass to this right method so after writing I want to close this file output stream always remember you need to close the file output streams any stream file input stream our output streams okay so close it once the operation is completed we will close it so in my earlier operation we are actually capturing the screenshot in the form of file and we are copying that file into local file local disk by changing this one part and this one but here we are capturing this in the format basic C 4 and we are converting that into byte array then we are actually creating one file output stream with the file name where exactly we want to store then we are writing this fight array into this one okay so let me just execute this see that image is capture okay here we have one JPEG image now hiccup check JPEG right see we are able to open the image okay so next we have another output type that is byte array right so here it is returning me directly binary in the earlier format it is giving me a base64 encoded one then I am converting that into this byte array right but if you use this one it will directly give you binary on Lee okay this one you can directly store this here you no need to decode okay so let me just put it in the next one and just comment this copy so here I need the pipe right so this is the third one so here it is already giving me into byte array so I no need to encode or decode anything okay so I change this to image three and we'll see jpg format No so let me just run this the remaining code is same okay for base64 and for this bytes it's just one extra step if you take into byte array you no need to again convert it okay so the image is captured so if you go here image 3 jpg so let me open that see I am able to see the screenshot right so this is how basically you need to capture the screenshots but you cannot do this thing every time right this is fine now you learnt how to capture the screenshot but you have to stick to one thing and that thing also you want to you place in the entire project so how do you do that in that case so what we can do we can create one any library extra library so generally in our frameworks you really have some utility library or something right even have one class file where we keep all the reusable methods right so here also I'm going to do the same thing I'll create one thing I'll name it as utils ok so here inside this one I need one static method a normal it doesn't return anything anyway capture screenshot so basically here if you see for capturing the screenshots what things we need we need the path and we need the driver instance right so let me just try it the driver instance as a parameter because we are going to pass the driver instance here and the path where exactly you want this you can do one thing you can directly pass the path or you can simply pass the filename also so what I will do I'll just pass the file okay I'll make the pot here itself so now let me just copy this core this is the simple one right so let me just copy this I'll just paste it here okay so here if you see what I need to change anyway this driver instance is referring here so let me import the things okay so this one this is the part I'm referring so this is going to be changed the file name will be changing right so I'll put the file name here so now the method is ready okay so now we will use this method so in real time projects they are not going to write this one right wherever we want to take the screenshot always you will not write this okay you're going to make it as a very simple one like this so you kept it in a utils class file okay this class is accessible for entire project so you can directly call the method you'll need to just pass the parameters so I'll just comment this one also let me comment these things I will delete these images okay so now we don't have any images in the screenshots folder so just out them this navigating to this website I will capture one screenshot then after this one after navigating to this one then I will capture another one okay so inside utils class II have the method okay it will start capture screenshot we need to pass the tracker instance here also the dragon instance name is driver only then the file name okay so here I am say oMG dot PNG okay so your file name and your extension then just copy the same I need to change this file name to something image to they will jpg so now we will execute this one and we will see it has to create two images right so we are not writing this entire code here c3 lines of code if you want to write this for these two steps three plus three six lines if you want to repeat in the entire project it will be more right so that is a reason we are going to take the help of this reusability so we will create one utils class and we will put it into this one it can be anything utils in your project you might have something different name okay so let me just execute this so the browser is open it will take one screenshot here and one here okay so the bronzer is closed so let me check the screenshots see we have one screenshot and we have second screenshot the first one is actually after navigating to this page immediately after opening w3schools dot cup then the second one is actually our to navigate into this menu okay see how easy this one is which type of output you want that is your wish anything is just saying it is going to produce the same image okay you can use a file output or you can use a base64 or byte array anything so that is about this capturing screenshot guys if you have any doubts please let me know in the comment section below thank you for watching bye bye I hope you enjoy this issue if you liked this video please hit the like buttom and also share with your friends thanks for watching bye bye
Info
Channel: H Y R Tutorials
Views: 4,408
Rating: undefined out of 5
Keywords: hyr tutorials, h y r tutorials, capture screenshots using selenium webdriver, how to capture screenshot in selenium webdriver, capture screenshot with selenium java webdriver, capture screenshot in selenium, take screenshot in selenium webdriver, screenshot in selenium webdriver, selenium webdriver tutorial for beginners, screenshot in selenium, selenium webdriver, how to take screenshot in selenium webdriver, webdriver, take screenshot in selenium, how to capture screenshot, java
Id: Ld9qJ4psQmI
Channel Id: undefined
Length: 19min 44sec (1184 seconds)
Published: Fri Jul 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.