Skip to content

Commit e23236a

Browse files
committed
[java] Fixing WindowSwitchingTest local rule broken by the recently extracted global one
1 parent c41447f commit e23236a

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

java/client/test/org/openqa/selenium/WindowSwitchingTest.java

+24-25
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
3636
import static org.openqa.selenium.testing.TestUtilities.isInternetExplorer;
3737

38+
import org.junit.After;
39+
import org.junit.Before;
3840
import org.junit.Rule;
3941
import org.junit.Test;
42+
import org.junit.rules.RuleChain;
4043
import org.junit.rules.TestRule;
4144
import org.junit.rules.TestWatcher;
4245
import org.junit.runner.Description;
@@ -53,31 +56,27 @@
5356

5457
public class WindowSwitchingTest extends JUnit4TestBase {
5558

56-
@Rule
57-
public final TestRule switchToMainWindow = new TestWatcher() {
58-
private String mainWindow;
59+
private String mainWindow;
5960

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+
}
6565

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());
7973
}
80-
};
74+
try {
75+
driver.switchTo().window(mainWindow);
76+
} catch (Exception ignore) {
77+
System.err.println("Ignoring: " + ignore.getMessage());
78+
}
79+
}
8180

8281
@SwitchToTopAfterTest
8382
@NoDriverAfterTest(failedOnly = true)
@@ -109,7 +108,7 @@ public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOpera
109108
public void testShouldThrowNoSuchWindowException() {
110109
driver.get(pages.xhtmlTestPage);
111110
assertThatExceptionOfType(NoSuchWindowException.class)
112-
.isThrownBy(() -> driver.switchTo().window("invalid name"));
111+
.isThrownBy(() -> driver.switchTo().window("invalid name"));
113112
}
114113

115114
@NoDriverAfterTest(failedOnly = true)
@@ -145,7 +144,7 @@ public void testShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed(
145144
assertThatExceptionOfType(NoSuchWindowException.class).isThrownBy(driver::getTitle);
146145

147146
assertThatExceptionOfType(NoSuchWindowException.class)
148-
.isThrownBy(() -> driver.findElement(By.tagName("body")));
147+
.isThrownBy(() -> driver.findElement(By.tagName("body")));
149148
}
150149

151150
@NoDriverAfterTest(failedOnly = true)
@@ -257,7 +256,7 @@ public void testFailingToSwitchToAWindowLeavesTheCurrentWindowAsIs() {
257256
String current = driver.getWindowHandle();
258257

259258
assertThatExceptionOfType(NoSuchWindowException.class)
260-
.isThrownBy(() -> driver.switchTo().window("i will never exist"));
259+
.isThrownBy(() -> driver.switchTo().window("i will never exist"));
261260

262261
String newHandle = driver.getWindowHandle();
263262
assertThat(newHandle).isEqualTo(current);

0 commit comments

Comments
 (0)