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