diff --git a/spec/features/element_interactions_spec.cr b/spec/features/element_interactions_spec.cr
index b63f7ec..830bb20 100644
--- a/spec/features/element_interactions_spec.cr
+++ b/spec/features/element_interactions_spec.cr
@@ -16,6 +16,39 @@ module Selenium::Command
end
end
+ it "can submit inside a form element" do
+ TestServer.route "/form", <<-HTML
+
+ HTML
+ TestServer.route "/post", "
You made it!
"
+
+ with_session do |session|
+ session.navigate_to("http://localhost:3002/form")
+ element = session.find_element(:name, "q")
+ element.send_keys("foobar")
+ element.submit
+ session.current_url.should eq("http://localhost:3002/post?q=foobar")
+ end
+ end
+
+ it "raise an error on submit the element which is not inside a form" do
+ TestServer.route "/form", <<-HTML
+
+ HTML
+ TestServer.route "/post", "
You made it!
"
+
+ with_session do |session|
+ session.navigate_to("http://localhost:3002/form")
+ element = session.find_element(:name, "q")
+ element.send_keys("foobar")
+ expect_raises Selenium::UnsupportedOperationError, "To submit an element, it must be nested inside a form element" do
+ element.submit
+ end
+ end
+ end
+
it "can clear" do
TestServer.route "/home", <<-HTML
diff --git a/src/selenium/element.cr b/src/selenium/element.cr
index 58fa541..abadeee 100644
--- a/src/selenium/element.cr
+++ b/src/selenium/element.cr
@@ -3,6 +3,8 @@ class Selenium::Element
getter session_id : SessionId
getter id : ElementId
+ ELEMENT_KEY = "element-6066-11e4-a52e-4f735466cecf"
+
def initialize(@command_handler, @session_id, @id)
end
@@ -10,6 +12,31 @@ class Selenium::Element
command_handler.execute(:element_click, path_variables)
end
+ def submit
+ script = <<-JS
+ /* submitForm */ var form = arguments[0];
+ while (form.nodeName != "FORM" && form.parentNode) {
+ form = form.parentNode;
+ }
+ if (!form) { throw Error('Unable to find containing form element'); }
+ if (!form.ownerDocument) { throw Error('Unable to find owning document'); }
+ var e = form.ownerDocument.createEvent('Event');
+ e.initEvent('submit', true, true);
+ if (form.dispatchEvent(e)) { HTMLFormElement.prototype.submit.call(form) }
+ JS
+
+ args = [{ELEMENT_KEY => id}]
+
+ parameters = {
+ script: script,
+ args: args,
+ }
+
+ command_handler.execute(:execute_script, path_variables, parameters)
+ rescue Selenium::Error
+ raise UnsupportedOperationError.new("To submit an element, it must be nested inside a form element")
+ end
+
def clear
command_handler.execute(:element_clear, path_variables)
end