03. Capture Screenshot

Capturing screenshot of software web application page is very easy in selenium webdriver.

As we knows, It is very basic required thing in software automation tools to capture screenshot on test case failure or whenever required during test case execution.

In Selenium WebDriver/Selenium 2, we need to capture screenshot of web page using bellow syntax.

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

Then we have to store it in our local drive using bellow given syntax. You can change/provide your own file destination path and name.

FileUtils.copyFile(screenshot, new File("D:\\screenshot.jpg));
Now we need to import bellow given header files to get support of capture screenshot in webdriver.

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;


Sample Script:

package Others;
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.testng.annotations.Test;
import org.apache.commons.io.FileUtils;
public class testsample {

            @Test
            public void Capturing_Screenshot() throws InterruptedException, IOException
            {

                        //1. Open the Chrome Browser
                        System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");   
                        WebDriver driver = new ChromeDriver();                
                       
                        //Open the URL
                        driver.get("http://www.google.com");

                        // Cature the screenshot
                        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                        FileUtils.copyFile(screenshot, new File("D:\\screenshot.jpg"));
                        System.out.print("Screenshot is captured and stored in your D: Drive");
            }
}



1 comment:

  1. You really need to work on your English! To bellow is to roar, usually in pain! This was just one of many, many grammar issues.

    ReplyDelete