eayurt
eayurt

Reputation: 1177

Capybara::ElementNotFound:

I am testing my Rails app's sign up form with RSpec and Capybara. When I want to test it I face this error,

Failures:

  1) user registration allows new users to register with an email address and password
     Failure/Error: fill_in "Confirmation",          :with => "foo"
     Capybara::ElementNotFound:
       no text field, text area or password field with id, name, or label 'Confirmation' found
     # ./user_spec.rb:9:in `block (2 levels) in <top (required)>'

This is my form which is a sign up form,

<div id="box">
  <div class="block" id="block-signup">
    <h2>Sign up</h2>
    <div class="content">
      <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :class => "form login"}) do |f| %>
          <% if @user.errors.any? %>
              <div id="errorExplanation">
                <h2><%= pluralize(@user.errors.count, "error") %> prohibited
                  this post from being saved:</h2>
                <ul>
                  <% @user.errors.full_messages.each do |msg| %>
                      <li><%= msg %></li>
                  <% end %>
                </ul>
              </div>
              <%end%>
        <div class="group wat-cf">
          <div class="left">
            <label class="label"><%= f.label :email %></label>
          </div>
          <div class="right">
            <%= f.email_field :email %>
            <span class="description">Ex: [email protected]</span>
          </div>
        </div>
        <div class="group wat-cf">
          <div class="left">
            <label class="label"><%= f.label :password %></label>
          </div>
          <div class="right">
            <%= f.password_field :password %>
            <span class="description">Must contains the word 'yeah'</span>
          </div>
        </div>
        <div class="group wat-cf">
          <div class="left">
            <label class="label"><%= f.label :confirmation %></label>
          </div>
          <div class="right">
            <%= f.password_field :password_confirmation %>
            <span class="description">Don't miss any letters</span>
          </div>
        </div>

        <div class="group navform wat-cf">
          <button class="button" type="submit">
            <%= f.submit "Sign in", :type => :image, :src => "http://pilu.github.com/web-app-theme/images/icons/tick.png" %>
          </button>
        </div>
      <% end %>
    </div>
    <div id="links-sup">
      <h4><%= render "links" %></h4></div>
  </div>
</div>

And my test file is spec/requests/user_spec.rb

require 'spec_helper'

describe "user registration" do
  it "allows new users to register with an email address and password" do
    visit "/users/sign_up"

    fill_in "Email",                 :with => "[email protected] "
    fill_in "Password",              :with => "foo"
    fill_in "Confirmation",          :with => "foo"

    click_button "Sign up"

    page.should have_content("Welcome! You have signed up successfully.")
  end
end

You have any idea to solve it?

Upvotes: 1

Views: 6347

Answers (1)

Chamnap
Chamnap

Reputation: 4766

Well, the error here indicates that it cannot find textbox with label "Confirmation". Here you have problem with html. There is no textbox with Confirmation label. You can test by clicking that label "Confirmation", the cursor won't be in the password_confirmation textbox.

See the difference why click on "Password" label, the cursor is in the password textbox. That's why there is no error on password field.

You could change the markup a little bit to f.label :password_confirmation.

When you see the error Capybara::ElementNotFound, it simply means it cannot find that element you reference. Therefore you need to check your markup is correct.

Upvotes: 2

Related Questions