How To Take Screenshot

In this article, we will learn how to take screenshot in Selenium. A Screenshot in Selenium Webdriver is used for bug analysis.

How to Take Screenshot in Selenium :

To capture screenshots of the visible portion of the page we use following steps :

Step 1: Convert web driver object to TakeScreenshot.

TakesScreenshot ts =((TakesScreenshot)driver);

Step 2: To create image file, call getScreenshotAs() method of TakesScreenshot interface and pass (OutputType.FILE) as argument.

File source = ts.getScreenshotAs(OutputType.FILE);

Step 3: After taking screenshot it will be stored in random location therefore we need to create the new object of file class and pass the expected destination path as string argument.

File dest = new File(“Path of folder\fileName.jpg”);

Step 4: Copy the source file to destination location.

FileHandler.copy(source, dest);

Example:

CODE
package screenshot;

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.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;

public class take_screenshot {

	public static void main(String[] args) throws IOException {

		System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Files\\chromedriver.exe");
		
		WebDriver driver = new ChromeDriver();
		
		// go to url
		driver.get("https://www.bookofnetwork.com/");
		
		// maximize url
		driver.manage().window().maximize();
		
		// Convert web driver object to TakeScreenshot
		TakesScreenshot ts = (TakesScreenshot)driver;
		
		// Call getScreenshotAs method to create image file
		File source = ts.getScreenshotAs(OutputType.FILE);
		
		// create new object of file class for desired location
		File destination = new File("C://automation.jpg");
		
		//Copy file at destination
		FileHandler.copy(source, destination);	
	}
}

#How_to_take_Screenshot_in_selenium_webdriver_with_example #How_to_take_Screenshot_using_selenium

(New page will open, for Comment)

Not yet commented...