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)
068                                            .getScreenshotAs(new ScreenShotFileOutput(targetDirName, name));
069            } catch (InterruptedException e) {
070                log.error(e, e);
071            }
072        }
073        return null;
074    }
075
076    public File dumpPageSource(WebDriver driver, String filename) {
077        if (driver == null) {
078            return null;
079        }
080        FileWriter writer = null;
081        try {
082            String location = System.getProperty("basedir") + File.separator + "target";
083            File outputFolder = new File(location);
084            if (!outputFolder.exists() || !outputFolder.isDirectory()) {
085                outputFolder = null;
086            }
087            if (outputFolder != null && !StringUtils.isBlank(targetDirName)) {
088                outputFolder = new File(outputFolder, targetDirName);
089                outputFolder.mkdir();
090            }
091            File tmpFile = File.createTempFile(filename, ".html", outputFolder);
092            log.trace(String.format("Created page source file named '%s'", tmpFile.getPath()));
093            writer = new FileWriter(tmpFile);
094            writer.write(driver.getPageSource());
095            return tmpFile;
096        } catch (IOException e) {
097            throw new WebDriverException(e);
098        } finally {
099            IOUtils.closeQuietly(writer);
100        }
101    }
102
103}