Chromedriver被测试数据库拒绝。

huangapple go评论87阅读模式
英文:

Chromedriver getting rejected by test database

问题

I understand that you want specific parts of the text translated. Here are the translations of the relevant code-related portions:

# Capybara注册Selenium Chrome驱动程序
Capybara.register_driver :selenium_chrome do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

# 设置默认的Capybara驱动程序为Selenium Chrome
Capybara.default_driver = :selenium_chrome
Capybara.javascript_driver = :selenium_chrome
# 应用程序中的配置,设置Chromedriver的版本
require 'webdrivers/chromedriver'
Webdrivers::Chromedriver.required_version = "114.0.5735.90"

These translations cover the configuration of the Selenium Chrome driver and the required Chromedriver version in your code.

If you have more specific code-related questions or need further assistance, please feel free to ask.

英文:

Have 2017 Rails project I'm trying to write Rspec tests for. It uses Capybara, FactoryBot and Devise.

I'm using Chromedriver, which fails to sign in the user during the first Rspec system test - the chrome browser brings up the bootstrap alert of wrong credentials.

I have confirmed the user is indeed in test database, and with the correct credentials using binding.pry.

If I don't use a driver (ie., if I take out :js => true from scenario), it logs the user in fine.

I have installed webdrivers gem, it is running chromedriver 114.0.5735.90, have this in the 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


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

... I tried using geckodriver instead, but get this error:

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

UPDATE

Thomas, much appreciated thank you.

I erased the other redundant question, as well as the server port/app_host.

So... we've got 3 different threads that do not communicate. Most likely an antiquated issue if project was updated, but I had it in mind to create the tests for this project before attempting updating it (I am not a professional developer).

I started to research the thread issue, which led me to this question, which led me to this post, which has (in section 3) code on how to get active record to run the same thread as the test, which worked.

However, it also mentions putting use_transactional_fixtures to true and no longer using database cleaner (rails 5.0.1), which I tried, but received this error:

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

Otherwise, this is my current configuration. It is factorybot, devise, rspec, capybara, chromedriver. I don't think any other config modifications have been made, will look more after a nap.

rails_helper

# 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"

... looking now, don't see any mention of database cleaner outside of the gemlist, but there's only ever one factorybot initiated user in test db, so I assume something's working somewhere.

if this is cool I'll proceed. i've been working on this first scenerio of the first of 50 system tests i want to make for over 10 days

答案1

得分: 2

你正在创建关于同一个问题的多个问题,这是被封禁StackOverflow的确切方法。你还在做出似乎没有任何逻辑基础的假设,比如它是chromedriver的问题。

鉴于提供的信息

  1. 你为什么要手动设置Capybara.server的端口?如果没有非常充分的理由,就移除它。
  2. 你为什么要设置Capybara.app_host,与问题1相同。
  3. "我已经确认用户确实在测试数据库中,并且使用binding.pry检查了正确的凭据。" - 你把binding.pry放在哪里了?它是在测试代码中还是在应用程序代码中?你直接连接到数据库并检查是否有记录吗?
  4. 当使用selenium运行Capybara时,有多个因素
    a) Capybara在一个线程中运行应用程序 - 它有自己的数据库连接。
    b) 测试在另一个线程中运行,并有它们自己的数据库连接
    c) 浏览器在其自己的进程中运行,并连接到正在测试的应用程序
    鉴于这些因素,有几种可能性

    1. 你可能在事务模式下运行,这意味着在测试线程中创建的记录对应用程序是不可见的 - 在测试暂停时直接连接到数据库(在ruby之外)并检查记录是否真的提交到数据库会验证这一点。
    2. 你的应用程序实际上可能没有连接到你的测试所使用的数据库 - 是否更改了其他默认配置?
    3. 你可能在告诉浏览器连接到完全不同的应用程序实例,运行在不同的数据库上(可能是开发实例?)- 因此建议停止覆盖Capybara的设置,让它自己完成。你有没有观察浏览器,并查看它访问的URL是什么?查看浏览器控制台并查看它将登录凭据发送到哪里等等?

如果你更新正在使用的软件版本到较新的水平,一切都会更容易。例如,更新的Rails将在测试和应用程序之间共享数据库连接,允许在事务模式下运行,并在大多数情况下消除了数据库清理器等等 - 简化了设置。

英文:

You're creating multiple questions about the same issue, that's a sure way to get yourself banned from stackoverflow. You're also jumping to assumptions that don't seem to have any logical basis, like that it's a chromedriver issue.

Given the information provided

  1. Why are you manually setting Capybara.server port? If you don't have a really good reason for doing that, remove it.
  2. Why are you setting Capybara.app_host, same as #1.
  3. I have confirmed the user is indeed in test database, and with the correct credentials using binding.pry. -- where did you put the binding.pry? Was it in test code or in application code? Did you connect to the DB directly and check if there is a record in it?
  4. When running Capybara using selenium you have multiple things going on
    a) Capybara runs the application in one thread - which has it's own connection to the database.
    b) Tests are run in another thread, and have their own DB connection
    c) Browser runs in its own process and connects to the application under test
    Given those things, there are a few possiblities

    1. you're running in transaction mode, which means records created in the test thread wouldn't be visible to the application - connecting to the DB directly (outside of ruby) while the tests are paused and checking if the records are actually getting committed to the DB would verify that
    2. your application isn't actually connecting to the DB your tests are - what other default configurations have been changed?
    3. you're telling the browser to connect to a completely different instance of the application running against a different DB (dev instance potentially?) - hence the suggestion to stop overriding Capybaras settings and just let it do it's thing. Have you watched the browser and seen what url it's going to? Look at the browser console and see where it posted the login credentials to, etc?

This would all be easier if you updated the versions of software you're using to modern levels. For instance, newer rails will share the DB connection between the tests and application allowing to run in transaction mode and removing the need for database-cleaner, etc in most situations - simplifying setup

huangapple
  • 本文由 发表于 2023年8月5日 06:31:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定