Taking Screenshots

Other topics

JAVA

Code to take and save screenshot :

public class Sample 
{
    public static void main (String[] args) 
    {
        *//Initialize Browser*
        System.setProperty("webdriver.gecko.driver", "**E:\\path\\to\\geckodriver.exe**");
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();        
        driver.get("https://www.google.com/");
        
        //Take Screesnshot
        File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
            //Save Screenshot in destination file
            FileUtils.copyFile(src, new File("D:\\screenshot.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Takes Screenshot :

File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

Stores Screenshot from source to destination :

FileUtils.copyFile(src, new File("D:\\screenshot.png"));

Syntax:

  • File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  • FileUtils.copyFile(src, new File("D:\screenshot.png"));

Contributors

Topic Id: 10094

Example Ids: 30968

This site is not affiliated with any of the contributors.