Capybara::ElementNotFound: 错误 [英] Capybara::ElementNotFound: Error

查看:51
本文介绍了Capybara::ElementNotFound: 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 railstutorial.org 教程,但出现以下错误

 1) 使用有效信息的用户页面注册应该创建一个用户失败/错误:fill_in "password" , with: "foobar"水豚::ElementNotFound:无法填写,未找到带有 ID、名称或标签密码"的文本字段、文本区域或密码字段# (eval):2:in `fill_in'# ./spec/requests/user_pages_spec.rb:32:in `block (4 levels) in <top (required)>'2) 信息无效的认证登录失败/错误:在 { click_button "Sign in" } 之前水豚::ElementNotFound:找不到带有值或 ID 或文本登录"的按钮# (eval):2:in `click_button'# ./spec/requests/authentication_pages_spec.rb:14:in `block (4 levels) in <top (required)>'3) 信息无效的认证登录失败/错误:在 { click_button "Sign in" } 之前水豚::ElementNotFound:找不到带有值或 ID 或文本登录"的按钮# (eval):2:in `click_button'# ./spec/requests/authentication_pages_spec.rb:14:in `block (4 levels) in <top (required)>'4) 访问另一个页面后使用无效信息进行身份验证登录失败/错误:在 { click_button "Sign in" } 之前水豚::ElementNotFound:找不到带有值或 ID 或文本登录"的按钮# (eval):2:in `click_button'# ./spec/requests/authentication_pages_spec.rb:14:in `block (4 levels) in <top (required)>'在 3.67 秒内完成46 个例子,4 个失败

和我的 authentication_pages_spec.rb

需要'spec_helper'描述身份验证"做主题{页面}描述登录页面"做{ 访问 signin_path } 之前它{应该有_选择器('h1',文本:'登录')}it { should have_selector('title', text: 'Sign in') }结尾描述登录"做{ 访问 signin_path } 之前描述使用无效信息"做{ click_button "登录" } 之前it { should have_selector('title', text: 'Sign in') }它{应该有_选择器('div.alert.alert-error',文本:'无效')}描述在访问另一个页面之后"做{ click_link "首页" } 之前它 { should_not have_selector('div.alert.alert-error') }结尾结尾let(:user) { FactoryGirl.create(:user) }在做之前填写电子邮件",使用:user.email填写密码",用:user.passwordclick_button "登录"结尾它{should have_selector('title', text: user.name) }它{should have_link('Profile', href:user_path(user))}它{should have_link('退出',href: signout_path)}它{should_not have_link('登录',href: signin_path)}结尾结尾

和 app/view/sessions/new.html.erb

<% provide(:title, "登录") %><h1>登录</h1><div class="row"><div class="span6 offset3"><%= form_for(:session, url: session_path) 做 |f|%><%= f.label :email %><%= f.text_field :email %><%= f.label :password %><%= f.password_field :password %><%= f.submit "Sign in", class: "btn btn-large btn-primary" %><%结束%><p>新用户?<%= link_to "立即注册!", signup_path %></p>

解决方案

您缺少一个 describe 块.让你的代码看起来像这样:

需要'spec_helper'描述身份验证"做主题{页面}描述登录页面"做{ 访问 signin_path } 之前它{应该有_选择器('h1',文本:'登录')}it { should have_selector('title', text: 'Sign in') }结尾描述登录"做{ 访问 signin_path } 之前描述使用无效信息"做{ click_button "登录" } 之前it { should have_selector('title', text: 'Sign in') }它{应该有_选择器('div.alert.alert-error',文本:'无效')}描述在访问另一个页面之后"做{ click_link "首页" } 之前它 { should_not have_selector('div.alert.alert-error') }结尾结尾# 缺少以下块描述有有效信息"做let(:user) { FactoryGirl.create(:user) }在做之前填写电子邮件",使用:user.email填写密码",用:user.passwordclick_button "登录"结尾它 {should have_selector('title', text: user.name) }它 {should have_link('Profile', href:user_path(user))}它{should have_link('退出',href: signout_path)}它{should_not have_link('登录',href: signin_path)}结尾结尾结尾

Capybara 通常希望你使用它的 visit 方法,然后它才会让你使用它的 fill_in 方法.您忘记将最后的 before 块包装在 describe 块中,就像我在上面所做的那样,所以来自 before 块的代码实际上覆盖了前一个 before 块,其中您使用访问方法.所以这就是你遇到这个问题的原因.通过将最后一组断言包装在 describe 块中,您可以解决问题.

i am following railstutorial.org tutorial i am getting the following error

  1) User pages signup with valid information should create a user
     Failure/Error: fill_in "password" , with: "foobar"
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'password' found
     # (eval):2:in `fill_in'
     # ./spec/requests/user_pages_spec.rb:32:in `block (4 levels) in <top (required)>'

  2) Authentication signin with invalid information 
     Failure/Error: before { click_button "Sign in" }
     Capybara::ElementNotFound:
       no button with value or id or text 'Sign in' found
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:14:in `block (4 levels) in <top (required)>'

  3) Authentication signin with invalid information 
     Failure/Error: before { click_button "Sign in" }
     Capybara::ElementNotFound:
       no button with value or id or text 'Sign in' found
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:14:in `block (4 levels) in <top (required)>'

  4) Authentication signin with invalid information after visiting another page 
     Failure/Error: before { click_button "Sign in" }
     Capybara::ElementNotFound:
       no button with value or id or text 'Sign in' found
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:14:in `block (4 levels) in <top (required)>'

Finished in 3.67 seconds
46 examples, 4 failures

and my authentication_pages_spec.rb

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }
    it { should have_selector('h1', text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end

  describe "signin" do
    before { visit signin_path }
    describe "with invalid information" do
      before { click_button "Sign in" }
      it { should have_selector('title', text: 'Sign in') }
      it { should have_selector('div.alert.alert-error', text: 'Invalid') }
      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
      end
    end


    let(:user) { FactoryGirl.create(:user) }
    before do
      fill_in "Email", with: user.email
      fill_in "Password", with: user.password
      click_button "Sign in"
    end

    it{should have_selector('title', text: user.name) }
    it{should have_link('Profile', href:user_path(user))}
    it{should have_link('Sign out',href: signout_path)}
    it{should_not have_link('Sign in',href: signin_path)}
  end
end 

and app/view/sessions/new.html.erb

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>
<div class="row">
  <div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>
      <%= f.label :email %>
      <%= f.text_field :email %>
      <%= f.label :password %>
      <%= f.password_field :password %>
      <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>
    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>

解决方案

You have a missing describe block. Make your code look like this:

require 'spec_helper'

describe "Authentication" do
  subject { page }

  describe "signin page" do
    before { visit signin_path }
    it { should have_selector('h1', text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end

  describe "signin" do
    before { visit signin_path }
    describe "with invalid information" do
      before { click_button "Sign in" }
      it { should have_selector('title', text: 'Sign in') }
      it { should have_selector('div.alert.alert-error', text: 'Invalid') }
      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
      end
    end

    # The following block is missing
    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before do
        fill_in "Email", with: user.email
        fill_in "Password", with: user.password
        click_button "Sign in"
      end

      it {should have_selector('title', text: user.name) }
      it {should have_link('Profile', href:user_path(user))}
      it{should have_link('Sign out',href: signout_path)}
      it{should_not have_link('Sign in',href: signin_path)}
     end
  end
end

Capybara usually wants you to use it's visit method before it will let you use it's fill_in method. You have forgotten to wrap your final before block in a describe block, like I have done above, and so the code from that before block actually overrides the previous before block, where you use the visit method. So that's why you have this problem. By wrapping the last bunch of assertions in a describe block, you fix the issue.

这篇关于Capybara::ElementNotFound: 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆