001/** 002 * Copyright 2005-2015 The Kuali Foundation 003 * 004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 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 */ 016package org.kuali.rice.testtools.selenium; 017 018import org.apache.commons.io.FileUtils; 019import org.openqa.selenium.OutputType; 020import org.openqa.selenium.TakesScreenshot; 021import org.openqa.selenium.WebDriver; 022 023import java.io.File; 024import java.io.IOException; 025 026/** 027 * @author Kuali Rice Team (rice.collab@kuali.org) 028 */ 029public class WebDriverScreenshotHelper { 030 031 /** 032 * -Dremote.driver.screenshot.filename= appended with a formatted date time stamp and png file extension. 033 */ 034 private static final String REMOTE_DRIVER_SCREENSHOT_FILENAME = "remote.driver.screenshot.filename"; 035 036 /** 037 * -Dremote.driver.screenshot.dir= default is java working dir. 038 */ 039 private static final String REMOTE_DRIVER_SCREENSHOT_DIR = "remote.driver.screenshot.dir"; 040 041 /** 042 * -Dremote.driver.screenshot.archive.url= default is empty string. 043 * 044 * Used to link Jenkins output of screenshots to their archive. 045 */ 046 private static final String REMOTE_DRIVER_SCREENSHOT_ARCHIVE_URL = "remote.driver.screenshot.archive.url"; 047 048 /** 049 * -Dremote.driver.failure.screenshot= default is false 050 * 051 */ 052 private static final String REMOTE_DRIVER_FAILURE_SCREENSHOT = "remote.driver.failure.screenshot"; 053 054 /** 055 * -Dremote.driver.step.screenshot= default is false 056 */ 057 private static final String REMOTE_DRIVER_STEP_SCREENSHOT = "remote.driver.step.screenshot"; 058 059 /** 060 * Screenshots will be saved using either the value of (#REMOTE_DRIVER_SCREENSHOT_FILENAME or if none, testName.testNameMethod) 061 * appended with a date time stamp and the png file extension. 062 * 063 * @see WebDriverUtils#getDateTimeStampFormatted 064 * 065 * @param driver to use, if not of type TakesScreenshot no screenshot will be taken 066 * @param testName to save test as, unless #REMOTE_DRIVER_SCREENSHOT_FILENAME is set 067 * @param testMethodName to save test as, unless #REMOTE_DRIVER_SCREENSHOT_FILENAME is set 068 * @throws IOException 069 */ 070 public void screenshot(WebDriver driver, String testName, String testMethodName) throws IOException { 071 screenshot(driver, testName, testMethodName, ""); 072 } 073 074 /** 075 * Screenshots will be saved using either the value of (#REMOTE_DRIVER_SCREENSHOT_FILENAME or if none, testName.testNameMethod) 076 * appended with a date time stamp and the png file extension. 077 * 078 * @see WebDriverUtils#getDateTimeStampFormatted 079 * 080 * @param driver to use, if not of type TakesScreenshot no screenshot will be taken 081 * @param testName to save test as, unless #REMOTE_DRIVER_SCREENSHOT_FILENAME is set 082 * @param testMethodName to save test as, unless #REMOTE_DRIVER_SCREENSHOT_FILENAME is set 083 * @throws IOException 084 */ 085 public void screenshot(WebDriver driver, String testName, String testMethodName, String screenName) throws IOException { 086 if (driver instanceof TakesScreenshot) { 087 088 if (!"".equals(screenName)) { 089 screenName = "-" + screenName; 090 } 091 092 File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 093 // It would be nice to make the screenshot file name much more configurable. 094 String screenshotFileName = WebDriverUtils.getDateTimeStampFormatted() + "-" 095 + System.getProperty(REMOTE_DRIVER_SCREENSHOT_FILENAME, testName + "." + testMethodName) 096 + screenName + ".png"; 097 FileUtils.copyFile(scrFile, new File(System.getProperty(REMOTE_DRIVER_SCREENSHOT_DIR, ".") 098 + File.separator, screenshotFileName)); 099 String archiveUrl = System.getProperty(REMOTE_DRIVER_SCREENSHOT_ARCHIVE_URL, ""); 100 WebDriverUtils.jGrowl(driver, "Screenshot", false, archiveUrl + screenshotFileName); 101 } 102 } 103 104 /** 105 * @return false unless #REMOTE_DRIVER_FAILURE_SCREENSHOT is set to true. 106 */ 107 public boolean screenshotOnFailure() { 108 return "true".equals(System.getProperty(REMOTE_DRIVER_FAILURE_SCREENSHOT, "false")); 109 } 110 111 /** 112 * @return false unless #REMOTE_DRIVER_STEP_SCREENSHOT is set to true. 113 */ 114 public boolean screenshotSteps() { 115 return "true".equals(System.getProperty(REMOTE_DRIVER_STEP_SCREENSHOT, "false")); 116 } 117}