View Javadoc

1   package com.atlassian.webdriver.testing.rule;
2   
3   import com.atlassian.webdriver.debug.WebDriverDebug;
4   import com.google.common.base.Supplier;
5   import org.junit.rules.TestWatcher;
6   import org.junit.runner.Description;
7   import org.openqa.selenium.WebDriver;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  import javax.annotation.Nonnull;
12  import javax.inject.Inject;
13  import java.io.File;
14  
15  import static com.google.common.base.Preconditions.checkNotNull;
16  import static com.google.common.base.Preconditions.checkState;
17  
18  /**
19   * A rule for taking screen-shots when a WebDriver test fails. It will also dump the html source of the page to
20   * the target/webDriverTests directory.
21   *
22   * @since 2.1
23   */
24  public class WebDriverScreenshotRule extends TestWatcher
25  {
26      private static final Logger log = LoggerFactory.getLogger(WebDriverScreenshotRule.class);
27  
28      private final WebDriverDebug debug;
29      private final File artifactDir;
30  
31      private static File defaultArtifactDir()
32      {
33          return new File("target/webdriverTests");
34      }
35  
36      protected WebDriverScreenshotRule(@Nonnull WebDriverDebug webDriverDebug, @Nonnull File artifactDir)
37      {
38          this.debug = checkNotNull(webDriverDebug, "webDriverDebug");
39          this.artifactDir = checkNotNull(artifactDir, "artifactDir");
40      }
41  
42      protected WebDriverScreenshotRule(@Nonnull WebDriverSupport<? extends WebDriver> support, @Nonnull File artifactDir)
43      {
44          this(new WebDriverDebug(checkNotNull(support, "support").getDriver()), artifactDir);
45      }
46  
47      public WebDriverScreenshotRule(@Nonnull Supplier<? extends WebDriver> driverSupplier, @Nonnull File artifactDir)
48      {
49          this(WebDriverSupport.forSupplier(driverSupplier), artifactDir);
50      }
51  
52      public WebDriverScreenshotRule(@Nonnull Supplier<? extends WebDriver> driverSupplier)
53      {
54          this(driverSupplier, defaultArtifactDir());
55      }
56  
57      public WebDriverScreenshotRule(@Nonnull WebDriver webDriver)
58      {
59          this(WebDriverSupport.forInstance(webDriver), defaultArtifactDir());
60      }
61  
62      @Inject
63      public WebDriverScreenshotRule(@Nonnull WebDriverDebug webDriverDebug)
64      {
65          this(webDriverDebug, defaultArtifactDir());
66      }
67  
68      public WebDriverScreenshotRule()
69      {
70          this(WebDriverSupport.fromAutoInstall(), defaultArtifactDir());
71      }
72  
73  
74  
75      @Override
76      protected void starting(@Nonnull final Description description)
77      {
78          File dir = getTargetDir(description);
79          if (!dir.exists())
80          {
81              checkState(dir.mkdirs(), "Unable to create screenshot output directory " + dir.getAbsolutePath());
82          }
83      }
84  
85      @Override
86      protected void failed(@Nonnull final Throwable e, @Nonnull final Description description)
87      {
88          final File dumpFile = getTargetFile(description, "html");
89          final File screenShotFile = getTargetFile(description, "png");
90          log.info("----- {} failed. ", description.getDisplayName());
91          log.info("----- At page: " + debug.getCurrentUrl());
92          log.info("----- Dumping page source to {} and screenshot to {}", dumpFile.getAbsolutePath(),
93                  screenShotFile.getAbsolutePath());
94          debug.dumpSourceTo(dumpFile);
95          debug.takeScreenshotTo(screenShotFile);
96      }
97  
98      private File getTargetDir(Description description)
99      {
100         return new File(artifactDir, description.getClassName());
101     }
102 
103     private File getTargetFile(Description description, String extension)
104     {
105         return new File(getTargetDir(description), description.getMethodName() + "." + extension);
106     }
107 
108 }