diff --git a/common/src/web/form_handling_js_submit.html b/common/src/web/form_handling_js_submit.html index 3023143929a64..34c7a3d6ae9ef 100644 --- a/common/src/web/form_handling_js_submit.html +++ b/common/src/web/form_handling_js_submit.html @@ -21,10 +21,10 @@ Form with JS action -
+

- \ No newline at end of file + diff --git a/java/test/org/openqa/selenium/AlertsTest.java b/java/test/org/openqa/selenium/AlertsTest.java index b0f92008ccb28..689964a0f5a72 100644 --- a/java/test/org/openqa/selenium/AlertsTest.java +++ b/java/test/org/openqa/selenium/AlertsTest.java @@ -23,6 +23,7 @@ import static org.openqa.selenium.WaitingConditions.newWindowIsOpened; import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent; import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated; +import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs; import static org.openqa.selenium.testing.drivers.Browser.CHROME; import static org.openqa.selenium.testing.drivers.Browser.EDGE; import static org.openqa.selenium.testing.drivers.Browser.FIREFOX; @@ -467,7 +468,9 @@ void shouldHandleAlertOnFormSubmit() { new Page() .withTitle("Testing Alerts") .withBody( - "
", + "", "", "
"))); @@ -477,6 +480,8 @@ void shouldHandleAlertOnFormSubmit() { alert.accept(); assertThat(value).isEqualTo("Tasty cheese"); - assertThat(driver.getTitle()).isEqualTo("Testing Alerts"); + + wait.until(titleIs("Submitted Successfully!")); + assertThat(driver.getCurrentUrl()).contains("submitted_page.html"); } } diff --git a/java/test/org/openqa/selenium/FormHandlingTest.java b/java/test/org/openqa/selenium/FormHandlingTest.java index efb66413ca3a0..d633382dd174b 100644 --- a/java/test/org/openqa/selenium/FormHandlingTest.java +++ b/java/test/org/openqa/selenium/FormHandlingTest.java @@ -22,6 +22,7 @@ import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent; import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated; import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs; +import static org.openqa.selenium.support.ui.ExpectedConditions.urlContains; import static org.openqa.selenium.testing.drivers.Browser.FIREFOX; import static org.openqa.selenium.testing.drivers.Browser.IE; import static org.openqa.selenium.testing.drivers.Browser.SAFARI; @@ -254,6 +255,8 @@ public void handleFormWithJavascriptAction() { alert.accept(); assertThat(text).isEqualTo("Tasty cheese"); + wait.until(titleIs("Submitted Successfully!")); + wait.until(urlContains("submitted_page.html")); } @Test diff --git a/java/test/org/openqa/selenium/environment/GlobalTestEnvironment.java b/java/test/org/openqa/selenium/environment/GlobalTestEnvironment.java index 0b4f4b7e33060..6f20bf073067c 100644 --- a/java/test/org/openqa/selenium/environment/GlobalTestEnvironment.java +++ b/java/test/org/openqa/selenium/environment/GlobalTestEnvironment.java @@ -17,10 +17,12 @@ package org.openqa.selenium.environment; -import java.util.function.Supplier; +import java.util.logging.Level; +import java.util.logging.Logger; /** Used to hold a TestEnvironment in a static class-level field. */ public class GlobalTestEnvironment { + private static final Logger LOG = Logger.getLogger(GlobalTestEnvironment.class.getName()); private static TestEnvironment environment; @@ -32,10 +34,14 @@ public static TestEnvironment get() { return environment; } - public static synchronized TestEnvironment getOrCreate( - Supplier startThisIfNothingIsAlreadyRunning) { + public static synchronized TestEnvironment getOrCreate(boolean needsSecureServer) { + if (needsSecureServer && environment != null && !environment.isSecure()) { + LOG.log(Level.WARNING, "Restarting appServer with secureServer=true"); + environment.stop(); + environment = null; + } if (environment == null) { - environment = startThisIfNothingIsAlreadyRunning.get(); + environment = new InProcessTestEnvironment(needsSecureServer); environment.assertIsValid(); } return environment; diff --git a/java/test/org/openqa/selenium/environment/TestEnvironment.java b/java/test/org/openqa/selenium/environment/TestEnvironment.java index 6f4e0b363e1b5..adcf48016eb70 100644 --- a/java/test/org/openqa/selenium/environment/TestEnvironment.java +++ b/java/test/org/openqa/selenium/environment/TestEnvironment.java @@ -33,4 +33,8 @@ default void assertIsValid() { assertThat(hostName).isNotEqualTo(alternateHostName); } + + default boolean isSecure() { + return getAppServer().isSecure(); + } } diff --git a/java/test/org/openqa/selenium/environment/webserver/AppServer.java b/java/test/org/openqa/selenium/environment/webserver/AppServer.java index d039c3652ae68..f3e8c8ac2cc08 100644 --- a/java/test/org/openqa/selenium/environment/webserver/AppServer.java +++ b/java/test/org/openqa/selenium/environment/webserver/AppServer.java @@ -34,6 +34,8 @@ public interface AppServer { String whereIsWithCredentials(String relativeUrl, String user, String password); + boolean isSecure(); + String create(Page page); void start(); diff --git a/java/test/org/openqa/selenium/environment/webserver/NettyAppServer.java b/java/test/org/openqa/selenium/environment/webserver/NettyAppServer.java index 858942258b941..d05aff62161bb 100644 --- a/java/test/org/openqa/selenium/environment/webserver/NettyAppServer.java +++ b/java/test/org/openqa/selenium/environment/webserver/NettyAppServer.java @@ -189,6 +189,10 @@ public String whereIsSecure(String relativeUrl) { return createUrl(secure, "https", getHostName(), relativeUrl); } + public boolean isSecure() { + return secure != null; + } + @Override public String whereIsWithCredentials(String relativeUrl, String user, String password) { return String.format( diff --git a/java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java b/java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java index cbb5f660b959a..ddf0bdbc2ac91 100644 --- a/java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java +++ b/java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java @@ -36,7 +36,6 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.build.InProject; import org.openqa.selenium.environment.GlobalTestEnvironment; -import org.openqa.selenium.environment.InProcessTestEnvironment; import org.openqa.selenium.environment.TestEnvironment; import org.openqa.selenium.environment.webserver.AppServer; import org.openqa.selenium.testing.drivers.WebDriverBuilder; @@ -61,8 +60,8 @@ private static boolean isBazel() { @BeforeEach public void setup() { - // this field is actually in use, javascript test do access it - testEnvironment = GlobalTestEnvironment.getOrCreate(() -> new InProcessTestEnvironment(true)); + // this field is actually in use, JavaScript test do access it + testEnvironment = GlobalTestEnvironment.getOrCreate(true); } @AfterEach diff --git a/java/test/org/openqa/selenium/testing/JupiterTestBase.java b/java/test/org/openqa/selenium/testing/JupiterTestBase.java index a53d8389b3173..acff502526094 100644 --- a/java/test/org/openqa/selenium/testing/JupiterTestBase.java +++ b/java/test/org/openqa/selenium/testing/JupiterTestBase.java @@ -31,9 +31,9 @@ import org.junit.jupiter.api.extension.RegisterExtension; import org.openqa.selenium.Capabilities; import org.openqa.selenium.NoSuchSessionException; +import org.openqa.selenium.NoSuchWindowException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.environment.GlobalTestEnvironment; -import org.openqa.selenium.environment.InProcessTestEnvironment; import org.openqa.selenium.environment.TestEnvironment; import org.openqa.selenium.environment.webserver.AppServer; import org.openqa.selenium.support.ui.Wait; @@ -50,39 +50,26 @@ public abstract class JupiterTestBase { protected AppServer appServer; protected Pages pages; protected WebDriver driver; + private String initialWindowHandle; protected Wait wait; protected Wait shortWait; protected WebDriver localDriver; @BeforeAll - public static void shouldTestBeRunAtAll() { + static void shouldTestBeRunAtAll() { assumeThat(Boolean.getBoolean("selenium.skiptest")).isFalse(); } @BeforeEach - public void prepareEnvironment() { + final void prepareEnvironment() { boolean needsSecureServer = Optional.ofNullable(this.getClass().getAnnotation(NeedsSecureServer.class)) .map(NeedsSecureServer::value) .orElse(false); - environment = - GlobalTestEnvironment.getOrCreate(() -> new InProcessTestEnvironment(needsSecureServer)); + environment = GlobalTestEnvironment.getOrCreate(needsSecureServer); appServer = environment.getAppServer(); - if (needsSecureServer) { - try { - appServer.whereIsSecure("/"); - } catch (IllegalStateException ex) { - // this should not happen with bazel, a new JVM is used for each class - // the annotation is on class level, so we should never see this - LOG.log(Level.WARNING, "appServer is restarted with secureServer=true", ex); - environment.stop(); - environment = new InProcessTestEnvironment(true); - appServer = environment.getAppServer(); - } - } - pages = new Pages(appServer); driver = seleniumExtension.getDriver(); @@ -90,6 +77,7 @@ public void prepareEnvironment() { shortWait = seleniumExtension::shortWaitUntil; if (driver != null) { + initialWindowHandle = driver.getWindowHandle(); driver.get("about:blank"); driver.get(pages.blankPage + "?test=" + seleniumExtension.currentTest()); driver.manage().deleteAllCookies(); @@ -97,7 +85,7 @@ public void prepareEnvironment() { } @AfterEach - public void quitLocalDriver() { + final void quitLocalDriver() { if (localDriver != null) { try { localDriver.quit(); @@ -110,6 +98,25 @@ public void quitLocalDriver() { } } + @AfterEach + final void switchToInitialWindow() { + if (driver == null) { + return; + } + + if (initialWindowHandle != null) { + try { + driver.switchTo().window(initialWindowHandle); + } catch (NoSuchWindowException | NoSuchSessionException ok) { + LOG.log( + Level.FINE, + String.format( + "The initial window has been closed in test %s: %s", + seleniumExtension.currentTest(), ok)); + } + } + } + public void createNewDriver(Capabilities capabilities) { driver = seleniumExtension.createNewDriver(capabilities); wait = seleniumExtension::waitUntil;