gpt4 book ai didi

ruby-on-rails - Capybara::ElementNotFound:无法找到任何 css 元素

转载 作者:数据小太阳 更新时间:2023-10-29 08:02:53 25 4
gpt4 key购买 nike

我正在编写 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>

gem 文件

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

最佳答案

所以我找到了问题所在。 PostsController#index 在创建帖子之前被调用。因此,DOM 未填充且无法找到元素。在我将创作移入场景 block 之后。它开始按预期工作。

    require 'rails_helper'

feature "Deleting a Post" do
let(:user) { FactoryGirl.create(:user) }
let(:blog) { FactoryGirl.create(:blog, user: user) }
let(:post) {FactoryGilr.create(:post, blog: blob} ### This doesn't create post yet. I am not sure why.

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

### If I put my post creation here, it does work though.
post = FactoryGirl.create(:post, blog: blog)

visit posts_path(blog.url_slug)

within "#post_#{post.id}" do
click_link 'Delete'
end

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

end

end

关于ruby-on-rails - Capybara::ElementNotFound:无法找到任何 css 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38975523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com