Rahul
Rahul

Reputation: 1876

capybara's fill_in method not working as expected

I have 2 forms in my page. The first form has a field "Username" and the second form has a field "Username:".

When fill_in label, :with => value is run (where label = "Username:"), the input box labeled "Username" is getting filled instead.

I changed "Username:" to "User name:" but even then "Username" gets filled.

What am I doing wrong?

Upvotes: 2

Views: 4963

Answers (1)

Jon M
Jon M

Reputation: 11705

If I understand correctly, you have 2 identically named text inputs on 2 different forms on your page.

I believe fill_in some_field will look for an input with a name or ID matching some_field, rather than reading an attached label. Edit: It does actually look for an attached label - thanks to AlistairH for the correction

I would suggest the best way to get the behaviour you want is using a within block:

within 'form1' do
    fill_in 'Username', :with => value
end

Replace 'form1' with the name of whichever form contains the textbox you'd like to target.

I would consider this to be far more reliable, and readable, than relying on the presence of spaces or colons to differentiate between almost identically named elements on the page

Upvotes: 3

Related Questions