|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+package se.citerus.dddsample.acceptance;
|
|
|
2
|
+
|
|
|
3
|
+import org.apache.commons.io.FileUtils;
|
|
|
4
|
+import org.junit.rules.MethodRule;
|
|
|
5
|
+import org.junit.runners.model.FrameworkMethod;
|
|
|
6
|
+import org.junit.runners.model.Statement;
|
|
|
7
|
+import org.openqa.selenium.OutputType;
|
|
|
8
|
+import org.openqa.selenium.TakesScreenshot;
|
|
|
9
|
+import org.openqa.selenium.WebDriver;
|
|
|
10
|
+
|
|
|
11
|
+import java.io.File;
|
|
|
12
|
+
|
|
|
13
|
+class ScreenshotTestRule implements MethodRule {
|
|
|
14
|
+ private WebDriver driver;
|
|
|
15
|
+
|
|
|
16
|
+ public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
|
|
|
17
|
+ return new Statement() {
|
|
|
18
|
+ @Override
|
|
|
19
|
+ public void evaluate() throws Throwable {
|
|
|
20
|
+ try {
|
|
|
21
|
+ statement.evaluate();
|
|
|
22
|
+ } catch (Throwable t) {
|
|
|
23
|
+ captureScreenshot(frameworkMethod.getName());
|
|
|
24
|
+ throw t;
|
|
|
25
|
+ }
|
|
|
26
|
+ }
|
|
|
27
|
+
|
|
|
28
|
+ public void captureScreenshot(String fileName) {
|
|
|
29
|
+ try {
|
|
|
30
|
+ new File("target/surefire-reports/").mkdirs(); // Insure directory is there
|
|
|
31
|
+ File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
|
|
|
32
|
+ FileUtils.copyFile(scrFile, new File("target/surefire-reports/screenshot-" + fileName + ".png"));
|
|
|
33
|
+ } catch (Exception e) {
|
|
|
34
|
+ System.err.println("ERROR: " + e);
|
|
|
35
|
+ }
|
|
|
36
|
+ }
|
|
|
37
|
+ };
|
|
|
38
|
+ }
|
|
|
39
|
+
|
|
|
40
|
+ public void updateDriver(WebDriver driver) {
|
|
|
41
|
+ this.driver = driver;
|
|
|
42
|
+ }
|
|
|
43
|
+}
|