Repository navigation
[java] manage state in tests not in global hooks #18142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
titusfortner
merged 5 commits into
SeleniumHQ:trunk
from
titusfortner:java-test-base-cleanup
Oct 9, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6184d22
[java] let test classes own cookie and window cleanup instead of Jupi…
titusfortner ae67eed
[java] make tests that open windows close them through a CloseExtraWi…
titusfortner b89b104
[java] mark BiDi tests that switch into a context they close, run the…
titusfortner 085b73c
[java] reset the mouse pointer by viewport location, warn when window…
titusfortner 4ddf059
[java] wait for the new window's contextCreated event instead of taki…
titusfortner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1 @@ | ||
| <html lang="en"> | ||
| <head> | ||
| <title>blank</title> | ||
| <style> | ||
| body { | ||
| background: #f5f5f5; | ||
| background: linear-gradient( | ||
| 109deg, | ||
| rgba(245, 245, 245, 1) 0%, | ||
| rgba(225, 225, 225, 1) 35%, | ||
| rgba(205, 205, 205, 1) 100% | ||
| ); | ||
| } | ||
| body { | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <h1 id="header">A blank slate</h1> | ||
| <script> | ||
| localStorage.clear(); | ||
| sessionStorage.clear(); | ||
| const params = new URLSearchParams(window.location.search); | ||
| const test = params.get('test'); | ||
| document.getElementById('header').innerText = test; | ||
| </script> | ||
| </body> | ||
| </html> | ||
| <html><head><title>blank</title></head><body></body></html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Licensed to the Software Freedom Conservancy (SFC) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The SFC licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.openqa.selenium; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
|
|
||
| import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; | ||
| import org.junit.jupiter.api.Order; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.TestMethodOrder; | ||
| import org.openqa.selenium.testing.CloseExtraWindowsAfterTest; | ||
| import org.openqa.selenium.testing.JupiterTestBase; | ||
| import org.openqa.selenium.testing.NoDriverAfterTest; | ||
| import org.openqa.selenium.testing.SeleniumExtension; | ||
|
|
||
| @TestMethodOrder(OrderAnnotation.class) | ||
| @CloseExtraWindowsAfterTest | ||
| class CloseExtraWindowsTest extends JupiterTestBase { | ||
|
|
||
| private static String windowBeforeOpener; | ||
|
|
||
| @Test | ||
| @Order(1) | ||
| void leavesExtraWindowsOpenForTheAnnotationToClose() { | ||
| windowBeforeOpener = driver.getWindowHandle(); | ||
| driver.switchTo().newWindow(WindowType.TAB); | ||
| driver.switchTo().newWindow(WindowType.WINDOW); | ||
|
|
||
| assertThat(driver.getWindowHandles()).hasSize(3); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(2) | ||
| void startsOnTheInitialWindowWithNothingElseOpen() { | ||
| assumeTrue(windowBeforeOpener != null); | ||
|
|
||
| assertThat(driver.getWindowHandles()).containsExactly(windowBeforeOpener); | ||
| assertThat(driver.getWindowHandle()).isEqualTo(windowBeforeOpener); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(3) | ||
| void closesExtraWindowsAndRestoresTheInitialOne() { | ||
| String initial = driver.getWindowHandle(); | ||
| driver.switchTo().newWindow(WindowType.TAB); | ||
| driver.switchTo().newWindow(WindowType.WINDOW); | ||
|
|
||
| SeleniumExtension.closeExtraWindows(driver, initial); | ||
|
|
||
| assertThat(driver.getWindowHandles()).containsExactly(initial); | ||
| assertThat(driver.getWindowHandle()).isEqualTo(initial); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(4) | ||
| void restoresTheInitialWindowAfterTheFocusedOneWasClosed() { | ||
| String initial = driver.getWindowHandle(); | ||
| driver.switchTo().newWindow(WindowType.TAB); | ||
| driver.close(); | ||
|
|
||
| SeleniumExtension.closeExtraWindows(driver, initial); | ||
|
|
||
| assertThat(driver.getWindowHandles()).containsExactly(initial); | ||
| assertThat(driver.getWindowHandle()).isEqualTo(initial); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(5) | ||
| @NoDriverAfterTest | ||
| void keepsTheFirstRemainingWindowWhenTheInitialOneIsGone() { | ||
| String initial = driver.getWindowHandle(); | ||
| String replacement = driver.switchTo().newWindow(WindowType.TAB).getWindowHandle(); | ||
| driver.switchTo().window(initial).close(); | ||
|
|
||
| SeleniumExtension.closeExtraWindows(driver, initial); | ||
|
|
||
| assertThat(driver.getWindowHandles()).containsExactly(replacement); | ||
| assertThat(driver.getWindowHandle()).isEqualTo(replacement); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.