Devise/Capybara模棱两可的比赛

Ayd*_*kov 9 ruby-on-rails cucumber devise capybara

我正在使用devise来创建一个注册向导,但是capybara(2.0.2)会加注

Feature: Signing up
    In order to be attributed for my work
    As a user
    I want to be able to sign u

Scenario: Signing up
    Given I am on the homepage
    When I follow "Sign up"
    And I fill in "Email" with "user@ticketee.com"
    And I fill in "Password" with "password"
    Ambiguous match, found 2 elements matching field "Password" (Capybara::Ambiguous)
./features/step_definitions/web_steps.rb:10:in `/^(?:|I )fill in "([^"]*)" with "([^"]*)"$/'
features/signing_up.feature:10:in `And I fill in "Password" with "password"'
    And I fill in "Password confirmation" with "password"
    And I press "Sign up"
    Then I should see "You have signed up successfully."
Run Code Online (Sandbox Code Playgroud)

步骤定义是

When /^(?:|I )fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
  fill_in(field, :with => value)
end
Run Code Online (Sandbox Code Playgroud)

Ano*_*oel 65

使用Capybara 2.1,这有效:

  fill_in("Password", with: '123456', :match => :prefer_exact)
  fill_in("Password confirmation", with: '123456', :match => :prefer_exact)
Run Code Online (Sandbox Code Playgroud)

这里:prefer_exact是Capybara 1.x中存在的行为.如果找到多个匹配,其中一些是精确的,而其中一些不匹配,则返回第一个eaxctly匹配元素.

  • fill_in("密码",带:'123456',:match =>:first)也是一个很好的 (6认同)

And*_*lov 8

在版本2.0中,Capybara的find方法Capybara::Ambiguous在找到匹配指定定位器的多个元素时引发异常.Capybara不想为你做出暧昧的选择.

正确的解决方案是使用另一个定位器(例如find('#id').set('password')fill_in('field_name', with: 'password')

阅读Capybara 2.0升级指南的 "不明确的匹配"部分,以获得更长的解释.