Thursday, October 25, 2012

How to take a screenshot of the window using Selenium WebDriver

Below code demonstrate about how to take a snapshot of the Desktop window using Selenium 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;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;



public class TakeAScreenShot
{
    public static void main(String[] args)
    {
        WebDriver driver = new FirefoxDriver();
        String s_FilePath="c:\\screenshot.jpg";
       
        driver.get("http://www.google.com/");
        try
        {
            File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            //Once we have the screenshot in our file 'srcFile' you can use all FileUtils methods like
            FileUtils.copyFile(srcFile, new File(s_FilePath));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.