1   package com.atlassian.selenium.junit4;
2   
3   import com.atlassian.selenium.Browser;
4   import com.atlassian.selenium.SeleniumClient;
5   import com.atlassian.selenium.SeleniumConfiguration;
6   import com.atlassian.selenium.SeleniumStarter;
7   import org.apache.commons.lang.StringUtils;
8   import org.junit.runner.Description;
9   import org.junit.runner.notification.Failure;
10  import org.junit.runner.notification.RunListener;
11  
12  import java.io.UnsupportedEncodingException;
13  import java.net.URLEncoder;
14  
15  /**
16   * This is a {@link org.junit.runner.notification.RunListener} and captures a screenshot when a test fails.
17   * It writes the screenshot into the output directory configured for the maven surefire plugin. The location of this directory
18   * is exposed via the system property 'reportsDirectory' by the AMPS IntegrationTestMojo.
19   * 
20   * It will only take a screenshot if the browser is FireFox. Selenium requires for IE a plugin to be able to take screenshots.
21   *
22   * @since 2.0
23   */
24  public class CaptureScreenshotListener extends RunListener
25  {
26      public CaptureScreenshotListener()
27      {
28          super();
29      }
30  
31      @Override
32      public void testFailure(final Failure failure) throws Exception
33      {
34          //This is the output directory configured for the maven surefire plugin.
35          final String reportsDirectory = System.getProperty("reportsDirectory");
36          if (!StringUtils.isEmpty(reportsDirectory))
37          {
38              try
39              {
40                  //Can throw NPE, if selenium client is not initialised yet.
41                  final SeleniumClient seleniumClient = SeleniumStarter.getInstance().getSeleniumClient((SeleniumConfiguration) null);
42                  if (seleniumClient.getBrowser().equals(Browser.FIREFOX))
43                  {
44                      //Default background color is WHITE
45                      seleniumClient.captureEntirePageScreenshot(reportsDirectory + "/" + createSreenshotFileName(failure.getDescription()), "background=#FFFFFF");
46                  }
47              }
48              catch (Exception e)
49              {
50                  //ignored
51              }
52          }
53      }
54  
55      protected String createSreenshotFileName(Description description)
56      {
57          final String displayName = description.getDisplayName();
58          int startIndex = displayName.indexOf("(");
59          int endIndex = displayName.lastIndexOf(")");
60          final String testName = displayName.substring(0, startIndex);
61  
62          String filename;
63          if (startIndex != -1 && endIndex != -1)
64          {
65              filename = displayName.substring(startIndex + 1, endIndex);
66              if (!StringUtils.isEmpty(testName))
67              {
68                   filename += "_" + testName;
69              }
70          }
71          else
72          {
73              filename = description.getDisplayName();
74          }
75  
76          try
77          {
78              filename = URLEncoder.encode(filename, "UTF-8");
79          }
80          catch (UnsupportedEncodingException e)
81          {
82              //ignored
83          }
84          return filename + ".png";
85      }
86  }