|
35 | 35 | import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
|
36 | 36 | import static org.openqa.selenium.testing.TestUtilities.isInternetExplorer;
|
37 | 37 |
|
| 38 | +import org.junit.After; |
| 39 | +import org.junit.Before; |
38 | 40 | import org.junit.Rule;
|
39 | 41 | import org.junit.Test;
|
| 42 | +import org.junit.rules.RuleChain; |
40 | 43 | import org.junit.rules.TestRule;
|
41 | 44 | import org.junit.rules.TestWatcher;
|
42 | 45 | import org.junit.runner.Description;
|
|
53 | 56 |
|
54 | 57 | public class WindowSwitchingTest extends JUnit4TestBase {
|
55 | 58 |
|
56 |
| - @Rule |
57 |
| - public final TestRule switchToMainWindow = new TestWatcher() { |
58 |
| - private String mainWindow; |
| 59 | + private String mainWindow; |
59 | 60 |
|
60 |
| - @Override |
61 |
| - protected void starting(Description description) { |
62 |
| - super.starting(description); |
63 |
| - mainWindow = driver.getWindowHandle(); |
64 |
| - } |
| 61 | + @Before |
| 62 | + public void storeMainWindowHandle() { |
| 63 | + mainWindow = driver.getWindowHandle(); |
| 64 | + } |
65 | 65 |
|
66 |
| - @Override |
67 |
| - protected void finished(Description description) { |
68 |
| - try { |
69 |
| - driver.getWindowHandles().stream().filter(handle -> ! mainWindow.equals(handle)) |
70 |
| - .forEach(handle -> driver.switchTo().window(handle).close()); |
71 |
| - } catch (Exception ignore) { |
72 |
| - System.err.println("Ignoring: " + ignore.getMessage()); |
73 |
| - } |
74 |
| - try { |
75 |
| - driver.switchTo().window(mainWindow); |
76 |
| - } catch (Exception ignore) { |
77 |
| - } |
78 |
| - super.finished(description); |
| 66 | + @After |
| 67 | + public void closeAllWindowsExceptForTheMainOne() { |
| 68 | + try { |
| 69 | + driver.getWindowHandles().stream().filter(handle -> ! mainWindow.equals(handle)) |
| 70 | + .forEach(handle -> driver.switchTo().window(handle).close()); |
| 71 | + } catch (Exception ignore) { |
| 72 | + System.err.println("Ignoring: " + ignore.getMessage()); |
79 | 73 | }
|
80 |
| - }; |
| 74 | + try { |
| 75 | + driver.switchTo().window(mainWindow); |
| 76 | + } catch (Exception ignore) { |
| 77 | + System.err.println("Ignoring: " + ignore.getMessage()); |
| 78 | + } |
| 79 | + } |
81 | 80 |
|
82 | 81 | @SwitchToTopAfterTest
|
83 | 82 | @NoDriverAfterTest(failedOnly = true)
|
@@ -109,7 +108,7 @@ public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOpera
|
109 | 108 | public void testShouldThrowNoSuchWindowException() {
|
110 | 109 | driver.get(pages.xhtmlTestPage);
|
111 | 110 | assertThatExceptionOfType(NoSuchWindowException.class)
|
112 |
| - .isThrownBy(() -> driver.switchTo().window("invalid name")); |
| 111 | + .isThrownBy(() -> driver.switchTo().window("invalid name")); |
113 | 112 | }
|
114 | 113 |
|
115 | 114 | @NoDriverAfterTest(failedOnly = true)
|
@@ -145,7 +144,7 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed(
|
145 | 144 | assertThatExceptionOfType(NoSuchWindowException.class).isThrownBy(driver::getTitle);
|
146 | 145 |
|
147 | 146 | assertThatExceptionOfType(NoSuchWindowException.class)
|
148 |
| - .isThrownBy(() -> driver.findElement(By.tagName("body"))); |
| 147 | + .isThrownBy(() -> driver.findElement(By.tagName("body"))); |
149 | 148 | }
|
150 | 149 |
|
151 | 150 | @NoDriverAfterTest(failedOnly = true)
|
@@ -257,7 +256,7 @@ public void testFailingToSwitchToAWindowLeavesTheCurrentWindowAsIs() {
|
257 | 256 | String current = driver.getWindowHandle();
|
258 | 257 |
|
259 | 258 | assertThatExceptionOfType(NoSuchWindowException.class)
|
260 |
| - .isThrownBy(() -> driver.switchTo().window("i will never exist")); |
| 259 | + .isThrownBy(() -> driver.switchTo().window("i will never exist")); |
261 | 260 |
|
262 | 261 | String newHandle = driver.getWindowHandle();
|
263 | 262 | assertThat(newHandle).isEqualTo(current);
|
|
0 commit comments