-
Notifications
You must be signed in to change notification settings - Fork 564
/
CollectionCondition.java
139 lines (113 loc) · 4.47 KB
/
CollectionCondition.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
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
package com.codeborne.selenide;
import com.codeborne.selenide.collections.ExactTexts;
import com.codeborne.selenide.collections.ListSize;
import com.codeborne.selenide.collections.SizeGreaterThan;
import com.codeborne.selenide.collections.SizeGreaterThanOrEqual;
import com.codeborne.selenide.collections.SizeLessThan;
import com.codeborne.selenide.collections.SizeLessThanOrEqual;
import com.codeborne.selenide.collections.SizeNotEqual;
import com.codeborne.selenide.collections.Texts;
import com.codeborne.selenide.collections.TextsInAnyOrder;
import com.codeborne.selenide.impl.WebElementsCollection;
import com.google.common.base.Predicate;
import org.openqa.selenium.WebElement;
import java.util.List;
public abstract class CollectionCondition implements Predicate<List<WebElement>> {
public abstract void fail(WebElementsCollection collection, List<WebElement> elements, Exception lastError, long timeoutMs);
public static CollectionCondition empty = size(0);
/**
* Checks that collection has the given size
*/
public static CollectionCondition size(int expectedSize) {
return new ListSize(expectedSize);
}
public static CollectionCondition sizeGreaterThan(int expectedSize) {
return new SizeGreaterThan(expectedSize);
}
public static CollectionCondition sizeGreaterThanOrEqual(int expectedSize) {
return new SizeGreaterThanOrEqual(expectedSize);
}
public static CollectionCondition sizeLessThan(int expectedSize) {
return new SizeLessThan(expectedSize);
}
public static CollectionCondition sizeLessThanOrEqual(int size) {
return new SizeLessThanOrEqual(size);
}
public static CollectionCondition sizeNotEqual(int expectedSize) {
return new SizeNotEqual(expectedSize);
}
/**
* Checks that given collection has given texts (each collection element CONTAINS corresponding text)
* <p>
* <p>NB! Ignores multiple whitespaces between words</p>
*/
public static CollectionCondition texts(String... expectedTexts) {
return new Texts(expectedTexts);
}
/**
* Checks that given collection has given texts (each collection element CONTAINS corresponding text)
* <p>
* <p>NB! Ignores multiple whitespaces between words</p>
*/
public static CollectionCondition texts(List<String> expectedTexts) {
return new Texts(expectedTexts);
}
/**
* Checks that given collection has given texts in any order (each collection element CONTAINS corresponding text)
* <p>
* <p>NB! Ignores multiple whitespaces between words</p>
*/
public static CollectionCondition textsInAnyOrder(String... expectedTexts) {
return new TextsInAnyOrder(expectedTexts);
}
/**
* Checks that given collection has given texts in any order (each collection element CONTAINS corresponding text)
* <p>
* <p>NB! Ignores multiple whitespaces between words</p>
*/
public static CollectionCondition textsInAnyOrder(List<String> expectedTexts) {
return new TextsInAnyOrder(expectedTexts);
}
/**
* Checks that given collection has given texts (each collection element EQUALS TO corresponding text)
* <p>
* <p>NB! Ignores multiple whitespaces between words</p>
*/
public static CollectionCondition exactTexts(String... expectedTexts) {
return new ExactTexts(expectedTexts);
}
/**
* Checks that given collection has given texts (each collection element EQUALS TO corresponding text)
* <p>
* <p>NB! Ignores multiple whitespaces between words</p>
*/
public static CollectionCondition exactTexts(List<String> expectedTexts) {
return new ExactTexts(expectedTexts);
}
/**
* Should be used for explaining the reason of condition
*/
public CollectionCondition because(String message) {
return new ExplainedCollectionCondition(this, message);
}
private static class ExplainedCollectionCondition extends CollectionCondition {
private final CollectionCondition delegate;
private final String message;
private ExplainedCollectionCondition(CollectionCondition delegate, String message) {
this.delegate = delegate;
this.message = message;
}
@Override
public void fail(WebElementsCollection collection, List<WebElement> elements, Exception lastError, long timeoutMs) {
delegate.fail(collection, elements, lastError, timeoutMs);
}
@Override
public boolean apply(List<WebElement> element) {
return delegate.apply(element);
}
@Override
public String toString() {
return delegate.toString() + " (because " + message + ")";
}
}
}