Aydar Omurbekov
Aydar Omurbekov

Reputation: 2117

Devise/Capybara Ambiguous match

I am using devise to create a sign up wizard, but capybara(2.0.2) raises

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 "[email protected]"
    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."

step definition is

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

Upvotes: 9

Views: 10073

Answers (2)

Anoel
Anoel

Reputation: 1063

With Capybara 2.1, this works:

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

From here :prefer_exact is the behaviour present in Capybara 1.x. If multiple matches are found, some of which are exact, and some of which are not, then the first eaxctly matching element is returned.

Upvotes: 66

Andrei Botalov
Andrei Botalov

Reputation: 21096

In version 2.0 Capybara's find method raises a Capybara::Ambiguous exception when several elements matching specified locator where found. Capybara doesn't want to make an ambiguous choice for you.

The proper solution is to use another locator (e.g. find('#id').set('password') or fill_in('field_name', with: 'password')

Read "Ambiguous Matches" section of Capybara 2.0 Upgrade guide for a bit longer explanation of the same.

Upvotes: 8

Related Questions