2
votes

I am starting with Cucumber and I am running this simple scenario:

Scenario: Creating a project
  Given I go to the new project page
  And I fill in "Name"  with "xxx"
  ...

The new project form is already in place and looks like this:

<% form_for (@project) do |f| %>
  <%= f.text_field :name %>
  ...
<% end %>

As you can see I haven't defined a label (don't need one). The problem is that cucumber doesn't find the field:

Given I go to the new project page
And I fill in "Name" with "I need something" # features/step_definitions/web_steps.rb:68 cannot fill in, no text field, text area or password field with id, name, or label 'Name' found (Capybara::ElementNotFound)

However it finds it if I add the label. I find it a little bit suspicious that it can't match the name I gave with the appropriate text field.

What is wrong?

Thank you

3

3 Answers

2
votes

The name of your textfield is by default:

project[name] 

Actually it follows the rule:

object[field]

You should look at your dom and check it.

Edit:

Jus FYI, It's weird to see:

<% form_for (@project) do |f| %>

instead of:

<%= form_for (@project) do |f| %>
5
votes

Try changing your cucumber feature to read:

Scenario: Creating a project
  Given I go to the new project page
  And I fill in "project_name" with "xxx"

Or...

Scenario: Creating a project
  Given I go to the new project page
  And I fill in "project[name]" with "xxx"

The reason this is failing is because if you have no label, capybara (which cucumber uses) looks for the ID or name attribute of the field. As apneadiving mentions, you can check these values by looking at the source of the generated file. You'll see something like:

<input id="project_name" name="project[name]" type="text">
0
votes

I had the same problem and I fixed it by simply changing

<% form_for ... %>

to

<%= for_for ... %>

The addition of the '=' enables the display of the form fields, which allows Capybara to correctly identify it.