20
20
import static org .hamcrest .Matchers .equalTo ;
21
21
import static org .hamcrest .Matchers .instanceOf ;
22
22
import static org .junit .Assert .assertEquals ;
23
- import static org .junit .Assert .assertFalse ;
24
23
import static org .junit .Assert .assertNotNull ;
25
24
import static org .junit .Assert .assertThat ;
26
25
import static org .junit .Assume .assumeFalse ;
34
33
import static org .openqa .selenium .testing .Driver .REMOTE ;
35
34
import static org .openqa .selenium .testing .TestUtilities .catchThrowable ;
36
35
37
- import com .google .common .collect .Sets ;
38
-
36
+ import org .junit .Rule ;
39
37
import org .junit .Test ;
38
+ import org .junit .rules .TestRule ;
39
+ import org .junit .rules .TestWatcher ;
40
+ import org .junit .runner .Description ;
40
41
import org .openqa .selenium .support .ui .ExpectedConditions ;
41
42
import org .openqa .selenium .testing .Ignore ;
42
43
import org .openqa .selenium .testing .JUnit4TestBase ;
47
48
import org .openqa .selenium .testing .drivers .Browser ;
48
49
49
50
import java .util .Set ;
51
+ import java .util .stream .Collectors ;
50
52
51
53
public class WindowSwitchingTest extends JUnit4TestBase {
52
54
55
+ @ Rule
56
+ public final TestRule switchToMainWindow = new TestWatcher () {
57
+ private String mainWindow ;
58
+
59
+ @ Override
60
+ protected void starting (Description description ) {
61
+ super .starting (description );
62
+ mainWindow = driver .getWindowHandle ();
63
+ }
64
+
65
+ @ Override
66
+ protected void finished (Description description ) {
67
+ try {
68
+ if (! mainWindow .equals (driver .getWindowHandle ())) {
69
+ driver .close ();
70
+ }
71
+ } catch (Exception ignore ) {
72
+ }
73
+ driver .switchTo ().window (mainWindow );
74
+ super .finished (description );
75
+ }
76
+ };
77
+
53
78
@ SwitchToTopAfterTest
54
79
@ NoDriverAfterTest (failedOnly = true )
55
80
@ Test
@@ -58,7 +83,6 @@ public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOpera
58
83
TestUtilities .getEffectivePlatform ().is (Platform .WINDOWS ));
59
84
60
85
driver .get (pages .xhtmlTestPage );
61
- String current = driver .getWindowHandle ();
62
86
Set <String > currentWindowHandles = driver .getWindowHandles ();
63
87
64
88
driver .findElement (By .linkText ("Open new window" )).click ();
@@ -75,30 +99,20 @@ public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOpera
75
99
driver .findElement (By .id ("iframe_page_heading" ));
76
100
driver .switchTo ().frame ("iframe1" );
77
101
assertThat (driver .getWindowHandle (), equalTo (handle ));
78
-
79
- driver .close ();
80
- driver .switchTo ().window (current );
81
102
}
82
103
83
104
@ Test
84
105
public void testShouldThrowNoSuchWindowException () {
85
106
driver .get (pages .xhtmlTestPage );
86
- String current = driver .getWindowHandle ();
87
-
88
- try {
89
- Throwable t = catchThrowable (() -> driver .switchTo ().window ("invalid name" ));
90
- assertThat (t , instanceOf (NoSuchWindowException .class ));
91
- } finally {
92
- driver .switchTo ().window (current );
93
- }
107
+ Throwable t = catchThrowable (() -> driver .switchTo ().window ("invalid name" ));
108
+ assertThat (t , instanceOf (NoSuchWindowException .class ));
94
109
}
95
110
96
111
@ NoDriverAfterTest (failedOnly = true )
97
112
@ Ignore ({MARIONETTE })
98
113
@ Test
99
114
public void testShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle () {
100
115
driver .get (pages .xhtmlTestPage );
101
- String current = driver .getWindowHandle ();
102
116
Set <String > currentWindowHandles = driver .getWindowHandles ();
103
117
104
118
driver .findElement (By .linkText ("Open new window" )).click ();
@@ -108,20 +122,15 @@ public void testShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle() {
108
122
driver .switchTo ().window ("result" );
109
123
driver .close ();
110
124
111
- try {
112
- Throwable t = catchThrowable (driver ::getWindowHandle );
113
- assertThat (t , instanceOf (NoSuchWindowException .class ));
114
- } finally {
115
- driver .switchTo ().window (current );
116
- }
125
+ Throwable t = catchThrowable (driver ::getWindowHandle );
126
+ assertThat (t , instanceOf (NoSuchWindowException .class ));
117
127
}
118
128
119
129
@ NoDriverAfterTest (failedOnly = true )
120
130
@ Ignore ({MARIONETTE })
121
131
@ Test
122
132
public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed () {
123
133
driver .get (pages .xhtmlTestPage );
124
- String current = driver .getWindowHandle ();
125
134
Set <String > currentWindowHandles = driver .getWindowHandles ();
126
135
127
136
driver .findElement (By .linkText ("Open new window" )).click ();
@@ -131,23 +140,18 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed(
131
140
driver .switchTo ().window ("result" );
132
141
driver .close ();
133
142
134
- try {
135
- Throwable t = catchThrowable (driver ::getTitle );
136
- assertThat (t , instanceOf (NoSuchWindowException .class ));
143
+ Throwable t = catchThrowable (driver ::getTitle );
144
+ assertThat (t , instanceOf (NoSuchWindowException .class ));
137
145
138
- Throwable t2 = catchThrowable (() -> driver .findElement (By .tagName ("body" )));
139
- assertThat (t2 , instanceOf (NoSuchWindowException .class ));
140
- } finally {
141
- driver .switchTo ().window (current );
142
- }
146
+ Throwable t2 = catchThrowable (() -> driver .findElement (By .tagName ("body" )));
147
+ assertThat (t2 , instanceOf (NoSuchWindowException .class ));
143
148
}
144
149
145
150
@ NoDriverAfterTest (failedOnly = true )
146
151
@ Ignore ({MARIONETTE })
147
152
@ Test
148
153
public void testShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIsClosed () {
149
154
driver .get (pages .xhtmlTestPage );
150
- String current = driver .getWindowHandle ();
151
155
Set <String > currentWindowHandles = driver .getWindowHandles ();
152
156
153
157
driver .findElement (By .linkText ("Open new window" )).click ();
@@ -158,12 +162,8 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIs
158
162
WebElement body = driver .findElement (By .tagName ("body" ));
159
163
driver .close ();
160
164
161
- try {
162
- Throwable t = catchThrowable (body ::getText );
163
- assertThat (t , instanceOf (NoSuchWindowException .class ));
164
- } finally {
165
- driver .switchTo ().window (current );
166
- }
165
+ Throwable t = catchThrowable (body ::getText );
166
+ assertThat (t , instanceOf (NoSuchWindowException .class ));
167
167
}
168
168
169
169
@ NoDriverAfterTest
@@ -181,14 +181,13 @@ public void testShouldBeAbleToIterateOverAllOpenWindows() {
181
181
Set <String > allWindowHandles = driver .getWindowHandles ();
182
182
183
183
// There should be three windows. We should also see each of the window titles at least once.
184
- Set <String > seenHandles = Sets .newHashSet ();
185
- for (String handle : allWindowHandles ) {
186
- assertFalse (seenHandles .contains (handle ));
184
+ Set <String > allWindowTitles = allWindowHandles .stream ().map (handle -> {
187
185
driver .switchTo ().window (handle );
188
- seenHandles . add ( handle );
189
- }
186
+ return driver . getTitle ( );
187
+ }). collect ( Collectors . toSet ());
190
188
191
189
assertEquals (3 , allWindowHandles .size ());
190
+ assertEquals (3 , allWindowTitles .size ());
192
191
}
193
192
194
193
@ JavascriptEnabled
@@ -202,7 +201,6 @@ public void testClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToH
202
201
driver .get (pages .xhtmlTestPage );
203
202
Boolean isIEDriver = TestUtilities .isInternetExplorer (driver );
204
203
Boolean isIE6 = TestUtilities .isIe6 (driver );
205
- String currentHandle = driver .getWindowHandle ();
206
204
Set <String > currentWindowHandles = driver .getWindowHandles ();
207
205
208
206
driver .findElement (By .name ("windowThree" )).click ();
@@ -215,20 +213,15 @@ public void testClickingOnAButtonThatClosesAnOpenWindowDoesNotCauseTheBrowserToH
215
213
if (TestUtilities .isChrome (driver ) && TestUtilities .getEffectivePlatform (driver ).is (ANDROID )) {
216
214
Thread .sleep (1000 );
217
215
}
218
- try {
219
- wait .until (ExpectedConditions .presenceOfElementLocated (By .id ("close" )));
220
- driver .findElement (By .id ("close" )).click ();
221
216
222
- if (isIEDriver && !isIE6 ) {
223
- Alert alert = wait .until (alertIsPresent ());
224
- alert .accept ();
225
- }
217
+ wait .until (ExpectedConditions .presenceOfElementLocated (By .id ("close" ))).click ();
226
218
227
- // If we make it this far, we're all good.
228
- } finally {
229
- driver .switchTo ().window (currentHandle );
230
- driver .findElement (By .id ("linkId" ));
219
+ if (isIEDriver && !isIE6 ) {
220
+ Alert alert = wait .until (alertIsPresent ());
221
+ alert .accept ();
231
222
}
223
+
224
+ // If we make it this far, we're all good.
232
225
}
233
226
234
227
@ JavascriptEnabled
@@ -242,7 +235,6 @@ public void testCanCallGetWindowHandlesAfterClosingAWindow() throws Exception {
242
235
243
236
Boolean isIEDriver = TestUtilities .isInternetExplorer (driver );
244
237
Boolean isIE6 = TestUtilities .isIe6 (driver );
245
- String currentHandle = driver .getWindowHandle ();
246
238
Set <String > currentWindowHandles = driver .getWindowHandles ();
247
239
248
240
driver .findElement (By .name ("windowThree" )).click ();
@@ -256,29 +248,23 @@ public void testCanCallGetWindowHandlesAfterClosingAWindow() throws Exception {
256
248
if (TestUtilities .isChrome (driver ) && TestUtilities .getEffectivePlatform (driver ).is (ANDROID )) {
257
249
Thread .sleep (1000 );
258
250
}
259
- try {
260
- wait .until (ExpectedConditions .presenceOfElementLocated (By .id ("close" ))).click ();
261
-
262
- if (isIEDriver && !isIE6 ) {
263
- Alert alert = wait .until (alertIsPresent ());
264
- alert .accept ();
265
- }
266
251
267
- Set < String > allHandles = wait .until (windowHandleCountToBe ( allWindowHandles - 1 ) );
252
+ wait .until (ExpectedConditions . presenceOfElementLocated ( By . id ( "close" ))). click ( );
268
253
269
- assertEquals ( currentWindowHandles . size (), allHandles . size ());
270
- } finally {
271
- driver . switchTo (). window ( currentHandle );
254
+ if ( isIEDriver && ! isIE6 ) {
255
+ Alert alert = wait . until ( alertIsPresent ());
256
+ alert . accept ( );
272
257
}
258
+
259
+ Set <String > allHandles = wait .until (windowHandleCountToBe (allWindowHandles - 1 ));
260
+
261
+ assertEquals (currentWindowHandles .size (), allHandles .size ());
273
262
}
274
263
275
264
@ Test
276
265
public void testCanObtainAWindowHandle () {
277
266
driver .get (pages .xhtmlTestPage );
278
-
279
- String currentHandle = driver .getWindowHandle ();
280
-
281
- assertNotNull (currentHandle );
267
+ assertNotNull (driver .getWindowHandle ());
282
268
}
283
269
284
270
@ Test
@@ -310,17 +296,12 @@ public void testCanCloseWindowWhenMultipleWindowsAreOpen() {
310
296
// There should be two windows. We should also see each of the window titles at least once.
311
297
assertEquals (2 , allWindowHandles .size ());
312
298
313
- for (String handle : allWindowHandles ) {
314
- if (! handle .equals (mainHandle )) {
315
- driver .switchTo ().window (handle );
316
- driver .close ();
317
- break ;
318
- }
319
- }
299
+ allWindowHandles .stream ().filter (anObject -> ! mainHandle .equals (anObject )).forEach (handle -> {
300
+ driver .switchTo ().window (handle );
301
+ driver .close ();
302
+ });
320
303
321
304
assertEquals (1 , driver .getWindowHandles ().size ());
322
-
323
- driver .switchTo ().window (mainHandle );
324
305
}
325
306
326
307
@ NoDriverAfterTest (failedOnly = true )
@@ -340,13 +321,10 @@ public void testCanCloseWindowAndSwitchBackToMainWindow() {
340
321
// There should be two windows. We should also see each of the window titles at least once.
341
322
assertEquals (2 , allWindowHandles .size ());
342
323
343
- for (String handle : allWindowHandles ) {
344
- if (! handle .equals (mainHandle )) {
345
- driver .switchTo ().window (handle );
346
- driver .close ();
347
- break ;
348
- }
349
- }
324
+ allWindowHandles .stream ().filter (anObject -> ! mainHandle .equals (anObject )).forEach (handle -> {
325
+ driver .switchTo ().window (handle );
326
+ driver .close ();
327
+ });
350
328
351
329
driver .switchTo ().window (mainHandle );
352
330
0 commit comments