views:

785

answers:

5

I have a test using Cucumber, capybara and selenium driver. This test should go to a form and submit it. The normal text would be

  Scenario: Fill form
    Given I am on the Form page
    When I fill in "field1" with "value1"
    And I fill in "field2" with "value2"
    And I press "OK"
    Then I should see "Form submited"

The problem is that I don't have the OK button in the form I need a way to do the "form.submit", without clicking any button or link - the same as happens when you press ENTER when you are in a form field using the browser.

I don't know how to tell capybara to submit a form. How can I do it?

A: 

Simply put: you can't.

Some browsers will not allow you to submit a form without a submit button at all (most notably Internet Explorer <= 6). So this kind of form is a bad idea to begin with. Add a submit button and position it off the screen with CSS.

jnicklas
display:none comes in mind. It's there, but it is'nt!
Robus
the problem is that I CAN'T add the button, because I'm testing a web site I can't change the source code.
Daniel Cukier
A: 

You may probably roll your own step (And I submit the form with the link "Ok", for example), and emulate the submit functionality yourself.

Here it is the javascript emulation dropped in Rails 3 to support "unobtrusive" (emphasis on the quotes) Javascript. The line

Capybara::Driver::RackTest::Form.new(driver, js_form(self[:href], emulated_method)).submit(self)

is probably the clue to answer your problem. The full code is here

Chubas
this will work for Selenium driver?
Daniel Cukier
I think it does. I'm using form submission with confirmation dialogs without problem in an application right now, and they work correctly.
Chubas
+1  A: 

A simple solution:

When /^I submit the form$/ do
  page.evaluate_script("document.forms[0].submit()")
end

Worked for me with capybara-envjs. Should work with selenium as well.

hakanensari
A: 

I'd recommend you add a submit button, then hide it with CSS. Then you can test the form submission, but still get the user behavior you want.

nakajima
A: 

With the capybara Selenium driver you can do something like this:

within(:xpath, "//form[@id='the_form']") do
  locate(:xpath, "//input[@name='the_input']").set(value)
  locate(:xpath, "//input[@name='the_input']").node.send_keys(:return)
end
Aaron Gibralter