Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions common/src/web/form_handling_js_submit.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<title>Form with JS action</title>
</head>
<body>
<form id="theForm" method="get" action="javascript:alert('Tasty cheese');">
<form id="theForm" method="get" action="click_tests/submitted_page.html" onsubmit="return alert('Tasty cheese')">
<input name="unused" type="submit">
</form>

<p id="result"></p>
</body>
</html>
</html>
9 changes: 7 additions & 2 deletions java/test/org/openqa/selenium/AlertsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -467,7 +468,9 @@ void shouldHandleAlertOnFormSubmit() {
new Page()
.withTitle("Testing Alerts")
.withBody(
"<form id='theForm' action='javascript:alert(\"Tasty cheese\");'>",
"<form id='theForm'"
+ " action='/click_tests/submitted_page.html' "
+ " onsubmit='return alert(\"Tasty cheese\");'>",
"<input id='unused' type='submit' value='Submit'>",
"</form>")));

Expand All @@ -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");
}
}
3 changes: 3 additions & 0 deletions java/test/org/openqa/selenium/FormHandlingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,10 +34,14 @@ public static TestEnvironment get() {
return environment;
}

public static synchronized TestEnvironment getOrCreate(
Supplier<TestEnvironment> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ default void assertIsValid() {

assertThat(hostName).isNotEqualTo(alternateHostName);
}

default boolean isSecure() {
return getAppServer().isSecure();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface AppServer {

String whereIsWithCredentials(String relativeUrl, String user, String password);

boolean isSecure();

String create(Page page);

void start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
45 changes: 26 additions & 19 deletions java/test/org/openqa/selenium/testing/JupiterTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,54 +50,42 @@ public abstract class JupiterTestBase {
protected AppServer appServer;
protected Pages pages;
protected WebDriver driver;
private String initialWindowHandle;
protected Wait<WebDriver> wait;
protected Wait<WebDriver> 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();
wait = seleniumExtension::waitUntil;
shortWait = seleniumExtension::shortWaitUntil;

if (driver != null) {
initialWindowHandle = driver.getWindowHandle();
driver.get("about:blank");
driver.get(pages.blankPage + "?test=" + seleniumExtension.currentTest());
driver.manage().deleteAllCookies();
}
}

@AfterEach
public void quitLocalDriver() {
final void quitLocalDriver() {
if (localDriver != null) {
try {
localDriver.quit();
Expand All @@ -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;
Expand Down