View Javadoc

1   package com.atlassian.webdriver.debug;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.commons.io.IOUtils;
5   import org.openqa.selenium.OutputType;
6   import org.openqa.selenium.TakesScreenshot;
7   import org.openqa.selenium.WebDriver;
8   import org.openqa.selenium.WebDriverException;
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  
12  import javax.annotation.Nonnull;
13  import javax.inject.Inject;
14  import java.io.File;
15  import java.io.FileWriter;
16  import java.io.IOException;
17  import java.io.Writer;
18  
19  import static com.atlassian.webdriver.utils.WebDriverUtil.as;
20  import static com.atlassian.webdriver.utils.WebDriverUtil.isInstance;
21  import static com.google.common.base.Preconditions.checkNotNull;
22  
23  /**
24   * An injectable component for performing common debug operations using {@link WebDriver}.
25   *
26   * @since 2.2
27   */
28  public final class WebDriverDebug
29  {
30      private static final Logger log = LoggerFactory.getLogger(WebDriverDebug.class);
31  
32      private final WebDriver webDriver;
33  
34      @Inject
35      public WebDriverDebug(@Nonnull WebDriver webDriver)
36      {
37          this.webDriver = checkNotNull(webDriver, "webDriver");
38      }
39  
40  
41      /**
42       * Get the URL that the underlying web driver is currently at.
43       *
44       * @return current URL
45       */
46      @Nonnull
47      public String getCurrentUrl()
48      {
49          return webDriver.getCurrentUrl();
50      }
51  
52      /**
53       * <p/>
54       * Writes the source of the last loaded page to the specified file. See {@link #dumpPageSourceTo(java.io.Writer)}
55       * for details on how it works.
56       *
57       * <p/>
58       * As opposed to {@link #dumpPageSourceTo(java.io.Writer)}, this method makes sure to close the handle used
59       * to write to {@code dumpFile}.
60       *
61       * @param dumpFile File to write the source to.
62       *
63       */
64      public boolean dumpSourceTo(@Nonnull File dumpFile)
65      {
66          checkNotNull(dumpFile, "dumpFile");
67          FileWriter fileWriter = null;
68          try
69          {
70              fileWriter = new FileWriter(dumpFile);
71              IOUtils.write(webDriver.getPageSource(), fileWriter);
72              return true;
73          }
74          catch (IOException e)
75          {
76              log.warn("Error dumping page source to " + dumpFile.getAbsolutePath() + ": " + e.getMessage());
77              log.debug("Error dumping page source - details", e);
78              return false;
79          }
80          finally
81          {
82              IOUtils.closeQuietly(fileWriter);
83          }
84      }
85  
86  
87      /**
88       * <p/>
89       * Writes the source of the last loaded page to the specified writer. The writer is othewrwise not interacted with.
90       * In particular, clients have to take care to close the {@code writer}.
91       *
92       * <p/>
93       * This method will attempt to not propagate any exceptions that prevent it from completing. Instead, the write will
94       * be aborted, the exception logged and {@literal false} returned. The only exception raised by this method is
95       * {@link NullPointerException} in case {@code writer} is {@literal null}.
96       *
97       * @param writer writer to write the page source to
98       */
99      public boolean dumpPageSourceTo(@Nonnull Writer writer)
100     {
101         checkNotNull(writer, "writer");
102         try
103         {
104             IOUtils.write(webDriver.getPageSource(), writer);
105             return true;
106         }
107         catch (IOException e)
108         {
109             log.warn("Error dumping page source to " + writer + ": " + e.getMessage());
110             log.debug("Error dumping page source - details", e);
111             return false;
112         }
113     }
114 
115 
116     /**
117      * Saves screen shot of the browser to the specified file.
118      *
119      * @param destFile File to save screen shot.
120      */
121     public boolean takeScreenshotTo(@Nonnull File destFile)
122     {
123         checkNotNull(destFile, "destFile");
124         if (isInstance(webDriver, TakesScreenshot.class))
125         {
126             TakesScreenshot shotter = as(webDriver, TakesScreenshot.class);
127             log.info("Saving screenshot to: " + destFile.getAbsolutePath());
128             try
129             {
130                 File screenshot = shotter.getScreenshotAs(OutputType.FILE);
131                 FileUtils.copyFile(screenshot, destFile);
132                 return true;
133             }
134             catch (IOException e)
135             {
136                 logScreenshotError(destFile, e);
137             }
138             catch (WebDriverException e)
139             {
140                 logScreenshotError(destFile, e);
141             }
142         }
143         else
144         {
145             log.warn("Driver {} is not capable of taking screenshots.", webDriver);
146         }
147         return false;
148     }
149 
150     private void logScreenshotError(File destFile, Exception e)
151     {
152         log.warn("Could not capture screenshot to {}: {}", destFile, e.getMessage());
153         log.debug("Capture screenshot - error details", e);
154     }
155 }