Capture Screenshot in Selenium Webdriver

Hello welcome to Selenium tutorials, in this post we will see how to capture Screenshot in Selenium Webdriver

In automation, it is mandatory to take the screenshot for verification so that can prove also that your test case has covered certain functionality or not.Screenshots help you a lot when your test case will fail so that you can identify what went wrong in your script or in your application.

I also implemented screenshot on failure in my framework which makes my test more robust so I would recommend you to implement the same in your framework too.

How to capture Screenshot in Selenium webdriver


For taking screenshots Selenium has provided TakesScreenShot interface in this interface you can use getScreenshotAs method which will capture the entire screenshot in form of file then using FileUtils we can copy screenshots from one location to another location

Scenario – Open Google and take screenshot

Let’s implement the same

For usage of FleUtils nee to import below maven defendency
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>

Code for capture Screenshot

package com.qaearth.test;
import java.io.File;
import java.io.IOException;
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;
public class ScreenshotExample {
public void TestJavaS1() {
// Open Firefox
System.setProperty("webdriver.gecko.driver", "Drivers/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
// Maximize the window
driver.manage().window().maximize();
// Pass the url
driver.get("http://www.google.com");
// Take screenshot and store as a file format
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
// now copy the  screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("C:/selenium/error.png"));
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
}



If you use above code then it will take screenshot and will store in C:/selenium and screenshot name will be error.png

Now consider a scenario where you have to take multiple screenshots then above code will be repetitive so for this we will create a small method which captures screenshots and will call this method from our script.

public void TestJavaS1() {
// Open Firefox
System.setProperty("webdriver.gecko.driver", "Drivers/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
// Maximize the window
driver.manage().window().maximize();
// Pass the url
driver.get("http://www.google.com");
// Take screenshot and store as a file format
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
// now copy the  screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("C:/selenium/"+System.currentTimeMillis()+".png"));
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
So this method we can call from our test case and call multiple time.



Complete program for Screenshot in Selenium Webdriver

package com.qaearth.test;
import java.io.File;
import java.io.IOException;
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;
public class ScreenshotExample {
public void TestJavaS1() {
// Open Firefox
System.setProperty("webdriver.gecko.driver", "Drivers/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
// Maximize the window
driver.manage().window().maximize();
// Pass the url
driver.get("http://www.google.com");
// Take screenshot and store as a file format
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
// now copy the  screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("C:/selenium/"+System.currentTimeMillis()+".png"));
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
So in above program you can see I called captureScreenshot method three times like this you can create reusable actions in your script and you can use the same.