001/* 002 * (C) Copyright 2014 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-2.1.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 * Anahide Tchertchian 016 */ 017package org.nuxeo.functionaltests; 018 019import java.io.File; 020import java.io.FileWriter; 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.TakesScreenshot; 028import org.openqa.selenium.WebDriver; 029import org.openqa.selenium.WebDriverException; 030 031/** 032 * Helper class to take screenshots. 033 * <p> 034 * Allows taking a png screenshot and the HTML source of the page. 035 * <p> 036 * Files are saved in a file in the maven base dir/target, and fallback on the the system temp folder if can't find it. 037 * <p> 038 * This temp file won't be deleted on exist. 039 * 040 * @since 5.9.2 041 */ 042public class ScreenshotTaker { 043 044 private static final Log log = LogFactory.getLog(ScreenshotTaker.class); 045 046 protected final String targetDirName; 047 048 public ScreenshotTaker() { 049 this(null); 050 } 051 052 public ScreenshotTaker(String targetDirName) { 053 super(); 054 this.targetDirName = targetDirName; 055 } 056 057 public File takeScreenshot(WebDriver driver, String filename) { 058 if (driver == null) { 059 return null; 060 } 061 if (TakesScreenshot.class.isInstance(driver)) { 062 try { 063 Thread.sleep(250); 064 String name = filename; 065 return TakesScreenshot.class.cast(driver).getScreenshotAs(new ScreenShotFileOutput(targetDirName, name)); 066 } catch (InterruptedException e) { 067 log.error(e, e); 068 } 069 } 070 return null; 071 } 072 073 public File dumpPageSource(WebDriver driver, String filename) { 074 if (driver == null) { 075 return null; 076 } 077 FileWriter writer = null; 078 try { 079 String location = System.getProperty("basedir") + File.separator + "target"; 080 File outputFolder = new File(location); 081 if (!outputFolder.exists() || !outputFolder.isDirectory()) { 082 outputFolder = null; 083 } 084 if (outputFolder != null && !StringUtils.isBlank(targetDirName)) { 085 outputFolder = new File(outputFolder, targetDirName); 086 outputFolder.mkdir(); 087 } 088 File tmpFile = File.createTempFile(filename, ".html", outputFolder); 089 log.trace(String.format("Created page source file named '%s'", tmpFile.getPath())); 090 writer = new FileWriter(tmpFile); 091 writer.write(driver.getPageSource()); 092 return tmpFile; 093 } catch (IOException e) { 094 throw new WebDriverException(e); 095 } finally { 096 IOUtils.closeQuietly(writer); 097 } 098 } 099 100}