为什么我在 Cucumber/Capybara 步骤中会得到“undefined method 'click' for Cucumber::Rails::World”的错误?

12

我可以让其他Capybara方法,如visit、fill_in、select等正常运作。但是click无法正常工作。

Given /^a no fee license called "([^"]*)"$/ do |arg1|
  @l = FactoryGirl.create(:license,
                      name: arg1)
end

When /^execute the license$/ do
  visit(license_path(@l))
  click('Execute License')
end

错误:

Scenario: no fee license is executed                                 # features/visitor_no_fee_license.feature:14
    When I fill out the licensee info with valid info                  # features/step_definitions/licenses_steps.rb:21
    And execute the license                                            # features/step_definitions/licenses_steps.rb:35
      undefined method `click' for #<Cucumber::Rails::World:0x007feebad4dec8> (NoMethodError)
      ./features/step_definitions/licenses_steps.rb:37:in `/^execute the license$/'
      features/visitor_no_fee_license.feature:16:in `And execute the license'
2个回答

24

1

也许你想使用点击链接

When /^(?:|I )follow "([^"]*)"$/ do |link|
  click_link(link)
end

还有点击按钮的选项。

如果以上两种方法都不适合您的需求,那么您可以在元素上点击

When /^(?:|I )click "([^"]*)" span$/ do |selector|
  element ||= find('span', :text => selector)
  element.click
end

如果以上方法都无法帮助您,这里有一个所有点击方法的列表,如果您能识别出您想要使用的方法,我可能能够提供进一步的帮助。

http://rubydoc.info/search/github/jnicklas/capybara/master?q=click


不,我想要点击是因为它可以选择链接或按钮。但你的第二种方法很好,谢谢!我找到了答案,如下所示。 - Ivan -Oats- Storck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接