This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
TestEventForScreenshotListener.java
112 lines (96 loc) · 3.53 KB
/
TestEventForScreenshotListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.zanata.util;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.AbstractWebDriverEventListener;
/**
* @author Damian Jansen <a
* href="mailto:djansen@redhat.com">djansen@redhat.com</a>
*/
@Slf4j
public class TestEventForScreenshotListener extends AbstractWebDriverEventListener {
private WebDriver driver;
private String targetDir;
private String testId = "";
/**
* A registered TestEventListener will perform actions on navigate,
* click and exception events
* @param drv the WebDriver to derive screen shots from
* @param targetDirectory full path to screen shot storage
*/
public TestEventForScreenshotListener(WebDriver drv, String targetDirectory) {
driver = drv;
targetDir =
targetDirectory.endsWith("/") ? targetDirectory
: targetDirectory.concat("/");
TestEventForScreenshotListener.log.info("Writing screenshots to {}", targetDir);
}
/**
* Update the screen shot directory/filename test ID component
* @param testId test identifier string
*/
public void updateTestID(String testId) {
this.testId = testId;
}
private void createScreenshot(String ofType) {
String outputDir;
try {
outputDir = targetDir.concat(testId);
File testIDDir = new File(outputDir);
testIDDir.mkdirs();
File screenshotFile =
((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File(outputDir
+ File.separator + generateFileName(ofType)));
} catch (WebDriverException wde) {
throw new RuntimeException("[Screenshot]: Invalid WebDriver: "
+ wde.getMessage());
} catch (IOException ioe) {
throw new RuntimeException("[Screenshot]: Failed to write to "
+ targetDir);
} catch (NullPointerException npe) {
throw new RuntimeException("[Screenshot]: Null Object: "
+ npe.getMessage());
}
}
private String generateFileName(String ofType) {
return testId.concat(":").concat(String.valueOf(new Date().getTime()))
.concat(ofType).concat(".png");
}
private boolean isAlertPresent(WebDriver driver) {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException nape) {
return false;
}
}
@Override
public void afterNavigateTo(String url, WebDriver driver) {
createScreenshot("_nav");
}
@Override
public void beforeClickOn(WebElement element, WebDriver driver) {
createScreenshot("_preclick");
}
@Override
public void afterClickOn(WebElement element, WebDriver driver) {
if (isAlertPresent(driver)) {
TestEventForScreenshotListener.log.info("[Screenshot]: Prevented by Alert");
return;
}
createScreenshot("_click");
}
@Override
public void onException(Throwable throwable, WebDriver driver) {
createScreenshot("_exc");
}
}