Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber, capybara and selenium - Submitting a form without a button

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?

like image 340
Daniel Cukier Avatar asked May 09 '10 13:05

Daniel Cukier


3 Answers

You can access the selenium send_keys method to invoke a return event like

 find_field('field2').native.send_key(:enter) 
like image 167
Brian Dunn Avatar answered Sep 30 '22 12:09

Brian Dunn


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.

like image 34
Hakan Ensari Avatar answered Sep 30 '22 13:09

Hakan Ensari


I just had to solve this problem myself. In webrat I had something like this:

Then /^I submit the "([^\"]*)" form$/ do |form_id|
  submit_form form_id
end

I was able to achieve the same thing with this in Capybara:

  Then /^I submit the "([^\"]*)" form$/ do |form_id|
    element = find_by_id(form_id)
    Capybara::RackTest::Form.new(page.driver, element.native).submit :name => nil
  end
like image 23
siannopollo Avatar answered Sep 30 '22 13:09

siannopollo