Ancora Imparo - I'm still learning (Michelangelo at 87)

How to Take Screenshots with Selenium WebDriver

It is a well-known concept that Automated testing is most important during the testing stage of a software. However, due to a range of failures encountered during the testing process, it is easy for anyone to get discouraged and uncomfortable with the prospect of re-test in an entire script. The introduction of screenshot in the Selenium WebDriver helps in detecting test failures on the spot. In this article, you will learn the different ways to take a screenshot with the Selenium WebDriver.

Screenshots Using Selenium WebDriver

There are three steps required to follow before you can successfully take a screenshot in Selenium. The steps are analyzed below:

1 Convert web  driver object  to TakeScreenshot

TakesScreenshot scrShot =((TakesScreenshot)webdriver);

2 Utilize the getScreenshotAs method to produce an image file

File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);

3 Copy file to desired location.

Programmers should note that the Selenium version 3.9.0 and above does not include the Apache Commons IO JAR. This can, however, be downloaded and integrated into your Work. Below is an example of how to take Screenshots with Selenium WebDriver. In the example, we will take a screenshot of https://testup.io/ and save it as C:/Test.png

package TestupTakeScreenshot;

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 TestupTakeScreenshot {
    @Test
    public void testTestupTakeScreenShot() throws Exception{

WebDriver driver ;
        System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
        driver = new FirefoxDriver();
        //goto url
        driver.get("https://testup.io/");
        //Call take screenshot function
        this.takeSnapShot(driver, "c://test.png") ;    
    }
    /**
     * This function will 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);
    }
}

About the Autor

Jamiu Idowu

Jamiu Idowu

Entrepreneur seeking to shape the world through IT and emerging technologies. Enjoys research and technical writing, and can serve as a bridge between technology and its users.

Liked this article?

We have a lot more where that came from!
Join the subscribers who stay ahead of the pack.

By entering your email, you agree to our Terms of Service and Privacy Policy.