Screenshots play a vital role when it comes to gathering test evidence, as one can always question whether the scripts that have been designed are working as per expectations or not. We can take screenshots using Selenium Web driver.

Test Case we will be automating:
1. Launch Google in Chrome browser.
2. Write the keyword Selenium in the search box.
3. Capture the screenshot.
4. Save the screenshot as Google at C:\selenium\screenshot.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;

public class Screenshots {

    public static void main(String[] args) {

        // Telling Selenium to find Chrome Driver
        System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");

        // Initialize browser
        ChromeDriver driver = new ChromeDriver();

        // Maximize Browser Window
        driver.manage().window().maximize();

        // Launch google
        driver.get("https://www.google.com/");

        // Type Selenium in the search box
        WebElement searchBox = driver.findElement(By.id("lst-ib"));
        searchBox.sendKeys("Selenium");

        // Take Screenshot
        File srce = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        // Save the screenshot in the given path by the name google.png
        try {
            FileUtils.copyFile(srce, new File("C:\\selenium\\screenshot\\Google.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Close the browser
        driver.quit();

    }
}

Above program will not work as we have not imported the required package for FileUtils. I personally faced some issue while importing the package for FileUtils, as I was importing the incorrect package. The problem is that we would be presented with two similar looking package import option, but we will have to choose import org.apache.commons.io.FileUtils.

fileutils package

After importing the correct package, the program should be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;

public class Screenshots {

    public static void main(String[] args) {

        // Telling Selenium to find Chrome Driver
        System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");

        // Initialize browser
        ChromeDriver driver = new ChromeDriver();

        // Maximize Browser Window
        driver.manage().window().maximize();

        // Launch google
        driver.get("https://www.google.com/");

        // Type java in the search box
        WebElement searchBox = driver.findElement(By.id("lst-ib"));
        searchBox.sendKeys("Selenium");

        // Take Screenshot
        File srce = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        // Save the screenshot in the given path by the name google.png
        try {
            FileUtils.copyFile(srce, new File("C:\\selenium\\screenshot\\Google.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Close the browser
        driver.quit();

    }
}

Upon successful execution, we will have the screenshot at our desired location.

screenshot location

Now if we are writing a big program, and we need multiple screenshots for various steps, then the above code has two limitations:
1. We will have to write the code for taking screenshots again and again.
2. Here, the screenshot is being saved using only one file name. Say suppose we need two screenshots to be saved, but since we are using only one file name, the second screenshot will overwrite the first screenshot, so at any point, we will have only one screenshot (latest one).

The solution to above problems is to create a parameterized function, which will have the code to take screenshot along with some arrangement to save the screenshots with different names. So, whenever we require a screenshot we will just call this function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.IOException;

public class ScreenshotwithReusableCode {

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

        // Telling Selenium to find Chrome Driver
        System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");

        // Initialize browser
        ChromeDriver driver = new ChromeDriver();

        // Maximize Browser Window
        driver.manage().window().maximize();

        // Launch google
        driver.get("https://www.google.com/");
        // Capture screenshot
        screenshot(driver, "GoogleWindow");

        // Type Selenium in the search box
        WebElement searchBox = driver.findElement(By.id("lst-ib"));
        searchBox.sendKeys("Selenium");
        // Capture screenshot
        screenshot(driver, "SeleniuminTextBox");

        // Wait
        Thread.sleep(1000);

        // Click on suggested Drop down item
        driver.findElement(By.xpath(".//*[@id='sbse5']/div[2]")).click();

        // Wait
        Thread.sleep(1000);

        // Capture screenshot
        screenshot(driver, "SearchResults");

        // Close the browser
        driver.quit();

    }

    // Function to be re-used to take multiple screenshot with unique name
    public static void screenshot(WebDriver driver, String name) {
        // Take Screenshot
        File scre = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        // Save the screenshot in the given path by the name google.png
        try {
            FileUtils.copyFile(scre, new File("C:\\selenium\\screenshot\" + name + ".png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Upon successful execution, we can find all the screenshots at our desired location.