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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
package selenium;
import com.google.common.base.*;
import com.google.common.collect.FluentIterable;
import org.openqa.selenium.WebElement;
import javax.annotation.Nullable;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.regex.Pattern;
import static selenium.Text.plural;
import static selenium.WebElementHelper.text;
class LazyShould {
private final LazyDomElement element;
private final Retry retry;
private final boolean ok;
LazyShould(LazyDomElement element, Retry retry, boolean ok) {
this.element = element;
this.retry = retry;
this.ok = ok;
}
public LazyShould beDisplayed() {
return verify(
isOrNot("displayed"),
new Predicate<List<WebElement>>() {
@Override
public boolean apply(List<WebElement> elements) {
return !elements.isEmpty() && FluentIterable.from(elements).allMatch(new Predicate<WebElement>() {
@Override
public boolean apply(WebElement element) {
return element.isDisplayed();
}
});
}
},
new Function<List<WebElement>, String>() {
@Override
public String apply(List<WebElement> elements) {
return "It is " + statuses(elements, new Function<WebElement, String>() {
@Override
public String apply(WebElement element) {
return displayedStatus(element);
}
});
}
});
}
public LazyShould match(final Pattern regexp) {
return verify(
doesOrNot("match") + " (" + regexp.pattern() + ")",
new Predicate<List<WebElement>>() {
@Override
public boolean apply(List<WebElement> elements) {
return !elements.isEmpty() && FluentIterable.from(elements).anyMatch(new Predicate<WebElement>() {
@Override
public boolean apply(WebElement element) {
return regexp.matcher(text(element)).matches();
}
});
}
},
new Function<List<WebElement>, String>() {
@Override
public String apply(List<WebElement> elements) {
return "It contains " + statuses(elements, new Function<WebElement, String>() {
@Nullable
@Override
public String apply(@Nullable WebElement element) {
return text(element);
}
});
}
});
}
public LazyShould contain(final String text) {
return verify(
doesOrNot("contain(") + text + ")",
new Predicate<List<WebElement>>() {
@Override
public boolean apply(List<WebElement> elements) {
return FluentIterable.from(elements).anyMatch(new Predicate<WebElement>() {
@Override
public boolean apply(@Nullable WebElement element) {
if (text.startsWith("exact:")) {
return text(element).equals(text.substring(6));
}
return text(element).contains(text);
}
});
}
},
new Function<List<WebElement>, String>() {
@Override
public String apply(List<WebElement> elements) {
return "It contains " + statuses(elements, new Function<WebElement, String>() {
@Override
public String apply(WebElement element) {
return text(element);
}
});
}
});
}
public LazyShould exist() {
return verify(
doesOrNot("exist"),
new Predicate<List<WebElement>>() {
@Override
public boolean apply(List<WebElement> elements) {
return !elements.isEmpty();
}
},
new Function<List<WebElement>, String>() {
@Override
public String apply(List<WebElement> elements) {
return "It contains " + plural(elements.size(), "element");
}
});
}
private static String displayedStatus(WebElement element) {
return element.isDisplayed() ? "displayed" : "not displayed";
}
private LazyShould verify(String message, Predicate<List<WebElement>> predicate, Function<List<WebElement>, String> toErrorMessage) {
String verification = "verify that " + element + " " + message;
System.out.println(" -> " + verification);
try {
if (!retry.verify(new Supplier<List<WebElement>>() {
@Override
public List<WebElement> get() {
return LazyShould.this.findElements();
}
}, ok ? predicate : Predicates.not(predicate))) {
throw Failure.create("Failed to " + verification + ". " + toErrorMessage.apply(findElements()));
}
} catch (NoSuchElementException e) {
throw Failure.create("Element not found. Failed to " + verification);
}
return ok ? this : not();
}
private List<WebElement> findElements() {
return element.stream();
}
private static String statuses(List<WebElement> elements, Function<WebElement, String> toStatus) {
return "(" + FluentIterable.from(elements).transform(toStatus).join(Joiner.on(";")) + ")";
}
public LazyShould not() {
return new LazyShould(element, retry, !ok);
}
private String doesOrNot(String verb) {
return Text.doesOrNot(!ok, verb);
}
private String isOrNot(String state) {
return Text.isOrNot(!ok, state);
}
}
|