001 /* 002 * Created on Mar 13, 2009 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 * 014 * Copyright @2009 the original author or authors. 015 */ 016 package org.fest.swing.junit.runner; 017 018 import static java.io.File.separator; 019 import static java.util.logging.Level.WARNING; 020 import static org.fest.swing.image.ImageFileExtensions.PNG; 021 import static org.fest.util.Strings.concat; 022 import static org.fest.util.Strings.quote; 023 024 import java.io.File; 025 import java.util.logging.Logger; 026 027 import org.fest.swing.image.ScreenshotTaker; 028 029 /** 030 * Understands taking a screenshot of the desktop when a GUI test fails. 031 * 032 * @author Alex Ruiz 033 */ 034 public class FailureScreenshotTaker { 035 036 private static Logger logger = Logger.getAnonymousLogger(); 037 038 private final File imageFolder; 039 private final ScreenshotTaker screenshotTaker; 040 041 /** 042 * Creates a new </code>{@link FailureScreenshotTaker}</code>. 043 * @param imageFolder the folder where screenshots will be saved to. 044 */ 045 public FailureScreenshotTaker(File imageFolder) { 046 this(imageFolder, new ScreenshotTaker()); 047 } 048 049 FailureScreenshotTaker(File imageFolder, ScreenshotTaker screenshotTaker) { 050 this.imageFolder = imageFolder; 051 this.screenshotTaker = screenshotTaker; 052 } 053 054 /** 055 * Saves a screenshot of the desktop using the given description as the file name. 056 * @param failedTest the description of the test failure. 057 */ 058 public void saveScreenshot(String failedTest) { 059 try { 060 String fileName = concat(imageFolder.getCanonicalPath(), separator, failedTest, ".", PNG); 061 screenshotTaker.saveDesktopAsPng(fileName); 062 logger.info(concat("Screenshot of failed test saved as ", quote(fileName))); 063 } catch (Exception e) { 064 logger.log(WARNING, concat("Unable to take screenshot of failed test ", quote(failedTest)), e); 065 } 066 } 067 }