简体   繁体   English

使用Selenium WebDriver进行水豚测试失败

[英]Capybara tests fail using selenium webdriver

I am trying to test a spree extension. 我正在尝试测试狂欢扩展。 Some of the tests are feature tests. 其中一些测试是功能测试。 These tests use selenium-webdriver as javascript driver. 这些测试使用selenium-webdriver作为javascript驱动程序。 Here is my gemspec for the extension. 这是我的扩展的gemspec。

spree_extension.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 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

I am using Firefox v45.0. 我正在使用Firefox v45.0。 The problem is that although the Firefox window opens and tests run, but the feature tests which interact with javascript (defined in the extension) are failing. 问题是,尽管Firefox窗口打开并运行测试,但是与javascript(在扩展名中定义)进行交互的功能测试失败。

As an example here is one scenario: 例如,这是一种情况:

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

I am disabling the button in the javascript that is defined in this extension, but the test fails because i think my tests are never seeing that. 我在此扩展中定义的javascript中禁用了按钮,但是测试失败,因为我认为我的测试从未看到过。

Does selenium require more setup for javascript? 硒是否需要对JavaScript进行更多设置?

Okay so I figured out what was wrong. 好的,所以我找出了问题所在。 The generators were not being run when making the dummy app. 制作虚拟应用程序时未运行发电机。 The generators in the lib directory have to be placed in the install directory. lib目录中的生成器必须放置在安装目录中。 So the directory structure has to be 所以目录结构必须是

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

That is when the dummy app that is made for testing will see the generators to run. 那就是为测试而制作的虚拟应用程序将看到生成器运行的时候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM