When we create an automation framework in selenium or any automation tool, it becomes critical to report issues with screenshot in case test execution fails at any of the steps.
While automating using selenium webdriver, we can use either the Webdriver class or the Robot class to create method for capturing screenshot.
Below code explains both the methods to capture screenshot in Selenium Webdriver.
package Selenium;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class CaptureScreenshot
{
public static void CaptureScreenshotWebDriver(WebDriver driver,String filePath)
{
try
{
Thread.sleep(20000);
File srcFile= ((InternetExplorerDriver) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File(filePath),true);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void CaptureScreenshotRobotClass(String filePath)
{
try
{
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "jpeg", new File(filePath));
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
No comments:
Post a Comment