26
26
import static org .mockito .Mockito .when ;
27
27
28
28
import com .google .common .base .Function ;
29
+ import com .google .common .base .Supplier ;
29
30
30
31
import org .junit .Before ;
31
32
import org .junit .Test ;
42
43
import java .util .concurrent .TimeUnit ;
43
44
44
45
@ RunWith (JUnit4 .class )
45
- public class FluentWaitTest {
46
+ public class FluentWaitTest {
46
47
47
48
private static final Object ARBITRARY_VALUE = new Object ();
48
49
49
- @ Mock private WebDriver mockDriver ;
50
- @ Mock private ExpectedCondition <Object > mockCondition ;
51
- @ Mock private Clock mockClock ;
52
- @ Mock private Sleeper mockSleeper ;
50
+ @ Mock
51
+ private WebDriver mockDriver ;
52
+ @ Mock
53
+ private ExpectedCondition <Object > mockCondition ;
54
+ @ Mock
55
+ private Clock mockClock ;
56
+ @ Mock
57
+ private Sleeper mockSleeper ;
53
58
54
59
@ Before
55
60
public void createMocks () {
@@ -63,9 +68,9 @@ public void shouldWaitUntilReturnValueOfConditionIsNotNull() throws InterruptedE
63
68
when (mockCondition .apply (mockDriver )).thenReturn (null , ARBITRARY_VALUE );
64
69
65
70
Wait <WebDriver > wait = new FluentWait <WebDriver >(mockDriver , mockClock , mockSleeper )
66
- .withTimeout (0 , TimeUnit .MILLISECONDS )
67
- .pollingEvery (2 , TimeUnit .SECONDS )
68
- .ignoring (NoSuchElementException .class , NoSuchFrameException .class );
71
+ .withTimeout (0 , TimeUnit .MILLISECONDS )
72
+ .pollingEvery (2 , TimeUnit .SECONDS )
73
+ .ignoring (NoSuchElementException .class , NoSuchFrameException .class );
69
74
70
75
assertEquals (ARBITRARY_VALUE , wait .until (mockCondition ));
71
76
verify (mockSleeper , times (1 )).sleep (new Duration (2 , TimeUnit .SECONDS ));
@@ -78,9 +83,9 @@ public void shouldWaitUntilABooleanResultIsTrue() throws InterruptedException {
78
83
when (mockCondition .apply (mockDriver )).thenReturn (false , false , true );
79
84
80
85
Wait <WebDriver > wait = new FluentWait <WebDriver >(mockDriver , mockClock , mockSleeper )
81
- .withTimeout (0 , TimeUnit .MILLISECONDS )
82
- .pollingEvery (2 , TimeUnit .SECONDS )
83
- .ignoring (NoSuchElementException .class , NoSuchFrameException .class );
86
+ .withTimeout (0 , TimeUnit .MILLISECONDS )
87
+ .pollingEvery (2 , TimeUnit .SECONDS )
88
+ .ignoring (NoSuchElementException .class , NoSuchFrameException .class );
84
89
85
90
assertEquals (true , wait .until (mockCondition ));
86
91
@@ -94,7 +99,7 @@ public void checksTimeoutAfterConditionSoZeroTimeoutWaitsCanSucceed() {
94
99
when (mockCondition .apply (mockDriver )).thenReturn (null );
95
100
96
101
Wait <WebDriver > wait = new FluentWait <WebDriver >(mockDriver , mockClock , mockSleeper )
97
- .withTimeout (0 , TimeUnit .MILLISECONDS );
102
+ .withTimeout (0 , TimeUnit .MILLISECONDS );
98
103
try {
99
104
wait .until (mockCondition );
100
105
fail ();
@@ -108,14 +113,14 @@ public void canIgnoreMultipleExceptions() throws InterruptedException {
108
113
when (mockClock .laterBy (0L )).thenReturn (2L );
109
114
when (mockClock .isNowBefore (2L )).thenReturn (true );
110
115
when (mockCondition .apply (mockDriver ))
111
- .thenThrow (new NoSuchElementException ("" ))
112
- .thenThrow (new NoSuchFrameException ("" ))
113
- .thenReturn (ARBITRARY_VALUE );
116
+ .thenThrow (new NoSuchElementException ("" ))
117
+ .thenThrow (new NoSuchFrameException ("" ))
118
+ .thenReturn (ARBITRARY_VALUE );
114
119
115
120
Wait <WebDriver > wait = new FluentWait <WebDriver >(mockDriver , mockClock , mockSleeper )
116
- .withTimeout (0 , TimeUnit .MILLISECONDS )
117
- .pollingEvery (2 , TimeUnit .SECONDS )
118
- .ignoring (NoSuchElementException .class , NoSuchFrameException .class );
121
+ .withTimeout (0 , TimeUnit .MILLISECONDS )
122
+ .pollingEvery (2 , TimeUnit .SECONDS )
123
+ .ignoring (NoSuchElementException .class , NoSuchFrameException .class );
119
124
120
125
assertEquals (ARBITRARY_VALUE , wait .until (mockCondition ));
121
126
@@ -130,9 +135,9 @@ public void propagatesUnIgnoredExceptions() {
130
135
when (mockCondition .apply (mockDriver )).thenThrow (exception );
131
136
132
137
Wait <WebDriver > wait = new FluentWait <WebDriver >(mockDriver , mockClock , mockSleeper )
133
- .withTimeout (0 , TimeUnit .MILLISECONDS )
134
- .pollingEvery (2 , TimeUnit .SECONDS )
135
- .ignoring (NoSuchElementException .class , NoSuchFrameException .class );
138
+ .withTimeout (0 , TimeUnit .MILLISECONDS )
139
+ .pollingEvery (2 , TimeUnit .SECONDS )
140
+ .ignoring (NoSuchElementException .class , NoSuchFrameException .class );
136
141
137
142
try {
138
143
wait .until (mockCondition );
@@ -148,14 +153,14 @@ public void timeoutMessageIncludesLastIgnoredException() {
148
153
149
154
when (mockClock .laterBy (0L )).thenReturn (2L );
150
155
when (mockCondition .apply (mockDriver ))
151
- .thenThrow (exception )
152
- .thenReturn (null );
156
+ .thenThrow (exception )
157
+ .thenReturn (null );
153
158
when (mockClock .isNowBefore (2L )).thenReturn (false );
154
159
155
160
Wait <WebDriver > wait = new FluentWait <WebDriver >(mockDriver , mockClock , mockSleeper )
156
- .withTimeout (0 , TimeUnit .MILLISECONDS )
157
- .pollingEvery (2 , TimeUnit .SECONDS )
158
- .ignoring (NoSuchWindowException .class );
161
+ .withTimeout (0 , TimeUnit .MILLISECONDS )
162
+ .pollingEvery (2 , TimeUnit .SECONDS )
163
+ .ignoring (NoSuchWindowException .class );
159
164
try {
160
165
wait .until (mockCondition );
161
166
fail ();
@@ -167,15 +172,45 @@ public void timeoutMessageIncludesLastIgnoredException() {
167
172
@ Test
168
173
public void timeoutMessageIncludesCustomMessage () {
169
174
TimeoutException expected = new TimeoutException (
170
- "Timed out after 0 seconds: Expected custom timeout message" );
175
+ "Timed out after 0 seconds: Expected custom timeout message" );
171
176
172
177
when (mockClock .laterBy (0L )).thenReturn (2L );
173
178
when (mockCondition .apply (mockDriver )).thenReturn (null );
174
179
when (mockClock .isNowBefore (2L )).thenReturn (false );
175
180
176
181
Wait <WebDriver > wait = new FluentWait <WebDriver >(mockDriver , mockClock , mockSleeper )
177
- .withTimeout (0 , TimeUnit .MILLISECONDS )
178
- .withMessage ("Expected custom timeout message" );
182
+ .withTimeout (0 , TimeUnit .MILLISECONDS )
183
+ .withMessage ("Expected custom timeout message" );
184
+
185
+ try {
186
+ wait .until (mockCondition );
187
+ fail ();
188
+ } catch (TimeoutException actual ) {
189
+ assertEquals (expected .getMessage (), actual .getMessage ());
190
+ }
191
+ }
192
+
193
+ private String state = null ;
194
+
195
+ @ Test
196
+ public void timeoutMessageIncludesCustomMessageEvaluatedOnFailure () {
197
+ TimeoutException expected = new TimeoutException (
198
+ "Timed out after 0 seconds: external state" );
199
+
200
+ when (mockClock .laterBy (0L )).thenReturn (2L );
201
+ when (mockCondition .apply (mockDriver )).thenReturn (null );
202
+ when (mockClock .isNowBefore (2L )).thenReturn (false );
203
+
204
+ Wait <WebDriver > wait = new FluentWait <WebDriver >(mockDriver , mockClock , mockSleeper )
205
+ .withTimeout (0 , TimeUnit .MILLISECONDS )
206
+ .withMessage (new Supplier <String >() {
207
+ @ Override
208
+ public String get () {
209
+ return state ;
210
+ }
211
+ });
212
+
213
+ state = "external state" ;
179
214
180
215
try {
181
216
wait .until (mockCondition );
@@ -188,7 +223,7 @@ public void timeoutMessageIncludesCustomMessage() {
188
223
@ Test
189
224
public void timeoutMessageIncludesToStringOfCondition () {
190
225
TimeoutException expected = new TimeoutException (
191
- "Timed out after 0 seconds waiting for toString called" );
226
+ "Timed out after 0 seconds waiting for toString called" );
192
227
193
228
Function <Object , Boolean > condition = new Function <Object , Boolean >() {
194
229
public Boolean apply (Object ignored ) {
@@ -202,7 +237,7 @@ public String toString() {
202
237
};
203
238
204
239
Wait <Object > wait = new FluentWait <Object >("cheese" )
205
- .withTimeout (0 , TimeUnit .MILLISECONDS );
240
+ .withTimeout (0 , TimeUnit .MILLISECONDS );
206
241
207
242
try {
208
243
wait .until (condition );
@@ -221,9 +256,9 @@ public void canIgnoreThrowables() {
221
256
when (mockClock .isNowBefore (2L )).thenReturn (false );
222
257
223
258
Wait <WebDriver > wait = new FluentWait <WebDriver >(mockDriver , mockClock , mockSleeper )
224
- .withTimeout (0 , TimeUnit .MILLISECONDS )
225
- .pollingEvery (2 , TimeUnit .SECONDS )
226
- .ignoring (AssertionError .class );
259
+ .withTimeout (0 , TimeUnit .MILLISECONDS )
260
+ .pollingEvery (2 , TimeUnit .SECONDS )
261
+ .ignoring (AssertionError .class );
227
262
228
263
try {
229
264
wait .until (mockCondition );
@@ -248,8 +283,8 @@ protected RuntimeException timeoutException(String message, Throwable lastExcept
248
283
}
249
284
};
250
285
wait .withTimeout (0 , TimeUnit .MILLISECONDS )
251
- .pollingEvery (2 , TimeUnit .SECONDS )
252
- .ignoring (TimeoutException .class );
286
+ .pollingEvery (2 , TimeUnit .SECONDS )
287
+ .ignoring (TimeoutException .class );
253
288
254
289
try {
255
290
wait .until (mockCondition );
@@ -259,8 +294,11 @@ protected RuntimeException timeoutException(String message, Throwable lastExcept
259
294
}
260
295
}
261
296
262
- private static class TestException extends RuntimeException {}
297
+ private static class TestException extends RuntimeException {
298
+
299
+ }
263
300
264
301
public interface GenericCondition extends ExpectedCondition <Object > {
302
+
265
303
}
266
304
}
0 commit comments