9

我正在使用设计创建注册向导,但水豚(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."

步骤定义是

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

2 回答 2

66

使用 Capybara 2.1,这有效:

  fill_in("Password", with: '123456', :match => :prefer_exact)
  fill_in("Password confirmation", with: '123456', :match => :prefer_exact)

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

于 2013-06-05T00:27:40.837 回答
8

在 2.0 版中,当找到多个与指定定位器匹配的元素时,Capybara 的find方法会引发异常。Capybara::AmbiguousCapybara 不想为你做出模棱两可的选择。

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

阅读Capybara 2.0 升级指南的“模糊匹配”部分以获得更长的解释。

于 2013-02-19T07:25:43.717 回答