Interview Questions

Selenium WebDriver - How to capture screenshot in WebDriver?

Selenium WebDriver Interview question and Answers


(Continued from previous question...)

Selenium WebDriver - How to capture screenshot in WebDriver?

1 import org.junit.After;
2 import org.junit.Before;
3 import org.junit.Test;
4 import java.io.File;
5 import java.io.IOException;
6 import org.apache.commons.io.FileUtils;
7 import org.openqa.selenium.OutputType;
8 import org.openqa.selenium.TakesScreenshot;
9 import org.openqa.selenium.WebDriver;
10 import org.openqa.selenium.firefox.FirefoxDriver;
11
12 public class CaptureScreenshot {
13 WebDriver driver;
14 @Before
15 public void setUp() throws Exception {
16 driver = new FirefoxDriver();
17 driver.get("https://google.com");
18 }
19 @After
20 public void tearDown() throws Exception {
21 driver.quit();
22 }
23
24 @Test
25 public void test() throws IOException {
26 // Code to capture the screenshot
27 File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
28 // Code to copy the screenshot in the desired location
29 FileUtils.copyFile(scrFile, new File("C:\\CaptureScreenshot\\google.jpg"));
30 }
31 }

(Continued on next question...)

Other Interview Questions