Solve (Capybara::ElementNotFound) with simple form and cucumber

807 Views Asked by At

I'm using cucumber to test and I'm using simple_form for the contact form. I get the following error:

Given I am on the login page 
And I fill in "student_name" with "sadik" 
    Unable to find field "student_name" (Capybara::ElementNotFound)

my steps:

Scenario: Add a student to the database
  Given I am on the login page
  And I fill in "student_name" with "sadik"
  When I press "OK"
  ...

The form:

<%= simple_form_for @student do |f| %>
  <%= f.input :name, label: 'student_name' %>,
    :input_html => { :field => 'student_name' } %>
  <%= f.button :submit %>
<% end %>

I also tried id and name instead of label. But it had no effect.

But the first step is correct:

When /^I go to (.+)$/ do |page_name|
  visit path_to(page_name)
end

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

So where is the problem?

1

There are 1 best solutions below

2
jakmarkiewicz On

Your syntax and labels seem incorrect.

For the form try:

<%= simple_form_for @student do |f| %>
   <%= f.input :name, as: :text, label: 'Student Name' %>
   <%= f.button :submit %>
<% end %>

And for the last step try:

When /^I fill in student_name with "([^\"]*)"$/ do |value|
  fill_in "Student Name", :with => value
end

edit: You might also need to change this line and remove the quotes:

And I fill in student_name with "sadik"