Original url:https://www.guru99.com/take-screenshot-selenium-webdriver.html
Screenshots is desirable for the bug analysis. Selenium can automatically take screenshots during execution. You need to type cast Webdriver instance to Takesscreenshot.
Taking screenshot in Selenium is a 3 Step process
Step 1) Convert Web Driver object to Takescreenshot
Takesscreenshot Scrshot = ((takesscreenshot) webdriver);
Step 2) Call Getscreenshotas method to create image file
File Srcfile=scrshot.getscreenshotas (Outputtype.file);
Step 3) Copy file to desired location
Example:in This Example we'll take screenshot of http://demo.guru99.com/V4/& save it as C:/test.png
Package Guru99takescreenshot;import Java.io.file;import Org.apache.commons.io.fileutils;import Org.openqa.selenium.outputtype;import Org.openqa.selenium.takesscreenshot;import Org.openqa.selenium.WebDriver; Import Org.openqa.selenium.firefox.firefoxdriver;import Org.testng.annotations.test;public class guru99takescreenshot {@Test public void Testguru99takescreenshot () throws Exception{webdriver driver; System.setproperty ("Webdriver.firefox.marionette", "C:\\geckodriver.exe"); Driver = new Firefoxdriver (); Goto URL Driver.get ("http://demo.guru99.com/V4/"); Call Take screenshot function this.takesnapshot (driver, "c://test.png"); }/** * This function would take screenshot * @param webdriver * @param filewithpath * @throws Exception */public static void Takesnapshot (Webdriver webdriver,string filewithpath) throws exception{//convert web Driver object to takescreenshot takesscreenshot scrshot = ((TAkesscreenshot) webdriver); Call Getscreenshotas method to create image file File Srcfile=scrshot.getscreenshotas (outputtype.file); Move image file to new destination file Destfile=new file (Filewithpath); Copy file at Destination fileutils.copyfile (srcfile, destfile); }}
[Selenium+java] How to take screenshot in Selenium Webdriver