Capybara测试使用selenium webdriver失败

时间:2018-06-04 17:52:25

标签: ruby-on-rails selenium-webdriver rspec capybara spree

我正在尝试测试狂欢扩展。一些测试是功能测试。这些测试使用selenium-webdriver作为javascript驱动程序。 这是扩展名的gemspec。

spree_extension.gemspec

# Runtime
s.add_dependency 'spree_core', '>= 3.2', '< 3.6'
s.add_dependency 'spree_sample', '>= 3.2', '< 3.6'
# Development
s.add_development_dependency 'appraisal'
s.add_development_dependency 'shoulda-matchers',   '~> 3.1.1'
s.add_development_dependency 'factory_bot',       '~> 4.8.2'
s.add_development_dependency 'coffee-rails',       '~> 4.2.1'
s.add_development_dependency 'database_cleaner',   '~> 1.5.3'
s.add_development_dependency 'sqlite3',            '~> 1.3.11'
s.add_development_dependency 'capybara',           '~> 2.7.1'
s.add_development_dependency 'selenium-webdriver', '~> 2.53.0'
s.add_development_dependency 'launchy',            '~> 2.4.3'
s.add_development_dependency 'rspec-rails',        '~> 3.7'

spec_helper.rb

# Configure Rails Environment
ENV['RAILS_ENV'] = 'test'

require File.expand_path('../dummy/config/environment.rb',  __FILE__)

require 'rspec/rails'
require 'capybara/rspec'
require 'database_cleaner'
require 'factory_bot'
FactoryBot.find_definitions
require 'ffaker'
require 'paperclip/matchers'
require 'shoulda/matchers'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }

# Requires factories defined in spree_core
require 'spree/testing_support/factories'
require 'spree/testing_support/controller_requests'
require 'spree/testing_support/authorization_helpers'
require 'spree/testing_support/capybara_ext'
require 'spree/testing_support/url_helpers'

RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.include Paperclip::Shoulda::Matchers

# == URL Helpers
#
# Allows access to Spree's routes in specs:
#
# visit spree.admin_path
# current_path.should eql(spree.products_path)
config.include Spree::TestingSupport::UrlHelpers
config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :feature
config.include Spree::TestingSupport::ControllerRequests, type: :controller

# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
config.color = true

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = File.join( File.dirname(__FILE__), 'spec/fixtures')

# Capybara javascript drivers require transactional fixtures set to false, and we use DatabaseCleaner
# to cleanup after each test instead.  Without transactional fixtures set to false the records created
# to setup a test will be unavailable to the browser, which runs under a seperate server instance.
config.use_transactional_fixtures = false

config.before :each do |example|
  if example.metadata[:js]
    DatabaseCleaner.strategy = :truncation
  else
    DatabaseCleaner.strategy = :transaction
  end
  DatabaseCleaner.start
end

config.after :each do
  DatabaseCleaner.clean
end

config.fail_fast = ENV['FAIL_FAST'] || false

# rspec-rails 3 will no longer automatically infer an example group's spec type
# from the file location. You can explicitly opt-in to the feature using this
# config option.
# To explicitly tag specs without using automatic inference, set the `:type`
# metadata manually:
#
#     describe ThingsController, :type => :controller do
#       # Equivalent to being in spec/controllers
#     end
config.infer_spec_type_from_file_location!
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    with.test_framework :rspec

    # Or, choose the following (which implies all of the above):
    with.library :rails
  end
end

我使用的是Firefox v45.0。问题是尽管Firefox窗口打开并且测试运行,但与javascript(在扩展中定义)交互的功能测试失败。

这里有一个例子:

scenario "expect to have a disabled add to cart button" do
  visit spree.product_path(product)
  within "div.add-to-cart" do
    expect(page).to have_css('button#add-to-cart-button')
    expect(page.find('button#add-to-cart-button').disabled?).to be_truthy  # <= This test here is failing
  end
end

我正在禁用此扩展程序中定义的javascript中的按钮,但测试失败,因为我认为我的测试从未见过。

selenium是否需要更多javascript设置?

1 个答案:

答案 0 :(得分:0)

好的,所以我弄清楚出了什么问题。在制作虚拟应用程序时,生成器没有运行。 lib目录中的生成器必须放在安装目录中。 所以目录结构必须是

lib
 -> generators
   -> *extension_name*
     -> install
       -> install_generator.rb 

那时为测试而制作的虚拟应用程序将看到生成器运行。