ruby-on-rails Chromedriver被测试数据库拒绝

jhkqcmku  于 2023-08-08  发布在  Ruby
关注(0)|答案(1)|浏览(111)

我有一个2017年的Rails项目,我正在尝试编写Rspec测试。它使用水豚,FactoryBot和Devise。
我使用的是Chromedriver,它在第一次Rspec系统测试期间无法登录用户-chrome浏览器会发出错误凭据的引导警告。
我已经确认用户确实在测试数据库中,并且使用binding. pry具有正确的凭据。
如果我不使用驱动程序(即,如果我从scenario中取出:js => true),它会很好地记录用户。
我已经安装了webdrivers gem,它运行的是chromedriver 114.0.5735.90,在rails_helper中有这个:

Capybara.register_driver :selenium_chrome do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.default_driver = :selenium_chrome
Capybara.javascript_driver = :selenium_chrome

字符串
application.rb

require 'webdrivers/chromedriver'
Webdrivers::Chromedriver.required_version = "114.0.5735.90"


test.log

Completed 401 Unauthorized in 9ms (ActiveRecord: 2.4ms)
Processing by Devise::SessionsController#new as HTML
  Parameters: {"utf8"=>"✓", "fan"=>{"email"=>"test1@example.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}


宝石

gem 'rails', '>= 5.0.1.rc2', '< 5.1'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
  gem 'rspec-rails', '~> 3.5.0'
  gem 'puma', '~> 3.0'
end

group :test do
  gem 'capybara', '~> 3.6.0'
  gem 'factory_girl_rails', '~> 4.7'
  gem 'faker'
  gem 'selenium-webdriver', '~> 3.6.0'
  gem "database_cleaner", "~> 1.5"
  gem 'shoulda-matchers', '~> 3.1'
  gem 'launchy'
end


...我尝试使用geckodriver代替,但得到这个错误:

Failures:

  1) Splash and devise: Fans in Canada can sign in, visit signed in home page, and log out in
     Failure/Error: click_link('LOG IN')
     
     Selenium::WebDriver::Error::UnknownError:
       Invalid Content-Type


更新
托马斯,非常感谢你。
我删除了另一个多余的问题,以及服务器端口/app_host。
所以...我们有三个不同的线程不通信。最有可能是一个过时的问题,如果项目被更新,但我有它的想法,以创建测试,为这个项目之前,试图更新它(我不是一个专业的开发人员)。
我开始研究线程问题,这让我想到了this问题,这让我想到了this帖子,其中(在第3节)有关于如何获得活动记录以运行与测试相同的线程的代码,它起作用了。
然而,它还提到将use_transactional_fixtures设置为true,并且不再使用数据库清理器(rails 5.0.1),我尝试了,但收到了以下错误:

An error occurred in a `before(:suite)` hook.
Failure/Error:
        raise(<<-MSG)
          Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
          (or set it to false) to prevent uncommitted transactions being used in
          JavaScript-dependent specs.
  
          During testing, the app-under-test that the browser driver connects to
          uses a different database connection to the database connection used by
          the spec. The app's database connection would not be able to access
          uncommitted transaction data setup over the spec's database connection.
        MSG


否则,这就是我目前的配置。它是factorybot,design,rspec,水豚,chromedriver.我不认为任何其他配置修改已作出,将看起来更多的午睡后。
轨道辅助程序

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require "devise"
require "pundit/rspec"
require 'capybara/rails'

Dir[Rails.root.join('spec/support/*.rb')].each { |f| require f }

# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

Capybara.register_driver :selenium_chrome do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.default_driver = :selenium_chrome
Capybara.javascript_driver = :selenium_chrome

Capybara.run_server = true 

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

RSpec.configure do |config|
  config.include Devise::Test::ControllerHelpers, type: :controller

  # controller spec macros
  config.extend ControllerMacros, :type => :controller

  # Include Factory Girl syntax to simplify calls to factories
  config.include FactoryGirl::Syntax::Methods 
  
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false

  config.infer_spec_type_from_file_location!
  config.include Rails.application.routes.url_helpers
  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")
  config.include Warden::Test::Helpers, type: :feature
  config.after(type: :feature) { Warden.test_reset! }
  config.include Capybara::DSL
  config.filter_run_when_matching focus: true

end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end


application.rb

require 'webdrivers/chromedriver'
Webdrivers::Chromedriver.required_version = "114.0.5735.90"


.现在看,没有看到任何提及gemlist之外的数据库清理器,但在测试数据库中只有一个factorybot启动的用户,所以我假设某个地方正在工作。
如果没问题我就继续我已经花了10多天的时间在制作50个系统测试的第一个场景

rxztt3cl

rxztt3cl1#

你正在创建关于同一个问题的多个问题,这肯定会让你自己被禁止堆栈溢出。你也跳到假设,似乎没有任何逻辑基础,如这是一个 chrome 驱动程序的问题。
根据所提供的信息
1.为什么要手动设置Capybara.服务器端口?如果你没有一个真正的好理由这样做,删除它。
1.为什么要设置Capybara.app_host,与#1相同。

  1. I have confirmed the user is indeed in test database, and with the correct credentials using binding.pry.--你把装订线放哪了,撬?它是在测试代码中还是在应用程序代码中?您是否直接连接到数据库并检查其中是否有记录?
    1.当使用selenium运行Capybara时,您需要执行多个操作:a)Capybara在一个线程中运行应用程序-该线程具有自己的数据库连接。B)测试在另一个线程中运行,并有自己的数据库连接c)浏览器在自己的进程中运行,并连接到被测应用程序鉴于这些情况,有几种可能性
    1.您正在事务模式下运行,这意味着在测试线程中创建的记录对于应用程序是不可见的--在测试暂停时直接连接到DB(在ruby之外),并检查记录是否实际上被提交到DB将验证这一点
    1.您的应用程序实际上并没有连接到您的测试所使用的数据库--还有哪些默认配置被更改了?
    1.您是在告诉浏览器连接到在不同DB上运行的应用程序的完全不同的示例(可能是dev示例?)-因此建议停止覆盖Capybaras设置,让它做它的事情。你有没有看过浏览器,看过它的网址?查看浏览器控制台,并查看它将登录凭据发布到何处,等等?
    如果你把你使用的软件版本更新到现代水平,这一切都会更容易。例如,较新的Rails将在测试和应用程序之间共享DB连接,从而允许在事务模式下运行,并且在大多数情况下无需数据库清理器等,从而简化了设置

相关问题