Skip to content

Commit bcc7618

Browse files
committed
[java] improve error message for unsuccessfully submitting form
1 parent 495c3db commit bcc7618

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

‎java/src/org/openqa/selenium/remote/RemoteWebElement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openqa.selenium.Beta;
2222
import org.openqa.selenium.By;
2323
import org.openqa.selenium.Dimension;
24+
import org.openqa.selenium.JavascriptException;
2425
import org.openqa.selenium.OutputType;
2526
import org.openqa.selenium.Point;
2627
import org.openqa.selenium.Rectangle;
@@ -79,7 +80,12 @@ public void click() {
7980

8081
@Override
8182
public void submit() {
82-
execute(DriverCommand.SUBMIT_ELEMENT(id));
83+
try {
84+
execute(DriverCommand.SUBMIT_ELEMENT(id));
85+
} catch (JavascriptException ex) {
86+
String message = "To submit an element, it must be nested inside a form element";
87+
throw new UnsupportedOperationException(message);
88+
}
8389
}
8490

8591
@Override

‎java/test/org/openqa/selenium/FormHandlingTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void testShouldSubmitAFormWhenAnyElementWithinThatFormIsSubmitted() {
8383
public void testShouldNotBeAbleToSubmitAFormThatDoesNotExist() {
8484
driver.get(pages.formPage);
8585
WebElement element = driver.findElement(By.name("SearchableText"));
86-
assertThatExceptionOfType(JavascriptException.class).isThrownBy(element::submit);
86+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(element::submit);
8787
}
8888

8989
@Test
@@ -282,14 +282,10 @@ public void testCanClickOnAnExternalImplicitSubmitButton() {
282282

283283
@Test
284284
public void canSubmitFormWithSubmitButtonIdEqualToSubmit() {
285-
String blank = appServer.create(new Page().withTitle("Submitted Successfully!"));
286-
driver.get(appServer.create(new Page().withBody(
287-
String.format("<form action='%s'>", blank),
288-
" <input type='submit' id='submit' value='Submit'>",
289-
"</form>")));
290-
291-
driver.findElement(By.id("submit")).submit();
292-
wait.until(titleIs("Submitted Successfully!"));
285+
driver.get(pages.formPage);
286+
driver.findElement(By.id("submit")).click();
287+
wait.until(titleIs("We Arrive Here"));
288+
assertThat(driver.getTitle()).isEqualTo("We Arrive Here");
293289
}
294290

295291
@Test

0 commit comments

Comments
 (0)