001/* 002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Sun Seng David TAN <[email protected]> 018 */ 019package org.nuxeo.functionaltests; 020 021import java.io.File; 022import java.io.FileOutputStream; 023import java.io.IOException; 024 025import org.apache.commons.io.IOUtils; 026import org.apache.commons.lang3.StringUtils; 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.openqa.selenium.OutputType; 030import org.openqa.selenium.WebDriverException; 031 032/** 033 * 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 034 * find it). 035 * <p> 036 * This temp file won't be deleted on exist. 037 * 038 * @author Sun Seng David TAN <[email protected]> 039 */ 040public class ScreenShotFileOutput implements OutputType<File> { 041 042 private static final Log log = LogFactory.getLog(ScreenShotFileOutput.class); 043 044 protected String targetDir; 045 046 protected String screenshotFilePrefix; 047 048 /** 049 * @param targetDir name of the target dir (can be null), will be ignored if file is created in temp directory. 050 * @param screenshotFilePrefix prefix of the screen shot file. 051 * @since 5.9.2 052 */ 053 public ScreenShotFileOutput(String targetDir, String screenshotFilePrefix) { 054 this.targetDir = targetDir; 055 this.screenshotFilePrefix = screenshotFilePrefix; 056 } 057 058 /** 059 * @param screenshotFilePrefix prefix of the screen shot file. 060 */ 061 public ScreenShotFileOutput(String screenshotFilePrefix) { 062 this.screenshotFilePrefix = screenshotFilePrefix; 063 } 064 065 @Override 066 public File convertFromBase64Png(String base64Png) { 067 byte[] data = BYTES.convertFromBase64Png(base64Png); 068 return convertFromPngBytes(data); 069 } 070 071 @Override 072 public File convertFromPngBytes(byte[] data) { 073 FileOutputStream fos = null; 074 try { 075 String location = System.getProperty("basedir") + File.separator + "target"; 076 File outputFolder = new File(location); 077 if (!outputFolder.exists() || !outputFolder.isDirectory()) { 078 outputFolder = null; 079 } 080 if (outputFolder != null && !StringUtils.isBlank(targetDir)) { 081 outputFolder = new File(outputFolder, targetDir); 082 outputFolder.mkdir(); 083 } 084 File tmpFile = File.createTempFile(screenshotFilePrefix, ".png", outputFolder); 085 log.trace(String.format("Created screenshot file named '%s'", tmpFile.getPath())); 086 fos = new FileOutputStream(tmpFile); 087 fos.write(data); 088 return tmpFile; 089 } catch (IOException e) { 090 throw new WebDriverException(e); 091 } finally { 092 IOUtils.closeQuietly(fos); 093 } 094 } 095 096}