Cucumber, capybara and selenium - Submitting a for

2019-03-09 18:42发布

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?

12条回答
闹够了就滚
2楼-- · 2019-03-09 19:09

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
查看更多
做自己的国王
3楼-- · 2019-03-09 19:10

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

 find_field('field2').native.send_key(:enter)
查看更多
Fickle 薄情
4楼-- · 2019-03-09 19:12

This is a bit hackish, but it filled a need. I monkey-patched Capybara to support a #submit method on elements.

It is not robust because it naively creates the POST parameters from every input elements's name and value attributes. (In my case, all my <input> elements were of type hidden, so it works fine).

class Capybara::Node::Element
  # If self is a form element, submit the form by building a
  # parameters from all 'input' tags within this form.
  def submit
    raise "Can only submit form, not #{tag_name}" unless tag_name =~ /form/i

    method = self['method'].to_sym
    url = self['action']
    params = all(:css, 'input').reduce({}) do |acc, input|
      acc.store(input['name'], input['value'])
      acc
    end

    session.driver.submit(method, url, params)
  end
end

...

form = find('#my_form_with_no_submit_button')
form.submit
查看更多
不美不萌又怎样
5楼-- · 2019-03-09 19:14

You can try sending a newline:

find_field('field2').native.send_key("\n")
查看更多
一纸荒年 Trace。
6楼-- · 2019-03-09 19:17

The display:none solution does not work with capybara using selenium driver because selenium complains about interacting with invisible elements. If you try the above solution you may end up seeing the following error message:

Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotVisibleError)
查看更多
迷人小祖宗
7楼-- · 2019-03-09 19:20

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

查看更多
登录 后发表回答