Skip to content

Commit

Permalink
[dotnet] update implementation for submitting forms
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed May 25, 2022
1 parent db61990 commit 495c3db
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
16 changes: 11 additions & 5 deletions dotnet/src/webdriver/WebElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,17 @@ public virtual void Submit()
}
else
{
IWebElement form = this.FindElement(By.XPath("./ancestor-or-self::form"));
this.driver.ExecuteScript(
"var e = arguments[0].ownerDocument.createEvent('Event');" +
"e.initEvent('submit', true, true);" +
"if (arguments[0].dispatchEvent(e)) { arguments[0].submit(); }", form);
String script = "var form = arguments[0];\n" +
"while (form.nodeName != \"FORM\" && form.parentNode) {\n" +
" form = form.parentNode;\n" +
"}\n" +
"if (!form) { throw Error('Unable to find containing form element'); }\n" +
"if (!form.ownerDocument) { throw Error('Unable to find owning document'); }\n" +
"var e = form.ownerDocument.createEvent('Event');\n" +
"e.initEvent('submit', true, true);\n" +
"if (form.dispatchEvent(e)) { HTMLFormElement.prototype.submit.call(form) }\n";

this.driver.ExecuteScript(script, this);
}
}

Expand Down
22 changes: 20 additions & 2 deletions dotnet/test/common/FormHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,28 @@ public void ShouldSubmitAFormWhenAnyElementWithinThatFormIsSubmitted()
}

[Test]
public void ShouldNotBeAbleToSubmitAFormThatDoesNotExist()
public void ShouldSubmitAFormWithIdSubmit()
{
driver.Url = formsPage;
Assert.That(() => driver.FindElement(By.Name("SearchableText")).Submit(), Throws.InstanceOf<NoSuchElementException>());
driver.FindElement(By.Id("submit")).Submit();
WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
Assert.AreEqual(driver.Title, "We Arrive Here");
}

[Test]
public void ShouldSubmitAFormWithNameSubmit()
{
driver.Url = formsPage;
driver.FindElement(By.Name("submit")).Submit();
WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
Assert.AreEqual(driver.Title, "We Arrive Here");
}

[Test]
public void ShouldNotBeAbleToSubmitAnInputOutsideAForm()
{
driver.Url = formsPage;
Assert.That(() => driver.FindElement(By.Name("SearchableText")).Submit(), Throws.InstanceOf<WebDriverException>());
}

[Test]
Expand Down

0 comments on commit 495c3db

Please sign in to comment.