blinkomania
blinkomania

Reputation: 157

Cucumber Test Cannot find field when its there

While I was writing cucumber test code, I get:

Unable to find field "username" (Capybara::ElementNotFound)

But I have the following on the page itself.

<%= f.text_field :username, :placeholder => "Username" %>

I've checked that it lands on the correct page using

save_and_open_page
fill_in "username", :with => "TESTUSER"

isn't the tag :username? What am I supposed to write instead?

Upvotes: 0

Views: 1330

Answers (1)

user740584
user740584

Reputation:

Capybara will match fields based on their id, name or label text. See here for more details.

Since you are using the default text_field helper, id and name will default to include the model (e.g. user_username for the id, or user[username] for the name). You can change these defaults by simply using id: <id> or name: <name> on your text_field call but you might run into other problems later - so probably best to stay with the defaults.

Change your capybara test to fill_in "user_username" or fill_in "user[username]" to make it match. Alternatively, if you have a label_for on the field, you can match on the text of the label which can make your tests more readable.

Upvotes: 1

Related Questions