1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.test.ui; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
|
24 |
|
import org.apache.commons.io.FileUtils; |
25 |
|
import org.junit.rules.TestWatcher; |
26 |
|
import org.junit.runner.Description; |
27 |
|
import org.openqa.selenium.OutputType; |
28 |
|
import org.openqa.selenium.TakesScreenshot; |
29 |
|
import org.openqa.selenium.WebDriver; |
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.slf4j.LoggerFactory; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@link |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@version |
51 |
|
@since |
52 |
|
|
|
|
| 82.4% |
Uncovered Elements: 6 (34) |
Complexity: 10 |
Complexity Density: 0.43 |
|
53 |
|
public class TestDebugger extends TestWatcher |
54 |
|
{ |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(TestDebugger.class); |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private static final String SCREENSHOT_DIR = System.getProperty("screenshotDirectory"); |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private final WebDriver driver; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
@param |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
577 |
public TestDebugger(WebDriver driver)... |
76 |
|
{ |
77 |
577 |
this.driver = driver; |
78 |
|
} |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
80 |
577 |
@Override... |
81 |
|
protected void starting(Description description) |
82 |
|
{ |
83 |
577 |
LOGGER.info("{} started", getTestName(description)); |
84 |
|
} |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
86 |
567 |
@Override... |
87 |
|
protected void succeeded(Description description) |
88 |
|
{ |
89 |
567 |
LOGGER.info("{} passed", getTestName(description)); |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
92 |
10 |
@Override... |
93 |
|
protected void failed(Throwable e, Description description) |
94 |
|
{ |
95 |
10 |
LOGGER.info("{} failed", getTestName(description)); |
96 |
10 |
takeScreenshot(description); |
97 |
10 |
LOGGER.info("Current page URL is [{}]", driver.getCurrentUrl()); |
98 |
10 |
LOGGER.info("Current page source is [{}]", driver.getPageSource()); |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
@param |
103 |
|
@return |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
1164 |
private String getTestName(Description description)... |
106 |
|
{ |
107 |
1164 |
return description.getTestClass().getSimpleName() + "-" + description.getMethodName(); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
@param |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
115 |
10 |
private void takeScreenshot(Description description)... |
116 |
|
{ |
117 |
10 |
takeScreenshot(getTestName(description)); |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
@param |
124 |
|
|
|
|
| 66.7% |
Uncovered Elements: 6 (18) |
Complexity: 4 |
Complexity Density: 0.29 |
|
125 |
10 |
public void takeScreenshot(String testName)... |
126 |
|
{ |
127 |
10 |
if (!(driver instanceof TakesScreenshot)) { |
128 |
0 |
LOGGER.warn("The WebDriver that is currently used doesn't support taking screenshots."); |
129 |
0 |
return; |
130 |
|
} |
131 |
|
|
132 |
10 |
try { |
133 |
10 |
File sourceFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); |
134 |
10 |
File screenshotFile; |
135 |
10 |
if (SCREENSHOT_DIR != null) { |
136 |
10 |
File screenshotDir = new File(SCREENSHOT_DIR); |
137 |
10 |
screenshotDir.mkdirs(); |
138 |
10 |
screenshotFile = new File(screenshotDir, testName + ".png"); |
139 |
|
} else { |
140 |
0 |
screenshotFile = new File(new File(System.getProperty("java.io.tmpdir")), testName + ".png"); |
141 |
|
} |
142 |
10 |
FileUtils.copyFile(sourceFile, screenshotFile); |
143 |
10 |
LOGGER.info("Screenshot for failing test [{}] saved at [{}].", testName, screenshotFile.getAbsolutePath()); |
144 |
|
} catch (Exception e) { |
145 |
0 |
LOGGER.error("Failed to take screenshot for failing test [{}].", testName, e); |
146 |
|
} |
147 |
|
} |
148 |
|
} |