Selenium WebDriver - Take Screenshot and Partial Screenshot Commands

In Selenium Webdriver we can also capture the screenshots for reference. To capture screenshot in Sleenium 'TakeScreenshot' class 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:


Hope it helps!




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

Comments

Post a Comment

Popular Posts

Top 10 Demo Websites to Practice Selenium Webdriver Online

Future of Selenium Automation Testing as a Career Option

How to learn Selenium Webdriver on your own online

Selenium Webdriver - Overview & Features