41
41
import org .openqa .selenium .testing .Ignore ;
42
42
import org .openqa .selenium .testing .JUnit4TestBase ;
43
43
import org .openqa .selenium .testing .JavascriptEnabled ;
44
- import org .openqa .selenium .testing .NeedsFreshDriver ;
45
44
import org .openqa .selenium .testing .NoDriverAfterTest ;
46
45
import org .openqa .selenium .testing .NotYetImplemented ;
47
46
import org .openqa .selenium .testing .SwitchToTopAfterTest ;
53
52
public class WindowSwitchingTest extends JUnit4TestBase {
54
53
55
54
@ SwitchToTopAfterTest
55
+ @ NoDriverAfterTest (failedOnly = true )
56
56
@ Test
57
57
public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOperations () {
58
58
assumeFalse (Browser .detect () == Browser .opera &&
@@ -96,6 +96,7 @@ public void testShouldThrowNoSuchWindowException() {
96
96
driver .switchTo ().window (current );
97
97
}
98
98
99
+ @ NoDriverAfterTest (failedOnly = true )
99
100
@ Ignore ({MARIONETTE })
100
101
@ Test
101
102
public void testShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle () {
@@ -120,6 +121,7 @@ public void testShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle() {
120
121
}
121
122
}
122
123
124
+ @ NoDriverAfterTest (failedOnly = true )
123
125
@ Ignore ({MARIONETTE })
124
126
@ Test
125
127
public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed () {
@@ -153,6 +155,7 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed(
153
155
}
154
156
}
155
157
158
+ @ NoDriverAfterTest (failedOnly = true )
156
159
@ Ignore ({MARIONETTE })
157
160
@ Test
158
161
public void testShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIsClosed () {
@@ -178,7 +181,6 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIs
178
181
}
179
182
}
180
183
181
- @ NeedsFreshDriver
182
184
@ NoDriverAfterTest
183
185
@ Ignore ({IE , REMOTE })
184
186
@ Test
@@ -313,11 +315,12 @@ public void testFailingToSwitchToAWindowLeavesTheCurrentWindowAsIs() {
313
315
assertEquals (current , newHandle );
314
316
}
315
317
316
- @ NeedsFreshDriver
317
- @ NoDriverAfterTest
318
+ @ NoDriverAfterTest (failedOnly = true )
318
319
@ Test
319
320
public void testCanCloseWindowWhenMultipleWindowsAreOpen () {
320
321
driver .get (pages .xhtmlTestPage );
322
+ String mainHandle = driver .getWindowHandle ();
323
+
321
324
Set <String > currentWindowHandles = driver .getWindowHandles ();
322
325
323
326
driver .findElement (By .name ("windowOne" )).click ();
@@ -328,19 +331,27 @@ public void testCanCloseWindowWhenMultipleWindowsAreOpen() {
328
331
329
332
// There should be two windows. We should also see each of the window titles at least once.
330
333
assertEquals (2 , allWindowHandles .size ());
331
- String handle1 = (String ) allWindowHandles .toArray ()[1 ];
332
- driver .switchTo ().window (handle1 );
333
- driver .close ();
334
- allWindowHandles = driver .getWindowHandles ();
335
- assertEquals (1 , allWindowHandles .size ());
334
+
335
+ for (String handle : allWindowHandles ) {
336
+ if (! handle .equals (mainHandle )) {
337
+ driver .switchTo ().window (handle );
338
+ driver .close ();
339
+ break ;
340
+ }
341
+ }
342
+
343
+ assertEquals (1 , driver .getWindowHandles ().size ());
344
+
345
+ driver .switchTo ().window (mainHandle );
336
346
}
337
347
338
- @ NeedsFreshDriver
339
- @ NoDriverAfterTest
348
+ @ NoDriverAfterTest (failedOnly = true )
340
349
@ Test
341
350
public void testCanCloseWindowAndSwitchBackToMainWindow () {
342
351
driver .get (pages .xhtmlTestPage );
352
+
343
353
Set <String > currentWindowHandles = driver .getWindowHandles ();
354
+ String mainHandle = driver .getWindowHandle ();
344
355
345
356
driver .findElement (By .name ("windowOne" )).click ();
346
357
@@ -350,26 +361,31 @@ public void testCanCloseWindowAndSwitchBackToMainWindow() {
350
361
351
362
// There should be two windows. We should also see each of the window titles at least once.
352
363
assertEquals (2 , allWindowHandles .size ());
353
- String mainHandle = (String ) allWindowHandles .toArray ()[0 ];
354
- String handle1 = (String ) allWindowHandles .toArray ()[1 ];
355
- driver .switchTo ().window (handle1 );
356
- driver .close ();
364
+
365
+ for (String handle : allWindowHandles ) {
366
+ if (! handle .equals (mainHandle )) {
367
+ driver .switchTo ().window (handle );
368
+ driver .close ();
369
+ break ;
370
+ }
371
+ }
372
+
357
373
driver .switchTo ().window (mainHandle );
358
374
359
375
String newHandle = driver .getWindowHandle ();
360
376
assertEquals (mainHandle , newHandle );
377
+
378
+ assertEquals (1 , driver .getWindowHandles ().size ());
361
379
}
362
380
363
- @ NeedsFreshDriver
364
381
@ NoDriverAfterTest
365
382
@ Test
366
383
public void testClosingOnlyWindowShouldNotCauseTheBrowserToHang () {
367
384
driver .get (pages .xhtmlTestPage );
368
385
driver .close ();
369
386
}
370
387
371
- @ NeedsFreshDriver
372
- @ NoDriverAfterTest
388
+ @ NoDriverAfterTest (failedOnly = true )
373
389
@ Test
374
390
@ Ignore (MARIONETTE )
375
391
public void testShouldFocusOnTheTopMostFrameAfterSwitchingToAWindow () {
0 commit comments