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
25
26
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
43
44
45
46 @Nonnull
47 public String getCurrentUrl()
48 {
49 return webDriver.getCurrentUrl();
50 }
51
52
53
54
55
56
57
58
59
60
61
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
89
90
91
92
93
94
95
96
97
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
118
119
120 public boolean takeScreenshotTo(@Nonnull File destFile)
121 {
122 if (isInstance(webDriver, TakesScreenshot.class))
123 {
124 TakesScreenshot shotter = as(webDriver, TakesScreenshot.class);
125 log.info("Saving screenshot to: " + destFile.getAbsolutePath());
126 try
127 {
128 File screenshot = shotter.getScreenshotAs(OutputType.FILE);
129 FileUtils.copyFile(screenshot, destFile);
130 return true;
131 }
132 catch (IOException e)
133 {
134 logScreenshotError(destFile, e);
135 }
136 catch (WebDriverException e)
137 {
138 logScreenshotError(destFile, e);
139 }
140 }
141 else
142 {
143 log.warn("Driver {} is not capable of taking screenshots.", webDriver);
144 }
145 return false;
146 }
147
148 private void logScreenshotError(File destFile, Exception e)
149 {
150 log.warn("Could not capture screenshot to {}: {}", destFile, e.getMessage());
151 log.debug("Capture screenshot - error details", e);
152 }
153 }