0 votes
in Selenium by
Can you capture a screenshot using Selenium? If yes, write a simple code to illustrate the same.

1 Answer

0 votes
by
Yes, using a web driver in Selenium, we can capture the screenshot. Following is the code to do the same:

import org.junit.After;

import org.junit.Before;

import org.junit.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 TakeScreenshot {

WebDriver drv;

   @ Before

   public void setUp() throws Exception {

   driver = new FirefoxDriver();

   drv.get("https://google.com");

}

   @ After

   public void tearDown() throws Exception {

   drv.quit();

   }

   @ Test

   public void test() throws IOException {

   // Capture the screenshot

   File scrFile = ((TakeScreenshot)drv).getScreenshotAs(OutputType.FILE);

   // Code for pasting screenshot to a user-specified location

   FileUtils.copyFile(scrFile, new File("C:\\Screenshot\\Scr.jpg"))

   }

}

Related questions

0 votes
asked Jan 30, 2020 in Selenium by rajeshsharma
0 votes
asked Dec 4, 2020 in Amazon Elastic Compute Cloud EC2 by sharadyadav1986
...