Selenium WebDriver - Take Screenshot and Partial Screenshot Commands

In this tutorial we will learn to take full screenshot and partial screenshot. In automation we capture the screenshots for reference. To capture screenshot in Selenium 'TakeScreenshot' interface is used.



Take Screenshot Command:

/************************************
 * Capture Screenshot               *
 ************************************ 
 */

/* Create object of File class of Java 
 * And capture screenshot using getScreenshotAs() method of Webdriver
 * Set Output type of screenshot taken as 'File'
*/
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

/* Use FileUtils class of Java 
 * Call it's copyFile method
 * Copy screenshot file to a location with some name and extension you want
*/
FileUtils.copyFile(screenshot, new File("D:\\screenshot.jpg"));




How to Take Partial Screenshot:

Sometimes we don't want to take screenshot of the full screen. Reasons might be, full size images would last in huge memory storage for image directory or sometimes we need to take element specific screenshot due to test case requirement which makes more sense.

Here is the Webdriver code snippet for taking partial screenshot:





Refer Selenium Webdriver Tutorials Series

Join Telegram channel at https://t.me/techlistic
Feel free to ask queries or share your thoughts in comments or email me.

Comments

  1. I appreciate your efforts for creating these quick reference blogs. Keep up the good work.
    Just an observation that TakeScreenshot is an Interface and not a Class.

    ReplyDelete

Post a Comment

Popular Posts

Top 10 Demo Websites to Practice Selenium Webdriver Online

Top 25 Must to know Selenium Webdriver Commands List

Selenium IDE - Sample Script With Most Commonly Used Commands

Assignment 6: Automate "User Registration" Process for E-commerce Website using Selenium

Assignment 5: Automate "Buy Product" Functionality for E-commerce Website using Selenium