简体   繁体   English

Capybara :: ElementNotFound:无法找到字段“title”

[英]Capybara::ElementNotFound: Unable to find field “title”

I could not resolve this issue. 我无法解决这个问题。 Please help me. 请帮我。 It gives me element not found error. 它给了我找不到元素的错误。

spec/features/todos/create_spec.rb 规格/功能/待办事项/ 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 _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 spec_helper.rb

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

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. 如果您在本地服务器上运行代码,请检查元素,并且字段的名称是Capybara需要运行的。 Generally, if the form is nested, the name rails will come up with (in this case) is something like todos[title] . 通常,如果表单是嵌套的,那么名称rails将会出现(在这种情况下)就像todos[title]

So, spec/features/todos/create_spec.rb should look something like: 因此,spec / features / todos / create_spec.rb应该类似于:

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

Found my answer. 找到我的答案。

In the view file I've a text_field with title with an id. 在视图文件中,我有一个带有id的标题的text_field。 The id = todo_title in the console But am calling title in the test . 控制台中的id = todo_title但是我在测试中调用了title。 Here the capybara not been able to find title. 这里的水豚未能找到头衔。 It was todo_title 这是todo_title

After using 使用后

fill_in "todo_title"

It worked like charm. 它像魅力一样工作。

In your form, there must be an element with the name as "title". 在您的表单中,必须有一个名称为“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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM