Capybara :: ElementNotFound:无法找到任何css元素

时间:2016-08-16 12:53:35

标签: ruby-on-rails ruby rspec capybara

我正在编写Capybara测试,其中一个测试用例是“删除”帖子 我不断得到这个错误。有什么帮助吗?

 Failure/Error:
   within "#post_#{post.id}" do
    click_link 'Delete'
   end

 Capybara::ElementNotFound:
   Unable to find css "#post_8"

代码

require 'rails_helper'

feature "Deleting a Post" do
    let(:user) { FactoryGirl.create(:user) }
    let(:blog) { FactoryGirl.create(:blog, user: user) }
    let(:post) { FactoryGirl.create(:post, blog: blog) }

    before do
        visit new_user_session_path

        fill_in('Email', with: user.email)
        fill_in('Password', with: user.password)

        click_on("Log in")
    end

    scenario "should delete a post" do

        visit posts_path(blog.url_slug)

        within "#post_#{post.id}" do #### Not able to find this element
            click_link 'Delete'
        end

        expect(page).not_to have_content(page.title)
        expect(Post.count).to eq(0)

    end

end

HTML

<div>
    <% @posts.each do |post| %>
        <div class="well well-lg">
            <div class="row" id="<%= dom_id(post) %>" >
                    <div class="col-md-2">
                        <%= image_tag @blog.user.gravatar(100) %>
                    </div>

                    <div class="col-md-10">
                        <h2>
                            <%= link_to post.title, "#" %>
                        </h2>
                        <p>
                            <%= @redcarpet.render(post.content).html_safe %>
                        </p>
                        <% if @user_have_access %>
                            <%= link_to edit_post_path(@blog.url_slug, post.id), id: post.id do %>
                                Edit
                                <span class="glyphicon glyphicon-pencil space"></span>
                            <% end %>
                            <%= link_to delete_post_path(@blog.url_slug, post.id), method: :delete, 
                                data: { confirm: 'Are you sure?' } do %>
                                Delete
                                <span class="glyphicon glyphicon-trash space"></span>
                            <% end %>
                        <% end %>
                        <%= link_to comments_path(@blog.url_slug, post.id) do %>
                            Comments
                            <span class="glyphicon glyphicon-comment"></span>
                        <% end %>
                    </div>
            </div>
        </div>
    <% end %>
</div>

的Gemfile

source 'https://rubygems.org'
ruby "2.3.1"

gem 'rails', '4.2.2'
gem 'mysql2', '~> 0.3.18'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails'
gem 'turbolinks'
gem "figaro"
gem 'bootstrap-sass', '~> 3.2.0'
gem 'devise'
gem 'aws-sdk', '~> 2.3'
gem "paperclip", "~> 5.0.0"
gem 'puma'
gem 'simple_form'
gem 'redcarpet'
gem 'rails_12factor'

group :test do
    gem 'rspec-rails'
    gem 'factory_girl_rails'
    gem 'faker'
    gem 'database_cleaner'
end

group :development do

  gem 'pry'
  gem 'byebug'
  gem 'capybara'

end

当我尝试使用save_and_open_screenshot截取屏幕截图时,出现以下错误

Capybara::NotSupportedByDriverError Exception: Capybara::Driver::Base#save_screenshot

以下是我的DOM

的屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:1)

let懒惰地定义变量,这意味着在第一次使用之前,该值实际上并未设置(在本例中为Post对象)。在您的情况下,第一次使用直到within语句,这意味着直到页面呈现之后(在访问调用期间)才创建记录,因此该项目实际上不在页面上。如果您需要在定义记录时使用该记录,则应使用let!

NotSupportedByDriver异常的原因是你使用的默认机架测试驱动程序没有屏幕概念(并且不支持JS)并且没有真正的CSS支持,因此没有实现屏幕截图