001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Sun Seng David TAN <stan@nuxeo.com> 016 */ 017package org.nuxeo.functionaltests; 018 019import java.io.File; 020import java.io.FileOutputStream; 021import java.io.IOException; 022 023import org.apache.commons.io.IOUtils; 024import org.apache.commons.lang.StringUtils; 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.openqa.selenium.OutputType; 028import org.openqa.selenium.WebDriverException; 029 030/** 031 * Screenshot into a temp file (will try to save it in maven base dir/target, save it in the system temp folder if can't 032 * find it). 033 * <p> 034 * This temp file won't be deleted on exist. 035 * 036 * @author Sun Seng David TAN <stan@nuxeo.com> 037 */ 038public class ScreenShotFileOutput implements OutputType<File> { 039 040 private static final Log log = LogFactory.getLog(ScreenShotFileOutput.class); 041 042 protected String targetDir; 043 044 protected String screenshotFilePrefix; 045 046 /** 047 * @param targetDir name of the target dir (can be null), will be ignored if file is created in temp directory. 048 * @param screenshotFilePrefix prefix of the screen shot file. 049 * @since 5.9.2 050 */ 051 public ScreenShotFileOutput(String targetDir, String screenshotFilePrefix) { 052 this.targetDir = targetDir; 053 this.screenshotFilePrefix = screenshotFilePrefix; 054 } 055 056 /** 057 * @param screenshotFilePrefix prefix of the screen shot file. 058 */ 059 public ScreenShotFileOutput(String screenshotFilePrefix) { 060 this.screenshotFilePrefix = screenshotFilePrefix; 061 } 062 063 @Override 064 public File convertFromBase64Png(String base64Png) { 065 byte[] data = BYTES.convertFromBase64Png(base64Png); 066 return convertFromPngBytes(data); 067 } 068 069 @Override 070 public File convertFromPngBytes(byte[] data) { 071 FileOutputStream fos = null; 072 try { 073 String location = System.getProperty("basedir") + File.separator + "target"; 074 File outputFolder = new File(location); 075 if (!outputFolder.exists() || !outputFolder.isDirectory()) { 076 outputFolder = null; 077 } 078 if (outputFolder != null && !StringUtils.isBlank(targetDir)) { 079 outputFolder = new File(outputFolder, targetDir); 080 outputFolder.mkdir(); 081 } 082 File tmpFile = File.createTempFile(screenshotFilePrefix, ".png", outputFolder); 083 log.trace(String.format("Created screenshot file named '%s'", tmpFile.getPath())); 084 fos = new FileOutputStream(tmpFile); 085 fos.write(data); 086 return tmpFile; 087 } catch (IOException e) { 088 throw new WebDriverException(e); 089 } finally { 090 IOUtils.closeQuietly(fos); 091 } 092 } 093 094}