3
votes

I have this code in my page

  <div>
    <label for="user_password">Heslo</label>
    <br />
    <input id="user_password" name="user[password]" size="30" type="password">
  </div>

  <div>
    <label for="user_password_confirmation">Heslo pro kontrolu</label>
    <br>
    <input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password">
  </div>

And I would like to use these steps in Cucumber:

And I fill in "Heslo" with "NeznameHeslo328"
And I fill in "Heslo pro konrolu" with "NeznameHeslo328"

Step definition is simple:

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

BUT Capybara raises Ambiguous match, found 2 elements matching field "Heslo" (Capybara::Ambiguous) for first step.

When I change name in label with for="user_password_confirmation" to "Hesla pro kontrolu" (and second step accordingly), everything is OK.

Is there any way to force Capybara to match exact phrase within label? Without using css selectors in scenario.

2

2 Answers

2
votes

Update

In Capybara 2.1 you can use :exact option:

fill_in(name, with: 'Heslo', exact: true)

This option is set to false by default


In previous versions of Capybara it's not possible to make an exact string matching. All matching is done using substrings. You can only use another selector type.

0
votes

This is what I've started using. It's working for me so far:

Given /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field_name, text|
  page.find(field_name).set "#{text}"
end