如何在水豚中点击表单提交按钮

问题描述 投票:0回答:3
ruby-on-rails rspec capybara
3个回答
19
投票

最后,一个宏就是我需要的答案,因为显然水豚中没有任何原生的东西。

# spec/support/form_helpers.rb
module FormHelpers
  def submit_form
    find('input[name="commit"]').click
  end
end

这包含在spec_helper.rb中

RSpec.configure do |config|
  config.include FormHelpers, :type => :feature
  ...etc...

我使用了

:type => :feature
,因此它仅包含在集成测试中。

在集成测试中你可以这样使用它:

scenario 'pages can be created' do
  visit new_page_path
  fill_in 'page_title', with: 'A Tale of Two Cities'
  submit_form # Clicks the commit button regardless of id or text

  expect(page).to have_content 'The page was created'
  ...etc..
end

当然

submit_form
也可以在
within
块内使用并与
:js => true
一起使用。


11
投票

我通常这样做:

within 'form#edit_page_1' do
  find('input[name="page[title]"]').set "Some Value" 
  find('input[name="commit"]').click
end

它与 html 相关,但与它的语义属性相关,所以我觉得它很好。 事实上我从来不使用神奇的取景器。

顺便说一句,我不明白你的评论:

(a user would never know/care about accessing an element that way)

集成规范适合您,它肯定会模仿用户,但这只是提供正确说明的问题。


1
投票

尝试使用:

find('input[name="commit"]').click

有帮助

© www.soinside.com 2019 - 2024. All rights reserved.