Capybara::ElementNotFound: Unable to find field “title”

陌路散爱 提交于 2020-01-13 19:22:32

问题


I could not resolve this issue. Please help me. It gives me element not found error.

spec/features/todos/create_spec.rb

require 'spec_helper'

describe "Creating todos" do

 let(:user) { FactoryGirl.create(:user)}
 before(:each) do
   visit root_path
   click_link "Login"

   fill_in "Email",  with:  "user@gmail.com"
   fill_in "Password",  with: "password"
   click_button "Sign in"
 end

  it "redirects to the todos index page" do
   visit "/todos"

   fill_in "title", with: "MyString"
   click_button "Create"

   expect(page).to have_title("MyString")
 end

my view code. _new_form.html.erb

     <%= form_for current_user.todos.new, remote:true  do |f| %>
       <div class="input-group">
         <%= f.text_field :title, class: "form-control",   placeholder: "title" %>
         <span class="input-group-btn">
         <%= f.submit "Create", class: "btn btn-success" %>
         </span>
       </div>
     <% end %>

spec_helper.rb

    def sign_in
      @user = FactoryGirl.create(:user)
      controller.stub(:authenticate_user!).and_return(true)
      @user
    end

回答1:


If you have the code running on a server locally, inspect the element and the name of the field is what Capybara needs to run. Generally, if the form is nested, the name rails will come up with (in this case) is something like todos[title].

So, spec/features/todos/create_spec.rb should look something like:

require 'spec_helper'
 ...

it "redirects to the todos index page" do
 visit "/todos"

 fill_in "todos[title]", with: "MyString"
 click_button "Create"

 expect(page).to have_title("MyString")
end



回答2:


Found my answer.

In the view file I've a text_field with title with an id. The id = todo_title in the console But am calling title in the test . Here the capybara not been able to find title. It was todo_title

After using

fill_in "todo_title"

It worked like charm.




回答3:


In your form, there must be an element with the name as "title". From the docs:

fill_in(locator, options = {}) ⇒ Object

Locate a text field or text area and fill it in with the given text The field can be found via its name, id or label text.


来源:https://stackoverflow.com/questions/27227328/capybaraelementnotfound-unable-to-find-field-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!