1 package io.wcm.qa.glnm.aspectj;
2
3 import static org.apache.commons.lang3.StringUtils.abbreviateMiddle;
4
5 import org.aspectj.lang.JoinPoint;
6 import org.aspectj.lang.annotation.AfterReturning;
7 import org.aspectj.lang.annotation.Aspect;
8 import org.aspectj.lang.annotation.Before;
9 import org.aspectj.lang.annotation.Pointcut;
10 import org.hamcrest.Matcher;
11 import org.hamcrest.StringDescription;
12 import org.openqa.selenium.TakesScreenshot;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import io.wcm.qa.glnm.reporting.GaleniumReportUtil;
17
18
19
20
21
22
23
24
25 @Aspect("perthis(execution(* *..*.matches(..)))")
26 public class MatcherAspect {
27
28 private static final Logger LOG = LoggerFactory.getLogger(MatcherAspect.class);
29 private String startStepUuid;
30
31
32
33
34
35
36
37 @AfterReturning(pointcut = "execution(boolean org.hamcrest.Matcher.matches(..))", returning = "result")
38 public void doneMatching(final JoinPoint joinPoint, boolean result) {
39 if (LOG.isTraceEnabled()) {
40 LOG.trace("done: " + joinPoint.getSignature().toLongString());
41 }
42 Matcher matcher = (Matcher)joinPoint.getTarget();
43 if (result) {
44 passStep(matcher);
45 }
46 else {
47 failStep(joinPoint, matcher);
48 }
49 }
50
51
52
53
54
55
56 @Pointcut("execution(boolean org.hamcrest.Matcher.matches(..))")
57 public void hamcrestMatcherMatch() {
58
59 }
60
61
62
63
64
65
66
67
68 @Before("execution(boolean org.hamcrest.Matcher.matches(..))")
69 public void startMatching(final JoinPoint joinPoint) {
70 if (LOG.isTraceEnabled()) {
71 LOG.trace("before: " + joinPoint.getSignature().toLongString());
72 }
73 startStepUuid = GaleniumReportUtil.startStep("matching");
74 }
75
76 private StringDescription descriptionFor(Matcher matcher) {
77 StringDescription description = new StringDescription();
78 description
79 .appendText("Expected: ")
80 .appendDescriptionOf(matcher);
81 return description;
82 }
83
84 private void failStep(final JoinPoint joinPoint, Matcher matcher) {
85 StringDescription description = descriptionFor(matcher);
86 description
87 .appendText(System.lineSeparator())
88 .appendText(" but: ");
89 Object matchedItem = joinPoint.getArgs()[0];
90 describeMismatch(matcher, description, matchedItem);
91 GaleniumReportUtil.updateStepName(startStepUuid, description.toString());
92 GaleniumReportUtil.failStep(startStepUuid);
93 screenshot(matchedItem);
94 GaleniumReportUtil.stopStep();
95 }
96
97 private void passStep(Matcher matcher) {
98 GaleniumReportUtil.updateStepName(startStepUuid, descriptionFor(matcher).toString());
99 GaleniumReportUtil.passStep(startStepUuid);
100 GaleniumReportUtil.stopStep();
101 }
102
103 private static void describeMismatch(Matcher matcher, StringDescription description, Object matchedItem) {
104 if (matchedItem != null
105 && matchedItem instanceof String) {
106 String shortMessage = abbreviateMiddle((String)matchedItem, "[...]", 200);
107 matcher.describeMismatch(shortMessage, description);
108 return;
109 }
110
111 matcher.describeMismatch(matchedItem, description);
112 }
113
114 private static void screenshot(Object matchedItem) {
115 if (matchedItem == null) {
116 return;
117 }
118 if (matchedItem instanceof TakesScreenshot) {
119 if (LOG.isDebugEnabled()) {
120 LOG.debug("taking screenshot of " + matchedItem);
121 }
122 GaleniumReportUtil.takeScreenshot("matcher-screenshot", (TakesScreenshot)matchedItem, false);
123 return;
124 }
125 }
126
127 }