How To Take A Screenshot Using Selenium
► Download Transcript and Code https://www.rexjones2.com/selenium-ta...
► Documentation via GitHub https://github.com/RexJonesII/Seleniu...
► Free Selenium PDF Book: https://www.rexjones2.com/book-seleni...
► Free Java PDF Book: https://www.rexjones2.com/book-part-1...
► All Paperback & eBooks: http://tinyurl.com/Rex-Allen-Jones-Books
► Transcript
In this video, we are going to take a screenshot of this GreyHound web page using Selenium.
Let’s go to Eclipse and before testing. We set up Chrome and maximize our window.
Let’s go to test and load the web page driver.get(“https://www.greyhound.com”). Next, we write TakesScreenshot. TakesScreenshot is an interface that is used to capture a screenshot and store that screenshot. Object reference is screenshot =.
Now we must type cast TakesScreenshot by writing a parenthesis then TakesScreenshot inside the parenthesis and driver. The purpose of type casting is to convert an object into a different type of object. In this example, we are going to convert driver to TakesScreenshot.
Next, we write screenshot dot getScreenshotAs. The target parameter is OutputType. So we write OutputType dot then select FILE. getScreenshotAs performs the action of capturing the screenshot and storing the screenshot in a certain location. When we capture the screenshot, it will become our source. Therefore, we write File source =.
Next is the destination – File destination; File is a Java class that represents a file or it represents a path directory. Let’s write our directory = new File(System.getProperty(“user.dir”) + “/Screenshots/GreyHound.png”); Import File.
user.dir is the working directory, Screenshots is the folder name and GreyHound is the file name. I’m going to create a folder for Screenshots. Right click Online Training – Teaching, right click, New then select Folder. Screenshots is the folder name.
Last, we handle the screenshot by copying from the source to the destination. We write FileHandler.copy from source to destination. Add a try catch block to surround FileHandler. The try catch block will try FileHandler if there is an exception then it will catch the exception and print the stack trace. Know what, let’s also print the Screenshot: sysout(“Screenshot Located At ” + destination);
Now, let’s Run
The screenshot is not available because we have to Refresh. Maybe in a different video, I will share how to automatically Refresh the project. For now, let’s Right Click then Refresh. Here’s the screenshot, right click, Open With, Other, GIF Image, click OK. Here’s the screenshot of Greyhound. That’s it for taking a screenshot of a page using Selenium.
#Screenshot #SeleniumAutomation #BeginnerSeleniumTutorials #SeleniumWebDriver