Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Selenium Automation Testing

  • Introduction to Automation Testing
  • Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  • Locators in Selenium
  • How to launch the browsers ? 
  • Browser Navigation Commands in Selenium
  • Handling the textbox and button using selenium
  • Handling the radio button and checkbox using selenium
  • Handling the drop downs and multi-select dropdown
  • Validate the text validation and attributes values. 
  • Handling Images
  • Handling Mouse Events
  • Handling Keyboard Events
  • Handling Alert Popup
  • Handling iFrames
  • Handling Multiple Browsers or Tabs
  • Types of wait statement in selenium
  • How to take screenshots?
  • How to upload File in Selenium?
  • How to handle scroll function using JavaScriptExecutor?
  • How to handle basic functionality using JavaScriptExecutor?
  • How to handle advanced functionality using JavaScriptExecutor?
  • How to automate CAPTCHA?
  • How to handle cookies in selenium
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • Selenium Automation Testing
  • How to take screenshots?

How to take screenshots?

How to take screenshots?

You need Apache Commons IO for FileUtils.copyFile().

Add this to your Maven pom.xml if you use Maven:

<dependency>

    <groupId>commons-io</groupId>

    <artifactId>commons-io</artifactId>

    <version>2.15.1</version>

</dependency>

1. Take a Screenshot of the Entire Page

Sample Program: 

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;

import org.apache.commons.io.FileUtils; // Add Apache Commons IO dependency

public class ScreenshotExample {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        try {

            driver.get("https://example.com");

            // Take screenshot and store as a file format

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

            // Now copy the screenshot to desired location

            FileUtils.copyFile(src, new File("./screenshot.png"));

            System.out.println("Screenshot captured!");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            driver.quit();

        }

    }

}

2. Take a Screenshot of a Specific Element

import org.openqa.selenium.WebElement;

// Assume 'element' is already located:

WebElement element = driver.findElement(By.id("myElement"));

File src = element.getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(src, new File("./element-screenshot.png"));

3. Take a Screenshot in case of failure using TestNG

Using TestNG’s ITestListener:

Implement ITestListener to automatically capture screenshots on test failure:

import org.testng.ITestListener;

import org.testng.ITestResult;

import org.openqa.selenium.WebDriver;

public class ScreenshotListener implements ITestListener {

    @Override

    public void onTestFailure(ITestResult result) {

        Object testClass = result.getInstance();

        WebDriver driver = ((YourBaseTestClass) testClass).getDriver(); // get your driver instance

        String methodName = result.getName();

        takeScreenshot(driver, methodName + "_FAILED");

    }

    //... other overridden methods

}

4. Manual Screenshot Capture on Catch Block

If you’re not using any test framework hooks, you can take screenshots in your catch blocks:

try {

    // test steps

} catch (Exception e) {

    takeScreenshot(driver, "TestStep_Failed");

    throw e;

}

Best Practices

  • Include timestamps in the screenshot file name for uniqueness.
  • Store screenshots in a structured folder, e.g., ./screenshots/.
  • Attach screenshots to your test reports (many reporting tools support this, e.g., Allure, ExtentReports).

Sample Method with Timestamp

import java.text.SimpleDateFormat;

import java.util.Date;

public static void takeScreenshot(WebDriver driver, String fileName) {

    try {

        String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

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

        FileUtils.copyFile(src, new File("./screenshots/" + fileName + "_" + timestamp + ".png"));

    } catch (Exception e) {

        e.printStackTrace();

    }

}

Selenium Automation Testing
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
Types of wait statement in seleniumHow to upload File in Selenium?
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105