Thursday, August 29, 2013

Take a screenshot with Selenium WebDriver


It is advisable to take screenshots of failed test cases for further analysis and proof of failure.
 
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Copy screenshot to somewhere 
FileUtils.copyFile(screenshot , new File("c:\\tmp\\screenshot.png"));
 
(OR)
public static void takeScreenShot(WebDriver driver, String screenshotPath) throws Exception {
try {
// Convert web driver object to TakeScreenshot TakesScreenshot scrShot = ((TakesScreenshot) driver); // Call getScreenshotAs method to create image file File screenShotImage = scrShot.getScreenshotAs(OutputType.FILE); // Move image file to new destination File destFile = new File(screenshotPath); // Copy file to Destination FileUtils.copyFile(screenShotImage, destFile); } catch (Exception e) { APP_LOGS.error(">>>Error while Taking ScreenShot :<<<" + e.getMessage()); } }

Full Page Screenshot

private void takeFullPageScreenshotZoom(String outputFileName) {
((JavascriptExecutor) driver).executeScript("document.body.style.zoom=(top.window.screen.height-70)/Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);");
takeScreenShot(driver,outputFileName); // declared above
}
Take a screenshot with Selenium RemoteWebDriver:
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);

Take a partial Screenshot:

public void takePartialScreenShot(WebElement element) throws IOException {

String screenShot = System.getProperty("user.dir") + \\screenShot.png;

File screen = ((TakesScreenshot) this.driver).getScreenshotAs(OutputType.FILE);
Point p = element.getLocation();
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
BufferedImage img = ImageIO.read(screen);
BufferedImage dest = img.getSubimage(p.getX(), p.getY(), width,
height);
ImageIO.write(dest, "png", screen);
FileUtils.copyFile(screen, new File(screenShot));

}
 
Take a screenshot in after method annotation
@AfterMethod
public void takeScreenShotOnFailure(ITestResult testResult) throws IOException {
    if (testResult.getStatus() == ITestResult.FAILURE) {
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("errorScreenshots\\" + testResult.getName() + "-"
                + Arrays.toString(testResult.getParameters()) +  ".jpg"));
    }

Take a screenshot for a webelement:
public void captureElementScreenshot(WebElement element) throws IOException{
        //Capture entire page screenshot as buffer.
        File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        //Used selenium getSize() method to get height and width of element.
        int ImageWidth = element.getSize().getWidth();
        int ImageHeight = element.getSize().getHeight();
        //Used selenium Point class to get x y coordinates of Image element.
        //get location(x y coordinates) of the element.
        Point point = element.getLocation();
        int xcord = point.getX();
        int ycord = point.getY();
        //Reading full image screenshot.
        BufferedImage img = ImageIO.read(screen);
        //cut Image using height, width and x y coordinates parameters.
        BufferedImage dest = img.getSubimage(xcord, ycord, ImageWidth, ImageHeight);
        ImageIO.write(dest, "png", screen);
        //Used FileUtils class of apache.commons.io.
        //save Image screenshot In D: drive.
        FileUtils.copyFile(screen, new File("D:\\screenshot.png"));
}

No comments:

Post a Comment