View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.testtools.selenium;
17  
18  import org.apache.commons.io.FileUtils;
19  import org.openqa.selenium.OutputType;
20  import org.openqa.selenium.TakesScreenshot;
21  import org.openqa.selenium.WebDriver;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  /**
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  public class WebDriverScreenshotHelper {
30  
31      /**
32       * -Dremote.driver.screenshot.filename= appended with a formatted date time stamp and png file extension.
33       */
34      private static final String REMOTE_DRIVER_SCREENSHOT_FILENAME = "remote.driver.screenshot.filename";
35  
36      /**
37       * -Dremote.driver.screenshot.dir= default is java working dir.
38       */
39      private static final String REMOTE_DRIVER_SCREENSHOT_DIR = "remote.driver.screenshot.dir";
40  
41      /**
42       * -Dremote.driver.screenshot.archive.url= default is empty string.
43       *
44       * Used to link Jenkins output of screenshots to their archive.
45       */
46      private static final String REMOTE_DRIVER_SCREENSHOT_ARCHIVE_URL = "remote.driver.screenshot.archive.url";
47  
48      /**
49       * -Dremote.driver.failure.screenshot= default is false
50       *
51       */
52      private static final String REMOTE_DRIVER_FAILURE_SCREENSHOT = "remote.driver.failure.screenshot";
53  
54      /**
55       * -Dremote.driver.step.screenshot= default is false
56       */
57      private static final String REMOTE_DRIVER_STEP_SCREENSHOT = "remote.driver.step.screenshot";
58  
59      /**
60       * Screenshots will be saved using either the value of (#REMOTE_DRIVER_SCREENSHOT_FILENAME or if none, testName.testNameMethod)
61       * appended with a date time stamp and the png file extension.
62       *
63       * @see WebDriverUtils#getDateTimeStampFormatted
64       *
65       * @param driver to use, if not of type TakesScreenshot no screenshot will be taken
66       * @param testName to save test as, unless #REMOTE_DRIVER_SCREENSHOT_FILENAME is set
67       * @param testMethodName to save test as, unless #REMOTE_DRIVER_SCREENSHOT_FILENAME is set
68       * @throws IOException
69       */
70      public void screenshot(WebDriver driver, String testName, String testMethodName) throws IOException {
71          screenshot(driver, testName, testMethodName, "");
72      }
73  
74      /**
75       * Screenshots will be saved using either the value of (#REMOTE_DRIVER_SCREENSHOT_FILENAME or if none, testName.testNameMethod)
76       * appended with a date time stamp and the png file extension.
77       *
78       * @see WebDriverUtils#getDateTimeStampFormatted
79       *
80       * @param driver to use, if not of type TakesScreenshot no screenshot will be taken
81       * @param testName to save test as, unless #REMOTE_DRIVER_SCREENSHOT_FILENAME is set
82       * @param testMethodName to save test as, unless #REMOTE_DRIVER_SCREENSHOT_FILENAME is set
83       * @throws IOException
84       */
85      public void screenshot(WebDriver driver, String testName, String testMethodName, String screenName) throws IOException {
86          if (driver instanceof TakesScreenshot) {
87  
88              if (!"".equals(screenName)) {
89                  screenName = "-" + screenName;
90              }
91  
92              File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
93              // It would be nice to make the screenshot file name much more configurable.
94              String screenshotFileName = WebDriverUtils.getDateTimeStampFormatted() + "-"
95                      + System.getProperty(REMOTE_DRIVER_SCREENSHOT_FILENAME, testName + "." + testMethodName)
96                      + screenName + ".png";
97              FileUtils.copyFile(scrFile, new File(System.getProperty(REMOTE_DRIVER_SCREENSHOT_DIR, ".")
98                      + File.separator, screenshotFileName));
99              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 }