• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / How to use FileHandler Class in Selenium for Screenshot

How to use FileHandler Class in Selenium for Screenshot

August 16, 2018 by Mukesh Otwani 13 Comments

FileHandler Class in Selenium for Screenshot

If you have upgraded to new version of Selenium 3.6.0 or newer version then you will not get new FileUtils class which we used to get in older versions of Selenium. FileHandler Class in Selenium for Screenshot and help us to copy screenshot from source to destination.

FileHandler is new Class in Selenium which help us the to store screenshots, create directory and so on. You can get full documentation of FileHandler using this link. 

Note- ChromeOption and FirefoxOption etc classes are added in new release of Selenium added few more features which you can try in your framework.

FileHandler Class in Selenium for Screenshot

Previously we have already seen how to capture screenshot in Selenium and How to capture screenshot only for failed test cases.

Steps to use FileHandler Class in Selenium for Screenshot

Step 1- Import the new package which is

import org.openqa.selenium.io.FileHandler;

Step 2- Call copy method of FileHandler Class which is static method and will ask two argument First is src and another is destination

Code will look like 

FileHandler.copy(ts.getScreenshotAs(OutputType.FILE), new File(“C\\Selenium\\Screenshot.png”));

Copy method will throw checked exception which you can handle either throws or try catch block of Java.

Final code will look like.

import java.io.File;
import java.io.IOException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.io.FileHandler;
import org.testng.annotations.Test;

public class ChromeHeadlessBrowser {

@Test
public void myfirst()
{
    // Set the chrome driver
    System.setProperty("webdriver.chrome.driver","./chromedriver.exe");

    // pass the option object in ChromeDriver constructor
    WebDriver driver=new ChromeDriver();

    // Provide any url
    driver.get("https://learn-automation.com/");

    System.out.println("Title is "+driver.getTitle());

    TakesScreenshot ts=(TakesScreenshot)driver;

    try {
        FileHandler.copy(ts.getScreenshotAs(OutputType.FILE), new File("C\\Selenium\\Screenshot.png"));
    } catch (WebDriverException e) {

    } catch (IOException e) {

    }

    // Close the instance
    driver.quit();

}

}

Another way to use old way

In older version of Selenium we used Apache Commons library which used to come along with Selenium but in new version of Selenium (3.6.0 or newer) we have to download it separately and then we can use the old Classes and methods. 

Library name is – Apache Commons Library 

Solution 1- if you are using Normal Java Project then go to below URL, download this library and then add into current project https://commons.apache.org/ 

Solution 2- If you are using Maven project which I always recommend then add below dependency which will add Apache commons library which you can use in your project.

That’s all I have for this post and you like this post or you find it useful then share with your friends and if you have any though to share then please let me know in the comment section.

Filed Under: Basic Selenium

Reader Interactions

Comments

  1. Kaustav Bag says

    July 2, 2023 at 10:59 PM

    Hi Mukesh, I was stuck with the old way as I didn’t add apache.comms dependency but this really helped. Been following your youtube over a year but never commented on anything. Today sending you an appreciation note on your blog. Thanks for your contribution towards testing community.

    Reply
    • Mukesh Otwani says

      July 24, 2023 at 12:21 AM

      Thank you so much Kaustav, really means alot to me. Keep learning.

      Reply
  2. avish sharma says

    September 14, 2020 at 8:11 PM

    Great I am stucking at the fileutils but now everything is going smoothly after learning from this.
    Thanks

    Reply
    • Mukesh Otwani says

      September 15, 2020 at 10:30 AM

      Hi Avish,

      Glad that my blog content has assisted you to achieve your task. Feel free to ask your doubts and concerns 🙂

      Reply
  3. Asha says

    January 8, 2020 at 11:24 PM

    Thank you. its very helpful. saved my day!

    Reply
  4. Kulal says

    November 25, 2019 at 10:19 PM

    Thank you

    Reply
    • Mukesh Otwani says

      November 26, 2019 at 2:53 PM

      You’re welcome…:)

      Reply
  5. shailaja says

    May 2, 2019 at 5:18 PM

    Thanks this article was very helpful

    Reply
    • Mukesh Otwani says

      May 2, 2019 at 7:25 PM

      Hi Shailaja,

      Thanks…:)

      Reply
  6. Gaurank says

    August 27, 2018 at 6:34 PM

    Nice Article !!

    Reply
    • Mukesh Otwani says

      August 28, 2018 at 8:31 AM

      Hi Gaurank,

      Thanks buddy…:)

      Reply
  7. Ruchi says

    August 22, 2018 at 9:54 PM

    Great, you keep us updated about new changes. Thanks.

    Reply
    • Mukesh Otwani says

      August 23, 2018 at 9:22 AM

      Hi Ruchi,

      I’ll try my level best to keep my subscribers with new topics…:)

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?