我有以下测试
require "rails_helper"
RSpec.feature "Menu", :type => :feature do
let!(:articles) { create_list(:article, 10) }
let!(:quotes) { create_list(:quote, 2) }
before { visit "/" }
scenario "should not have Javascript errors" do
Capybara.current_driver = :webkit
page.find("#menu-button").click
expect(page).not_to have_errors
end
end
它失败并显示消息
水豚:: ElementNotFound: 无法找到css"#menu-button"
我也试过
click_on "Open Menu"
click_button "Open Menu"
click_link "Open Menu"
page.find_button(id: 'menu-button').click
find("#menu-button").click
我正在尝试测试基础6具有的画布外功能。
application.html.erb文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><%= content_for?(:title) ? yield(:title) : "Stargazers.news" %></title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application", 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="off-canvas-wrapper">
<div class="off-canvas position-left" id="offCanvas" data-off-canvas>
<button class="close-button" arial-label="Close menu" type="button" data-close></button>
<div id="search">
<%= form_tag({controller: "/pages", action:"search"}, method: "get") do %>
<%= label_tag(:search, "Search for:") %>
<%= text_field_tag(:search) %>
<%= submit_tag("Search") %>
<% end %>
</div>
</div>
<div class="off-canvas-content" data-off-canvas-content>
<div class="top-bar">
<div class="top-bar-right">
<button id="menu-button" type="button" class="button" data-toggle="offCanvas">Open Menu</button>
</div>
</div>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</div>
</div>
</body>
</html>
index.html.erb文件
<div id="quote">
<%= @quote.body %>
</div>
<% @articles.each do |article| %>
<article>
<header> <%= article.title%> </header>
<div class="content-summary"> <%= article.body %> </div>
</article>
<hr/>
<% end %>
rails_helper.rb文件
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'simple_bdd/rspec'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
Capybara.javascript_driver = :webkit
config.include FactoryGirl::Syntax::Methods
config.include RSpecHtmlMatchers
config.include Devise::Test::IntegrationHelpers, type: :feature
config.use_transactional_fixtures = true
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
的Gemfile
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
gem 'rails', '~> 5.0.1'
gem 'pg', '~> 0.18'
gem 'puma', '~> 3.0'
gem 'sass-rails', '~> 5.0'
gem 'foundation-rails'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'jquery-rails'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem "paperclip", "~> 5.0.0"
gem 'devise'
group :development, :test do
gem 'byebug', platform: :mri
gem 'factory_girl_rails'
gem 'rspec-rails', '~> 3.5'
gem 'rspec-html-matchers'
gem 'ffaker'
gem 'simple_bdd'
end
group :development do
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.0.5'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem "better_errors"
end
group :test do
gem 'capybara-webkit'
gem 'database_cleaner'
gem 'shoulda-matchers', '~> 3.1'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
我的qt版本是5.5.1 我在Ubuntu 16.04
答案 0 :(得分:2)
默认情况下,Capybara找不到不可见元素,99%的时间它不能与不可见元素进行交互。这是因为它旨在模拟用户行为,用户无法与他们在页面上看不到的内容进行互动。
更新:既然您已经将整个测试包括在内,那么问题就更清晰了。您没有设置要使用的测试:webkit直到访问之后&#39; /&#39;已经发生了。这意味着:racktest驱动程序访问该页面,但随后您切换到:webkit驱动程序,该驱动程序实际上没有加载任何页面,因此没有任何内容。您可以使用js: true
(或driver: :webkit
)元数据标记测试,而不是在测试中手动设置current_driver
scenario "should not have Javascript errors", js: true do
...
end
然后,在访问发生之前,将当前驱动程序设置为Capybara.javascript_driver(您已设置为:webkit)的值。
此外,您的database_cleaner设置缺少 - 它应该是建议的设置 - https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example - 从after到append_after的交换对于测试稳定性很重要,并且检查驱动程序类型将有助于加快测试速度。< / p>
最后,如果您已经使用:type => :feature
RSpec.feature
答案 1 :(得分:0)
尝试在visible: false
来电中添加find
,如下所示:
page.find("#menu-button", visible: false).click